Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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 在多列上使用any()或all()和is.na()_R - Fatal编程技术网

R 在多列上使用any()或all()和is.na()

R 在多列上使用any()或all()和is.na(),r,R,我想从我的数据集中删除所有NAs的行(即保留带有任何非NAs的行)以获得列列表。如何更新此代码以使x和y作为向量提供?这将使我能够灵活地添加和删除列以供检查 library(dplyr) ds <- tibble( id = c(1:4), x = c(NA, 1, NA, 4), y = c(NA, NA , 3, 4) ) ds %>% rowwise() %>% filter( any( !is.na(x),

我想从我的数据集中删除所有NAs的行(即保留带有任何非NAs的行)以获得列列表。如何更新此代码以使
x
y
作为向量提供?这将使我能够灵活地添加和删除列以供检查

library(dplyr)

ds <- 
  tibble(
  id = c(1:4),
    x = c(NA, 1, NA, 4),
    y = c(NA, NA , 3, 4)
  ) 

ds %>%
  rowwise() %>%
  filter(
    any(
      !is.na(x),
      !is.na(y)
    ) 
  ) %>% 
  ungroup()
库(dplyr)
ds%
行()
滤器(
任何(
!is.na(x),
!is.na(y)
) 
) %>% 
解组()

我试图编写类似于
any(!is.na(c(x,y))
的东西,但我不确定如何为
is.na()
提供多个参数

我们可以使用
filter\u at
任何变量

ds %>% 
   filter_at(vars(x:y), any_vars(!is.na(.)))
# A tibble: 3 x 3
#     id     x     y
#  <int> <dbl> <dbl>
#1     2     1    NA
#2     3    NA     3
#3     4     4     4
ds%>%
过滤(变量(x:y),任何变量(!is.na())
#一个tibble:3x3
#id x y
#    
#1 2 1 NA
#2 3 NA 3
#3     4     4     4