子集行的列值大于阈值 [英] Subsetting Rows with a Column Value Greater than a Threshold
本文介绍了子集行的列值大于阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含70列的数据集。
I have a dataset with 70 columns.
我想对数据集的所有行进行子集化,其中第5列到第70列中的值都大于值7。
I would like to subset entire rows of the dataset where a value in any column 5 through 70 is greater than the value 7.
我尝试了以下代码,但是,我不需要TRUE / FALSE值。我只希望那些不符合从数据框中删除的条件的行
I have tried the following code, however, I do not want TRUE/FALSE values. I would just like the rows that do not meet the criteria eliminated from the data frame
subset <- (data[, 5:70] > 7)
推荐答案
我们可以使用 rowSums
data[rowSums(data[5:70] > 7) > 0, ]
或具有子集
subset(data, rowSums(data[5:70] > 7) > 0)
我们也可以使用 filter_at
来自 dplyr
和 any_vars
library(dplyr)
data %>% filter_at(vars(5:70), any_vars(. > 7))
使用来自 mtcars
的可复制数据(@Maurits Evers的窃取想法)
Using reproducible data from mtcars
(stealing idea from @Maurits Evers)
mtcars[rowSums(mtcars[3:11] > 300) > 0, ]
# mpg cyl disp hp drat wt qsec vs am gear carb
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
#Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
#Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
#Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
#Dodge Challenger 15.5 8 318 150 2.76 3.520 16.87 0 0 3 2
#AMC Javelin 15.2 8 304 150 3.15 3.435 17.30 0 0 3 2
#Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
#Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
#Ford Pantera L 15.8 8 351 264 4.22 3.170 14.50 0 1 5 4
#Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
使用 filter_at
al因此给出相同的输出
Using filter_at
also gives the same output
mtcars %>% filter_at(vars(3:11), any_vars(. > 300))
这篇关于子集行的列值大于阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文