Naren GK Naren GK - 1 year ago 54
R Question

passing a list in between()

I am trying to filter() a dataframe using dplyr and I would like to filter based on a custom range specified by a list/ values in another data frame.

An example of what I am trying to do is follows. Any help will be greatly appreciated.

a<- data.frame(source=c(1,22,36,41,5))
d<- a$source

t<- 0.1
al<- a$source- t
ah<- a$source + t

b= data.frame(source= c(1:50))

#filtering based on a list ( this works)
c<- b %>% filter(source %in% d)

#(filtering based on a fixed range, this works)
c<- b %>% filter(between(source,2,5))

#I am stuck here when I try to pass a series of values in between()
c<- b %>% filter(between(source,al,ah))

Is there another way of doing this? or doing this with the existing code? Is there something I am missing in the dplyr package.

Thank you so much.

Answer Source

I think the problem is that between expects a single value for the boundary conditions.

You could try to do:

c<- b %>% filter(source > al & source < ah)

However, I find this a little weird because the al and ah will be recycled to match the size of b$source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download