【发布时间】:2017-12-05 06:43:10
【问题描述】:
我有一个包含两列的 data.frame DF。名称和分数。 我有一个包含名称的列表(the.list)。有些等于 DF$names 中名称中的名称。 我需要在 DF$score 中插入一个数字(2000),如果名称在.list 中并且分数为 NA
数据:
DF.scores <- data.frame(c("steve", "anna", "albert", "john", "sarah", "lily"), c(2000, 1500, NA, NA, NA, 1750))
names(DF.scores) <- c("names", "score")
the.list <- c("anna", "steve", "john")
我需要数据框这样结束:
names score
steve 2000
anna 1500
albert NA
john 2000
sarah NA
lily 1750
我已尝试对数据进行子集化,使用 which 命令但没有得到任何结果。
【问题讨论】:
-
...替换表格中的
DF.scores$score[DF.scores$names %in% the.list] <- 2000 -
我认为上述建议没有解决OP将分数设置为2000的标准,即
if the name is in the.list and the score is NA。
标签: r