我想从数据帧(A)中过滤(子集)一些观察结果。但是,我必须对df中的观察结果进行分组,这与group_by相同。这使我总共有6660个群组。现在,我想将那些在每个组中满足一定范围的观察子集。因此,我创建了一个数据框(B),其中包含上下边界条件。与各组一样,此数据框包含6660个观测值。
NewDataFrame <-filter(A %>% group_by(a,b,c),which(d >= B$x && d <= B$y ))
A是原始df, B保持上下边界条件
除了我使用固定值而不是B $ x和B $ y之外,我尝试过的代码与其他功能的变化也不起作用。否则,我通常会得到以下结果:
“较长的对象长度不是较短的对象长度的倍数”
不幸的是,我在有关该主题的旧问题中一无所获。感谢您的帮助!
在这里,我尝试创建一些测试数据。我没有创建A $ b和A $ c,因为它们只是分组条件...
a<-c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C")
d<-rep(1:6,4)
A<-data.frame(a,d)
x<-c(3,5,1)
y<-c(6,6,3)
B<-data.frame(x,y)
a_new<-c("A","A","A","A","B","B","C","C","C")
d_new<-c(3,4,5,6,5,6,1,2,3)
NewDataFrame<-data.frame(a_new,d_new)