根据一天之内的日期匹配R中的列

问题描述 投票:-1回答:1

我有两个桌子。第一个表包含我的参考测量值:

> test
        date site value product
A1 2017-06-10    A   0.6  meter1
A2 2017-06-10    B   0.5  meter1
A3 2017-06-11    C   0.5  meter1
A4 2017-06-18    A   0.1  meter1
A5 2017-06-19    B   0.6  meter1
A6 2017-06-19    C   0.6  meter1

第二张表包含在不同日期可能不同或不匹配的不同仪器的第二组测量值。

> test2
         date site value product
B1 2017-06-07    A   0.4  meter2
B2 2017-06-09    B   0.5  meter2
B3 2017-06-09    C   0.6  meter3
B4 2017-06-09    A   0.2  meter2
B5 2017-06-20    B   0.7  meter3
B6 2017-06-23    B   0.5  meter2

我想确定在特定时间间隔内(例如1天之内)与第一张表匹配的度量。哪个应该像这样:

>   test3
        date site value product match
1 2017-06-07    A   0.4  meter2    NA
2 2017-06-09    B   0.5  meter2    A2
3 2017-06-09    C   0.6  meter3    NA
4 2017-06-09    A   0.2  meter2    A1
5 2017-06-20    B   0.7  meter3    A5
6 2017-06-23    B   0.5  meter2    NA

最重要的是,我想针对ggplot中的参考测量绘制这些测量中的每一个。

我用lubridate尝试了不同的方法,但无法使其正常工作。任何帮助表示赞赏。


  test <- structure(list(date = structure(c(17327, 17327, 17328, 17335,17336, 17336),
                                  class = "Date"),
                 site = c("A", "B", "C", "A","B", "C"),
                 value = c(0.6, 0.5,0.5, 0.1, 0.6, 0.6),
                 product = c("meter1", "meter1", "meter1", "meter1", "meter1", "meter1"))
            , row.names = c("A1", "A2", "A3", "A4", "A5", "A6"),
            class = "data.frame")



  test2 <- structure(list(date = structure(c(17324, 17326, 17326, 17326,17337, 17340),
                                          class = "Date"),
                         site = c("A", "B", "C", "A","B", "B"),
                         value = c(0.4, 0.5,0.6, 0.2, 0.7, 0.5),
                         product = c("meter2", "meter2", "meter3", "meter2", "meter3", "meter2"))
                    , row.names = c("B1", "B2", "B3", "B4", "B5", "B6"),
                    class = "data.frame")

  test3 <- structure(list(date = structure(c(17324, 17326, 17326, 17326,17337, 17340),
                                           class = "Date"),
                          site = c("A", "B", "C", "A","B", "B"),
                          value = c(0.4, 0.5,0.6, 0.2, 0.7, 0.5),
                          product = c("meter2", "meter2", "meter3", "meter2", "meter3", "meter2"),
                          match = c("NA", "A2", "NA", "A1", "A5", "NA")),
                     row.names = c("1", "2", "3", "4", "5", "6"),
                     class = "data.frame")


r date match lubridate
1个回答
0
投票

您可能想看看这个SO问题,您可能是一个重复的问题:Joining data frames by lubridate date %within% intervals

[在我看来,软件包{fuzzyjoin}或{lubridate}的%within%可能会有所帮助。

这里还有另一个更详细的示例:https://community.rstudio.com/t/tidy-way-to-range-join-tables-on-an-interval-of-dates/7881

最重要的是,我想绘制这些测量的每一个相对于ggplot中的参考测量。

当您以长格式整理数据并在{ggplot}中使用组时,这应该很容易。

© www.soinside.com 2019 - 2024. All rights reserved.