[英]Remove the first N rows from each factor level in an r data.frame
With the dat
below. 使用下面的
dat
。 How can I make a new dataframe subset that includes all values except the first five rows for each IndID? 如何创建包含除每个IndID的前五行之外的所有值的新数据框子集? Said differently I want new data frame with the first 5 rows for each IndID excluded.
换句话说,我想要排除每个IndID的前5行的新数据帧。
set.seed(123)
dat <- data.frame(IndID = rep(c("AAA", "BBB", "CCC", "DDD"), each = 10),
Number = sample(1:100,40))
I have seen a number of SO posts that select data, but I am not sure how to remove as mentioned above. 我已经看过一些选择数据的SO帖子,但我不知道如何删除如上所述。
We can use dplyr
's slice()
functionality: 我们可以使用
dplyr
的slice()
功能:
dat %>%
group_by(IndID) %>%
slice(6:n())
In base R, tapply()
is handy when used on a sequence of row numbers with tail()
. 在基数R中,
tapply()
在使用tail()
的行号序列上使用时非常方便。
idx <- unlist(tapply(1:nrow(dat), dat$IndID, tail, -5))
dat[idx, ]
Note that this will be more efficient with use.names=FALSE
in unlist()
. 请注意,在
unlist()
use.names=FALSE
会更有效。
With data.table , you can do the following with tail()
. 使用data.table ,您可以使用
tail()
执行以下操作。
library(data.table)
setDT(dat)[dat[, tail(.I, -5), by=IndID]$V1]
If the data is sorted and you are guaranteed to have at least n
rows per group... 如果数据已排序,并且保证每组至少有
n
行......
n = 5
w = match(unique(dat$IndID), dat$IndID)
dat[- (rep(w, each = n) + 1:n - 1L), ]
可以使用split
碱R
分裂dat
通过IndID
,除去前5行每个子组的,然后rbind
后它。
do.call(rbind, lapply(split(dat,as.character(dat$IndID)), function(x) x[-(1:5),]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.