如何在lapply
来电中将数据框的列传递给lapply(data[ , grepl( "Measured." , names( data ) ) ], with, (. <= 5 & . >= 1) | . == 4244)
lapply(data[ , grepl( "Measured." , names( data ) ) ], function(x) with((x <= 5 & x >= 1) | x == 4244))
函数?
我尝试了这些并且它们不起作用!
Measured.
我正在尝试查看1
列中的值是否介于5
和4244
之间,还接受data <- structure(list(ID = 1:10, Date = c(2018L, 2018L, 2018L, 2015L,
2018L, 2015L, 2015L, 2014L, 2014L, 2014L), Gender = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"),
Measured.1 = c(1L, 7L, 1L, 6L, 6L, 2L, 5L, 4L, 2L, 6L), Measured.2 = c(9L,
2L, 4L, 5L, 2L, 3L, 6L, 3L, 7L, 7L), Measured.3 = c(9L, 4L,
35L, 3L, 4L, 2L, 2L, 1L, 3L, 4L), Measured.4 = c(12L, 8L,
50L, 7L, 2L, 6L, 2L, 2L, 1L, 2L), Text = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
Test = c(5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L)), .Names = c("ID",
"Date", "Gender", "Measured.1", "Measured.2", "Measured.3", "Measured.4",
"Text", "Test"), class = "data.frame", row.names = c(NA, -10L
))
。
示例数据集:
ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
1 1 2018 M 1 9 9 12 N 5
2 2 2018 M 7 2 4 8 N 5
3 3 2018 M 1 4 35 50 N 5
4 4 2015 M 6 5 3 7 N 5
5 5 2018 M 6 2 4 2 N 5
6 6 2015 M 2 3 2 6 Y 6
7 7 2015 F 5 6 2 2 Y 6
8 8 2014 F 4 3 1 2 Y 6
9 9 2014 F 2 7 3 1 N 6
10 10 2014 F 6 7 4 2 N 6
及其输出:
{{1}}
答案 0 :(得分:1)
除了基础R
,您可以使用dplyr
解决方案:
library(dplyr)
data %>%
filter_at(vars(starts_with("Measured")),
any_vars((. >= 1 & . <= 5) | . == 4244))
这将查找记录,其中Measured
列中至少一个的值介于1和5或4244之间。
如果您想要限制并且所有值都必须在此范围内,您可以将其更改为:
data %>%
filter_at(vars(starts_with("Measured")),
all_vars((. >= 1 & . <= 5) | . == 4244))
<小时/> 前者产生
ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
1 1 2018 M 1 9 9 12 N 5
2 2 2018 M 7 2 4 8 N 5
3 3 2018 M 1 4 35 50 N 5
4 4 2015 M 6 5 3 7 N 5
5 5 2018 M 6 2 4 2 N 5
6 6 2015 M 2 3 2 6 Y 6
7 7 2015 F 5 6 2 2 Y 6
8 8 2014 F 4 3 1 2 Y 6
9 9 2014 F 2 7 3 1 N 6
10 10 2014 F 6 7 4 2 N 6
而后者产生
ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
1 8 2014 F 4 3 1 2 Y 6
<小时/> 使用基础
R
可以更快(但非常不易读)的方式完成相同的操作:
您可以使用基本R
方法和面具apply
:
# set up the cols of interest
colmask <- grepl("^Measured", names(data))
# apply the function rowwise (=1)
rowmask <- apply(data[colmask], 1, function(col) {
any(((col >= 1 & col <= 5) | col == 4244))
})
data[rowmask,]
或者
colmask <- grepl("^Measured", names(data))
rowmask <- apply(data[colmask], 1, function(col) {
all(((col >= 1 & col <= 5) | col == 4244))
})
data[rowmask,]
显然,这会产生相同的结果。
答案 1 :(得分:1)
使用基数R,您可以提取符合以下条件的行:
data[data[,1][data[,4] >= 1 & data[,4] <= 5 & data[,5] >= 1 & data[,5] <= 5 & data[,6] >= 1 & data[,6] <= 5 & data[,7] >= 1 & data[,7] <= 5 | data[,4] == 4244 | data[,5] == 4244 | data[,6] == 4244 | data[,7] == 4244],]
我使用&
创建附加条件(您正在查找测量的行数,测量值2,测量值3和测量值4均为{{1} }和>= 1
)和<= 5
创建替代标准(任何测量值为|
):
,并提供:
4424
它不是最漂亮的代码片段,但是(根据microbenchmark)它的运行速度比Jan的dplyr方法快43倍。