【发布时间】:2015-08-27 12:02:01
【问题描述】:
我有一个庞大的学生数据集,其中有针对荣誉学生的非标准命名约定。我需要创建/填充一个新列,该列将根据单词“Honours”返回 Y 或 N 进行字符串匹配
目前我的数据看起来像这样,有超过 200,000 名学生
library(data.table)
students<-data.table(Student_ID = c(10001:10005),
Degree= c("Bachelor of Laws", "Honours Degree in Commerce", "Bachelor of Laws (with Honours)", "Bachelor of Nursing with Honours", "Bachelor of Nursing"))
我需要添加第三列,以便在我以数据表方式创建新列“荣誉”后,它将像这样填充:
students<-data.table(Student_ID = c(10001:10005),
Degree= c("Bachelor of Laws", "Honours Degree in Commerce","Bachelor of Laws (with Honours)", "Bachelor of Nursing with Honours", "Bachelor of Nursing"),
Honours = c("N","Y", "Y", "Y","N"))
任何帮助将不胜感激。
另外,通过数据表的方式,我的意思是:
students[,Honours:="N"]
【问题讨论】:
-
您可以逐步进行以便于阅读:
idx <- grepl("honours", students$Degree, ignore.case = TRUE); students[idx, Honours := "Y"]; students[!idx, Honours := "N"]
标签: r string data.table multiple-columns