I have two data frame and I want to subset specific rows in df2. Here are df1 and df2:
df1:
Sdate columnA D
2020-05-14 DD 1
2020-05-14 FF 5
2020-05-14 EE 6
2020-05-14 GG 7
df2:
Sdate ColA C
2020-04-13 NN 1
2020-04-13 XX 1
2020-04-14 VV 5
2020-04-15 DD 6
2020-04-16 AA 7
Here are the steps to get my final output:
- I need to calculate date differences between df1's [1,1] which is "2020-05-14" and df2's [1,1] which is "2020-04-13"
- I need to figure out if the difference is larger than 10 days.
- Finally, if it is larger than 10 days, I want to delete rows having oldest dates in df2. Because 2020-04-13 is the oldest date in df2, I want to delete first two lows of df2.
"2020-05-14" - "2020-04-13" is 31. Therefore, my final output of df2 should be
Sdate ColA C
2020-04-14 VV 5
2020-04-15 DD 6
2020-04-16 AA 7
I tried with the codes following:
df2 <- ifelse(as.numeric(as.Date(as.character(df1[1,1]), format="%Y-%m-%d")-
as.Date(as.character(df2[1,1]), format="%Y-%m-%d"))>10,
subset(df2, Sdate!= df2[1,1]),print("Pass"))
I tested this code separately in three pieces, and they worked well. But it doesn't in combined code above. df2 is just gone with the code.
What should I change to get what I want to have?