我的数据集名称如下;
Col A Col B Col C
Brook
Ram
Sunny
Mike
Crook
David
我想用" Hello Wolrd"替换B列中的空白。所有那些列c值与" Brook"," crook"," David"。
对此非常感谢。 感谢
答案 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