Filter with 2 variable conditions

Asked

Viewed 297 times

-2

I have two databases, the first one has about 30,000 lines the second 571.

I need to filter the first with two conditions from the second bank.

Condition A: fctr be ==

Condition B: date <=

I tried to make it a double but it didn’t work.

Example:

df1 = c(col1(a,a,a,b,b,b,b,c,c),col2("10/02", "15/02", "14/03", "05/03", "07/03", "15/03", "20/03", "12/03", "15/03"))

df2 = c(col1(a,b,c), col2("15/02", "15/03", "15/03"))

I need something like:

filter(df1, col1 == [i]df2.col1 & col2 <= [i]df2.col2) 

Does anyone have any idea how I can do that?

  • 1

    You could provide a playable example? LINK But, a priori, I think your df should have the same amount of lines to carry out the action the way you envisioned.

  • Are you trying to create several tables or just one? Try to clarify the goal, please. And take a look at those that end with dlpyr _Join that might help you compare the two tables.

  • Rafael, the code you posted is not reproducible. Check.

1 answer

0


The data of the question are in a language (?) that is not the R nor another that I know, but I believe that with some effort one understands what is intended.

  1. The values of col1 must be equal in both tables.
  2. The values of col2 of the first cannot be greater than those of the second.

To obtain equal values of col1 I’m gonna make a merge by that column and then obtain a logical index from the comparison of col2. But these columns appear to be dates, so a preliminary step will be to turn them into dates with as.Date.

tmp <- merge(df1, df2, by = "col1")
i <- apply(tmp[-1], 1, function(x){
  x <- as.Date(paste(x, "2000", sep = "/"), "%d/%m/%Y")
  x[1] <= x[2]
})
rm(tmp)

df1[i, ]
#  col1  col2
#1    a 10/02
#2    a 15/02
#4    b 05/03
#5    b 07/03
#6    b 15/03
#8    c 12/03
#9    c 15/03

Dice.

df1 = data.frame(col1 = c("a","a","a","b","b","b","b","c","c"),
                 col2 = c("10/02", "15/02", "14/03", "05/03", "07/03", "15/03", "20/03", "12/03", "15/03"))

df2 = data.frame(col1 = c("a","b","c"), 
                 col2 = c("15/02", "15/03", "15/03"))
  • Thank you very much! I had already taken the solution in the stack in English. Your solution works also, is even more elegant. Great Abç!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.