Home > Blockchain >  dplyr::filter criteria from other data.frame
dplyr::filter criteria from other data.frame

Time:07-26

I want to filter a df1 with criteria of df2 and count the rows of df1 which fullfill the criteria of df2.

df1<-data.frame(A=c(1,2,3,4),
                B=c(1,2,3,4))
df2<-data.frame(A_min=c(2,3),
                A_max=c(3,3))

df2<-df2%>%
  mutate(Score=df1%>%
           filter(between(df1$A,df2$A_min,df2$A_max))%>%
           norw())

I get this error:

Error in mutate(): ! Problem while computing Score = df1 %>% filter(between(df1$A, df2$A_min, df2$A_max)). Caused by error in filter(): ! Problem while computing ..1 = between(df1$A, df2$A_min, df2$A_max)`. Caused by error in between(): ! left must be length 1

I can understand the error message, but I cannot find a way of handling it. Outcome shoud look like:

  A_min A_max Score
1     2     3     2
2     3     3     1

CodePudding user response:

You could use map2 from purrr:

library(dplyr)
library(purrr)

df2 |>
  mutate(Score = map2(A_min, A_max, ~ nrow(filter(df1, between(A, .x, .y)))))

Or rowwise():

df2 |>
  rowwise() |>
  mutate(Score = nrow(filter(df1, between(A, A_min, A_max)))) |>
  ungroup()

Output:

  A_min A_max Score
1     2     3     2
2     3     3     1

CodePudding user response:

Your error is separate from anything to do with the second data frame:

df1<-data.frame(A=c(1,2,3,4),
                B=c(1,2,3,4))
df1 %>% filter(between(df1$A,df2$A_min,df2$A_max))
Error in `filter()`:
! Problem while computing `..1 = between(df1$A,
  df2$A_min, df2$A_max)`.
Caused by error in `between()`:
! `left` must be length 1

You can't pass vectors over length 1 to the left and right values for between. This is what is causing your error. Try instead:

df1 %>% filter(df1$A >= df2$A_max & df2$A_min <= df2$A_max)
  A B
1 3 3
2 4 4
  • Related