假设我有一个包含此类文本的txt文件:
Type: fruits
Title: retail
Date: 2015-11-10
Country: UK
Products:
apple,
passion fruit,
mango
Documents: NDA
Export: 2.10
我使用readLines
功能读取了此文件。然后,我想要一个看起来像这样的向量:
x <- c(fruits, apple, passion fruit, mango)
因此,我想提取“类型:”之后的词以及“产品:”和“文档:”之间的所有词。我怎样才能做到这一点?谢谢!
如果不进行更改,则看起来类似于yaml
格式,例如使用同名包装
library(yaml)
info <- yaml::read_yaml("your file.txt")
# strsplit - split either side of the commas
# unlist - convert to vector
# trimws - remove trailing and leading white space
out <- trimws(unlist(strsplit(info$Products, ",")))