如果数据框中的一行包含特定值,请将该行中的所有条目转换为该值

时间:2015-03-11 14:50:02

标签: r

我的数据框看起来像这样:

b1 <- c(0,0,1,1,0,0,1,1)
b2 <- c(0,0,1,1,"NA",0,1,1)
b3 <- c(0,0,1,1,0,0,1,1)
b4 <- c(0,0,1,1,0,0,1,1)
b5 <- c(0,0,"NA",1,0,0,1,1)
b6 <- c(0,0,1,1,0,0,1,1)
b7 <- c(0,0,1,1,0,"NA",1,1)
b8 <- c(0,0,1,1,0,0,1,1)
df <- data.frame(b1,b2,b3,b4,b5,b6,b7,b8)
df

我想更改包含至少一个"NA"的任何行,以包含所有"NA" s,因此示例如下所示:

b1 <- c(0,0,"NA",1,"NA","NA",1,1)
b2 <- c(0,0,"NA",1,"NA","NA",1,1)
b3 <- c(0,0,"NA",1,"NA","NA",1,1)
b4 <- c(0,0,"NA",1,"NA","NA",1,1)
b5 <- c(0,0,"NA",1,"NA","NA",1,1)
b6 <- c(0,0,"NA",1,"NA","NA",1,1)
b7 <- c(0,0,"NA",1,"NA","NA",1,1)
b8 <- c(0,0,"NA",1,"NA","NA",1,1)
df <- data.frame(b1,b2,b3,b4,b5,b6,b7,b8)
df

我已经编写了这个功能来实现这一目标,但它不起作用:

NAconverter <- function(x){
df$x <- if(df$b1 == "NA" && df$b2 == "NA" && df$b3 == "NA" && df$b4 == "NA"  && df$b5 == "NA" && df$b6 == "NA" && df$b7 == "NA" && df$b8 == "NA", "NA")
}

然后我会为每一列运行该函数,例如

NAconverter(b1)

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在示例数据中,NA被创建为字符串,因此,您可以尝试

df[rowSums(df=='NA')!=0,] <- NA

如果NAs是真实的,即将数据集创建为

b1 <- c(0,0,1,1,0,0,1,1)
b2 <- c(0,0,1,1,NA,0,1,1)
b3 <- c(0,0,1,1,0,0,1,1)
b4 <- c(0,0,1,1,0,0,1,1)
b5 <- c(0,0,NA,1,0,0,1,1)
b6 <- c(0,0,1,1,0,0,1,1)
b7 <- c(0,0,1,1,0,NA,1,1)
b8 <- c(0,0,1,1,0,0,1,1)
df <- data.frame(b1,b2,b3,b4,b5,b6,b7,b8)

请注意,这也会将列的类从factor更改为numeric

df[!!rowSums(is.na(df)),] <- NA

或者

 df[Reduce(`|`,lapply(df, is.na)),] <- NA
 df
 #   b1 b2 b3 b4 b5 b6 b7 b8
 #1  0  0  0  0  0  0  0  0
 #2  0  0  0  0  0  0  0  0
 #3 NA NA NA NA NA NA NA NA
 #4  1  1  1  1  1  1  1  1
 #5 NA NA NA NA NA NA NA NA
 #6 NA NA NA NA NA NA NA NA
 #7  1  1  1  1  1  1  1  1
 #8  1  1  1  1  1  1  1  1

更新

假设有一些列应该省略,例如

a <- letters[1:8]
df <- data.frame(a, b1,b2,b3,b4,b5,b6,b7,b8)

在这里,我通过省略第一列('a')

来获取索引
df[-1][!!rowSums(is.na(df[-1])),] <- NA