过滤具有特定范围的data.frame的特定列

时间:2015-09-15 08:09:45

标签: r dplyr

我想使用filter()选择data.frame的行。选择行的条件是五个变量中的至少一个值应该在一个区间中。我不知道如何应用这样的条件。

我检查了类似的问题并尝试过但没有运气! 例如 Filter each column of a data.frame based on a specific value

这是一个可重复的例子:

  xx <- rep(rep(seq(0,800,200),each=10),times=2)
  yy<-replicate(5,c(replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-2,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-3,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-4,0), decreasing=TRUE))))

  V <- rep(seq(100,2500,length.out=10),times=2)
  No <- rep(1:10,each=10)
  df <- data.frame(V,xx,yy,No)

我想过滤X1:X5列,以便在X1到X5中的任何值都在(0.5; 0.55)区间内时选择该行。

library(dplyr)

f_1 <- df%>%
filter(X1:X5>=0.5&X1:X5<=0.55)

我收到了错误

    Warning messages:
1: In c(0.867315118241628, 0.720280300480341, 0.673805202395872, 0.489167242541468,  :
  numerical expression has 100 elements: only the first used
2: In c(0.867315118241628, 0.720280300480341, 0.673805202395872, 0.489167242541468,  :
  numerical expression has 100 elements: only the first used
3: In c(0.867315118241628, 0.720280300480341, 0.673805202395872, 0.489167242541468,  :
  numerical expression has 100 elements: only the first used
4: In c(0.867315118241628, 0.720280300480341, 0.673805202395872, 0.489167242541468,  :
  numerical expression has 100 elements: only the first used

1 个答案:

答案 0 :(得分:1)

您可以调整this answer中提供的解决方案。它查找其中至少有一个值响应条件的行(因为逻辑向量可以求和)。

filter(df,rowSums(.[,names(.) %in% paste0("X",1:5)] >= 0.50 & .[,names(.) %in% paste0("X",1:5)] <= 0.55) > 0)