根据列中的值范围删除行 [英] Delete rows based on range of values in column
本文介绍了根据列中的值范围删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果列中的值(在本例中为参与者标识号)在某个范围内,例如,我想删除 dataframe 中的数据行61701 至 61721 &61901 至 61929.
I would like to delete rows of data in a dataframe if the values in a column (in this case a participant identification number) fall within a certain range e.g. 61701 to 61721 & 61901 to 61929.
我知道如何根据阈值对数据进行子集化,例如:
I know how to subset data based on a threshold e.g.:
datasetnew = dataset[dataset$X<=100, ]
但不确定如何使用一系列数字对行进行子集化和删除.不确定子集是我需要的.
But not sure how to subset and remove the rows using a range of numbers. Not sure subset is what I need.
推荐答案
或者更直接的实现将只是使用 !
Or a more straight forward implementation will be just negate these rows using !
dataset[with(dataset, !((X >= 61701 & X <= 61721) | (X >= 61901 & X <= 61929))), ]
或
dataset[with(dataset, !((X %in% 61701:61721) | (X %in% 61901:61929))), ]
对于大数据集,您可以使用data.table
s %between%
函数
For a big data set you can use data.table
s %between%
function
library(data.table)
setDT(dataset)[!(X %between% c(61701, 61721) | X %between% c(61901, 61929))]
这篇关于根据列中的值范围删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文