根据R中的列c值替换列中的空白

时间:2015-03-04 14:07:37

标签: r

我的数据集名称如下;

Col A Col B Col C

            Brook

            Ram

            Sunny

            Mike

            Crook

            David

我想用" Hello Wolrd"替换B列中的空白。所有那些列c值与" Brook"," crook"," David"。

对此非常感谢。 感谢

2 个答案:

答案 0 :(得分:2)

df$colB[df$colB=="" & df$colC %in% c("Brook", "crook", "David")] <- "Hello Wolrd"

答案 1 :(得分:1)

也许是ifelse声明:

> df <- data.frame(A = "", B = "", C = c("Brook", "Ram", "Sunny", "Mike", "Crook", "David"), stringsAsFactors=FALSE)
> df
  A B     C
1     Brook
2       Ram
3     Sunny
4      Mike
5     Crook
6     David
> df$B <- ifelse(df$C %in% c("Brook", "Crook", "David"), "Hello World", "")
> df
  A           B     C
1   Hello World Brook
2                 Ram
3               Sunny
4                Mike
5   Hello World Crook
6   Hello World David