【发布时间】:2014-05-21 20:57:18
【问题描述】:
我有一个数据框,
d<-data.frame(name=c("brown cat", "blue cat", "big lion", "tall tiger",
"black panther", "short cat", "red bird",
"short bird stuffed", "big eagle", "bad sparrow",
"dog fish", "head dog", "brown yorkie",
"lab short bulldog"), label=1:14)
我想搜索name 列,如果这些词
出现“cat”、“lion”、“tiger”和“panther”,我想将字符串feline分配给新列和对应行species。
如果出现"bird", "eagle", and "sparrow"字样,我想将字符串avian分配给新列和对应行species。
如果出现单词“dog”、“yorkie”和“bulldog”,我想将字符串canine分配给新列和对应行species。
理想情况下,我会将其存储在一个列表或类似的东西中,我可以保留在脚本的开头,因为随着物种的新变种出现在名称类别中,很容易访问更新符合条件的 feline、avian 和 canine。
这个问题在这里几乎得到了回答 (How to create new column in dataframe based on partial string matching other column in R),但它没有解决这个问题中存在的多个名称扭曲。
【问题讨论】:
标签: r string dataframe matching