返回包含至少一个满足条件的元素的行 - 使用dplyr :: filter迭代多个列 - 过滤并应用

时间:2018-04-25 11:52:47

标签: r filter dplyr subset lapply

我有这个数据集:

dat <- structure(list(Date.1 = c(NA, NA, NA, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7), Date.2 = c(NA, NA, NA, 7, 7, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6), Date.3 = c(NA, NA, NA, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 
8, 8), Date.4 = c(NA, NA, NA, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7), Date.5 = c(NA, NA, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
), Date.6 = c(NA, NA, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), 
    Date.7 = c(NA, NA, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
    ), Date.8 = c(NA, NA, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
    7), Date.9 = c(NA, NA, NA, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 
    6, 6), Date.10 = c(NA, NA, NA, 6, 6, 8, 8, 8, 11, 8, 8, 8, 
    8, 8, 8), Date.11 = c(NA, NA, NA, 8, 8, NA, NA, NA, NA, NA, 
    7, 7, 7, 21, 7), Date.12 = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), Date.13 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), Date.14 = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), Date.15 = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    )), .Names = c("Date.1", "Date.2", "Date.3", "Date.4", "Date.5", 
"Date.6", "Date.7", "Date.8", "Date.9", "Date.10", "Date.11", 
"Date.12", "Date.13", "Date.14", "Date.15"), row.names = c(NA, 
15L), class = "data.frame")

查看:

> dat
   Date.1 Date.2 Date.3 Date.4 Date.5 Date.6 Date.7 Date.8 Date.9 Date.10 Date.11 Date.12 Date.13 Date.14 Date.15
1      NA     NA     NA     NA     NA     NA     NA     NA     NA      NA      NA      NA      NA      NA      NA
2      NA     NA     NA     NA     NA     NA     NA     NA     NA      NA      NA      NA      NA      NA      NA
3      NA     NA     NA     NA     NA     NA     NA     NA     NA      NA      NA      NA      NA      NA      NA
4       7      7      6      8      7      7      7      7      7       6       8      NA      NA      NA      NA
5       7      7      6      8      7      7      7      7      7       6       8      NA      NA      NA      NA
6       7      6      8      7      7      7      7      7      6       8      NA      NA      NA      NA      NA
7       7      6      8      7      7      7      7      7      6       8      NA      NA      NA      NA      NA
8       7      6      8      7      7      7      7      7      6       8      NA      NA      NA      NA      NA
9       7      6      8      7      7      7      7      7      6      11      NA      NA      NA      NA      NA
10      7      6      8      7      7      7      7      7      6       8      NA      NA      NA      NA      NA
11      7      6      8      7      7      7      7      7      6       8       7      NA      NA      NA      NA
12      7      6      8      7      7      7      7      7      6       8       7      NA      NA      NA      NA
13      7      6      8      7      7      7      7      7      6       8       7      NA      NA      NA      NA
14      7      6      8      7      7      7      7      7      6       8      21      NA      NA      NA      NA
15      7      6      8      7      7      7      7      7      6       8       7      NA      NA      NA      NA

我想以行方式检查数据,并且只提取包含至少一个大于8 的单元格的行。

因此,在上面的示例中,应报告行914,因为它们都包含一个数字大于8的单元格。

我尝试了很多许多变体而且都没有工作..只是列出其中一些:

dat %>% rowwise %>% dplyr::filter(. > 8)
dat %>% lapply(dplyr::filter, . > 8)
dat %>% dplyr::filter_at(vars(starts_with("Date)), any_vars(. > 8))
dat %>% lapply(function(x) dplyr::filter(x > 8))

如果我理解this postdplyr似乎完全不可能。

手动完成所有操作都是这样的:

test <- dat
test %>% filter(Date.1 > 8 )
test %>% filter(Date.2 > 8 )
test %>% filter(Date.3 > 8 )
test %>% filter(Date.4 > 8 )
test %>% filter(Date.5 > 8 )
test %>% filter(Date.6 > 8 )
test %>% filter(Date.7 > 8 )
test %>% filter(Date.8 > 8 )
test %>% filter(Date.9 > 8 )
test %>% filter(Date.10 > 8 )
test %>% filter(Date.11 > 8 )
test %>% filter(Date.12 > 8 )
test %>% filter(Date.13 > 8 )
test %>% filter(Date.14 > 8 )
test %>% filter(Date.15 > 8 )

有没有办法在base Rdplyr或任何其他package中有效地执行此操作?

2 个答案:

答案 0 :(得分:1)

您可以使用dplyr

执行此操作
library(dplyr) 
filter_all(dat, any_vars(. > 8))

干杯!

答案 1 :(得分:0)

您还可以使用apply基地的R功能:

dat[apply(dat, 1, function(x) any(x>8, na.rm = TRUE)),]