In R how to combine dataframes based on period between two dates?

zodavaan

I have two dataframes. One with dates column with every day.

seq(as.Date("2016/1/1"), as.Date("2016/3/1"), by = "day")

The other with two columns of dates reagent_type.

    2016-01-01 A
2016-02-01 B
2016-02-15 A
2016-03-30 B

I need to combine these two dataframes so I would have a reagent_types column for everyday. 2016-01-01 to 2016-01-31 would be Reagent A. Then 2016-02-01 to 2016-02-14 would have Reagent B. So on so forth.
Thank you very much in advance.

Psidom

You can use findInterval, assuming the first data frame is dateDf and the second one is typeDf:

dateDf$ReagentType <- typeDf$ReagentType[findInterval(dateDf$Date, typeDf$Date)]

Data:

dput(dateDf)
structure(list(Date = structure(c(16801, 16802, 16803, 16804, 
16805, 16806, 16807, 16808, 16809, 16810, 16811, 16812, 16813, 
16814, 16815, 16816, 16817, 16818, 16819, 16820, 16821, 16822, 
16823, 16824, 16825, 16826, 16827, 16828, 16829, 16830, 16831, 
16832, 16833, 16834, 16835, 16836, 16837, 16838, 16839, 16840, 
16841, 16842, 16843, 16844, 16845, 16846, 16847, 16848, 16849, 
16850, 16851, 16852, 16853, 16854, 16855, 16856, 16857, 16858, 
16859, 16860, 16861), class = "Date"), ReagentType = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", 
"B"), class = "factor")), .Names = c("Date", "ReagentType"), row.names = c(NA, 
-61L), class = "data.frame")

dput(typeDf)
structure(list(Date = structure(c(16801, 16832, 16846, 16890), class = "Date"), 
    ReagentType = structure(c(1L, 2L, 1L, 2L), .Label = c("A", 
    "B"), class = "factor")), .Names = c("Date", "ReagentType"
), row.names = c(NA, -4L), class = "data.frame")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I check that a two dates are between a period(interval)?

From Dev

R: Compare dates in two dataframes and isolate rows that match within a certain time period in R

From Dev

Get a Period of Days between two Dates

From Dev

Merging two dataframes based on a date between two other dates without a common column

From Dev

How can I combine two dataframes with different lengths in R?

From Dev

How to combine rows in two dataframes into one dataframe in R?

From Dev

Matching dates between two dataframes in pandas

From Dev

Matching dates between two dataframes in pandas

From Dev

How to format cells between two dates with a colour based on a value in vba?

From Dev

Combine two boxplots from different dataframes in R

From Dev

How to filter rows based on difference in dates between rows in R?

From Dev

R - How to subset a table between two specific dates?

From Dev

how to show an event happened between two dates in R

From Dev

How to list all months between two dates in R

From Dev

R - How to subset a table between two specific dates?

From Dev

How to enumerate dates between two dates in Moment

From Dev

How to generate all dates between two dates

From Dev

How to fill dates between two dates

From Dev

How to fill dates between two dates

From Dev

How to get dates between two dates

From Dev

MYSQL Datediff between two dates based on year

From Dev

Get all dates between range of two dates and based on the selected week

From Dev

R: merge two dataframes based on substring

From Dev

How to merge two dataframes R

From Java

How to combine two lists in R

From Dev

How to combine two plots in R?

From Dev

how to combine two files in R?

From Dev

How to combine two tables in R?

From Dev

Calculate number of days between two dates in r

Related Related

  1. 1

    How can I check that a two dates are between a period(interval)?

  2. 2

    R: Compare dates in two dataframes and isolate rows that match within a certain time period in R

  3. 3

    Get a Period of Days between two Dates

  4. 4

    Merging two dataframes based on a date between two other dates without a common column

  5. 5

    How can I combine two dataframes with different lengths in R?

  6. 6

    How to combine rows in two dataframes into one dataframe in R?

  7. 7

    Matching dates between two dataframes in pandas

  8. 8

    Matching dates between two dataframes in pandas

  9. 9

    How to format cells between two dates with a colour based on a value in vba?

  10. 10

    Combine two boxplots from different dataframes in R

  11. 11

    How to filter rows based on difference in dates between rows in R?

  12. 12

    R - How to subset a table between two specific dates?

  13. 13

    how to show an event happened between two dates in R

  14. 14

    How to list all months between two dates in R

  15. 15

    R - How to subset a table between two specific dates?

  16. 16

    How to enumerate dates between two dates in Moment

  17. 17

    How to generate all dates between two dates

  18. 18

    How to fill dates between two dates

  19. 19

    How to fill dates between two dates

  20. 20

    How to get dates between two dates

  21. 21

    MYSQL Datediff between two dates based on year

  22. 22

    Get all dates between range of two dates and based on the selected week

  23. 23

    R: merge two dataframes based on substring

  24. 24

    How to merge two dataframes R

  25. 25

    How to combine two lists in R

  26. 26

    How to combine two plots in R?

  27. 27

    how to combine two files in R?

  28. 28

    How to combine two tables in R?

  29. 29

    Calculate number of days between two dates in r

HotTag

Archive