Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 检测数据帧中的缺失(NA或0)_R_Dplyr - Fatal编程技术网

R 检测数据帧中的缺失(NA或0)

R 检测数据帧中的缺失(NA或0),r,dplyr,R,Dplyr,我想在包含其他变量信息的数据框中创建一个新变量 我有一个大数据框。简而言之,我们可以这样说: a <- c(1,0,2,3) b <- c(3,0,1,1) c <- c(2,0,2,2) d <- c(4,1,1,1) (df <- data.frame(a,b,c,d) ) a b c d 1 1 3 2 4 2 0 0 0 1 3 2 1 2 1 4 3 1 2 1 因为我有一个大数据帧,所以我考虑使用df[1:2]和df[3:4],这样我就不需要键

我想在包含其他变量信息的数据框中创建一个新变量

我有一个大数据框。简而言之,我们可以这样说:

a <- c(1,0,2,3)
b <- c(3,0,1,1)
c <- c(2,0,2,2)
d <- c(4,1,1,1)

(df <- data.frame(a,b,c,d) )

  a b c d
1 1 3 2 4
2 0 0 0 1
3 2 1 2 1
4 3 1 2 1

因为我有一个大数据帧,所以我考虑使用df[1:2]和df[3:4],这样我就不需要键入每个变量名。但我不确定哪种方式是最好的实施方式。也许dplyr有一个不错的选择?

我只需要收集某些列。这很好。谢谢。df$x我注意到我的问题更复杂。将进行编辑。
NA^(行和(df)==0)
df$x <- ifelse(rowSums(df), 1, NA)
df$x <- ifelse(rowSums(df), 1, NA)
df$x <- ifelse(rowSums(df[1:2])&rowSums(df[3:4]), 1, NA)
  a b c d  x
1 1 3 2 4  1
2 0 0 0 1 NA
3 2 1 2 1  1
4 3 1 2 1  1