Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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并删除该行_R - Fatal编程技术网

R 如何计算所有NA并删除该行

R 如何计算所有NA并删除该行,r,R,我有这样的数据 df<-structure(list(best2 = c(8972.7, 1944, 2022.7, 13001.7, NA, 3228.6, NA, 186.4, 100, 2655.9), best3 = c(2634.4, 1181.3, 505.2, 2802.4, NA, 1707.6, NA, 186.4, 100, 1219), best4 = c(3079.3, 1512.9, NA, 2804.5, NA, 1597.6, NA, 186.4

我有这样的数据

    df<-structure(list(best2 = c(8972.7, 1944, 2022.7, 13001.7, NA, 3228.6, 
NA, 186.4, 100, 2655.9), best3 = c(2634.4, 1181.3, 505.2, 2802.4, 
NA, 1707.6, NA, 186.4, 100, 1219), best4 = c(3079.3, 1512.9, 
NA, 2804.5, NA, 1597.6, NA, 186.4, 100, 1558.2), best5 = c(8972.7, 
1944, NA, 13001.7, NA, 3228.6, NA, 186.4, 100, 2655.9), best6 = c(2634.4, 
1181.3, NA, 2802.4, NA, 1707.6, NA, 186.4, 100, 1219), best7 = c(3079.3, 
1512.9, NA, 2804.5, NA, 1597.6, NA, 186.4, 100, 1558.2), best8 = c(8972.7, 
1944, NA, 13001.7, NA, 3228.6, NA, 186.4, 100, 2655.9), best9 = c(2634.4, 
1181.3, NA, 2802.4, NA, 1707.6, NA, 186.4, 100, 1219)), .Names = c("best2", 
"best3", "best4", "best5", "best6", "best7", "best8", "best9"
), row.names = c(NA, -10L), class = "data.frame")

df我们可以使用
apply
函数迭代
data.frame的行。然后,我们在逻辑向量上使用
rowSums

rowSums(all_logicals <- apply(df, 1, function(r){
    n <- length(r)
    nc2 <- n / 2
    nc2_plus1 <- nc2 + 1
    c('allNA' = all(is.na(r)),
    'allconstant' = (length(unique(r)) == 1 & !all(is.na(r))),
    'firsthalf' = all(is.na(r[1:nc2])) & !all(is.na(r[nc2_plus1:n])),
    'secondhalf' = all(is.na(r[nc2_plus1:n])) & !all(is.na(r[1:nc2])))
}))

  allNA allconstant   firsthalf  secondhalf 
      2           2           0           1 
编辑-使用结果删除行 我们可以使用
all_logical
查找要删除的行索引:

remove_rows <- unique(unlist(apply(all_logicals, 1, which)))
(df_sub <- df[-remove_rows,])

     best2  best3  best4   best5  best6  best7   best8  best9
1   8972.7 2634.4 3079.3  8972.7 2634.4 3079.3  8972.7 2634.4
2   1944.0 1181.3 1512.9  1944.0 1181.3 1512.9  1944.0 1181.3
4  13001.7 2802.4 2804.5 13001.7 2802.4 2804.5 13001.7 2802.4
6   3228.6 1707.6 1597.6  3228.6 1707.6 1597.6  3228.6 1707.6
10  2655.9 1219.0 1558.2  2655.9 1219.0 1558.2  2655.9 1219.0

remove_行是否可以添加如何删除每一行?例如,首先是allNA,然后是allcontsnt,然后是上半部分,然后是下半部分?@nik我编辑了我的答案,演示了一种方法
remove_rows <- unique(unlist(apply(all_logicals, 1, which)))
(df_sub <- df[-remove_rows,])

     best2  best3  best4   best5  best6  best7   best8  best9
1   8972.7 2634.4 3079.3  8972.7 2634.4 3079.3  8972.7 2634.4
2   1944.0 1181.3 1512.9  1944.0 1181.3 1512.9  1944.0 1181.3
4  13001.7 2802.4 2804.5 13001.7 2802.4 2804.5 13001.7 2802.4
6   3228.6 1707.6 1597.6  3228.6 1707.6 1597.6  3228.6 1707.6
10  2655.9 1219.0 1558.2  2655.9 1219.0 1558.2  2655.9 1219.0