简体   繁体   English

R:如何根据不同的日期合并两个数据框

[英]R: How to merge two data frame based on different date

I want to merge two dataset based on date and define specific date as different category.我想根据日期合并两个数据集并将特定日期定义为不同的类别。 The first data.frame is the water quality data with the date.Shown as follows (first a few columns of the data).第一个data.frame是带日期的水质数据。如下所示(前几列数据)。

Organization   ID Latitude Longitude       Date Year Month Day       Depth
1        NJHDG   19  40.6475 -74.17350 2010-06-02 2010     6   2     Surface
2        NJHDG   14  40.7919 -74.07837 2010-06-03 2010     6   3 Near Bottom
3        NJHDG    2  40.9212 -74.17550 2010-06-07 2010     6   7    Midwater
4        NJHDG    5  40.8795 -74.12066 2010-06-09 2010     6   9    Midwater
5 31ISC2RS_WQX HR8A  40.9850 -73.90833 2010-06-10 2010     6  10     Surface
6        NJHDG    6  40.8890 -74.08166 2010-06-14 2010     6  14    Midwater

Now, I have another data.frame that is a precipitation data(sample shown as follows).现在,我有另一个 data.frame 是降水数据(示例如下所示)。

    Date   PrecipitationIn
    128 2010-05-08            0.03
    129 2010-05-09            0.00
    130 2010-05-10            0.00
    131 2010-05-11            0.04
    132 2010-05-12            0.33
    133 2010-05-13            0.00

The question is that I want to define the date in the first data frame as "wet day" if the current date, previous one day, previous two day has >=0.2 inches by using the precipitation data.问题是,如果当前日期、前一天、前两天使用降水数据 >=0.2 英寸,我想将第一个数据框中的日期定义为“雨天”。 For example,for the date "2010-06-02" in the first data frame.例如,对于第一个数据框中的日期“2010-06-02”。 If either "2010-5-31" or "2010-6-01" or"2010-6-02"has >=0.2 inches precipitation,I will define "2010-06-02" as "wet day".如果“2010-5-31”或“2010-6-01”或“2010-6-02”的降水量 >=0.2 英寸,我会将“2010-06-02”定义为“雨天”。 else, I will define "dry day".否则,我将定义“干燥的一天”。 I tried to used for loop and if function to define, but I failed.我尝试使用 for 循环和 if 函数来定义,但我失败了。 Anyone has smart ideas to help me solve this problem?任何人都有聪明的想法来帮助我解决这个问题? I really appreciate it.对此,我真的非常感激。

Ok - I'm not 100% sure if I got the question correctly.好的 - 我不是 100% 确定我是否正确回答了这个问题。 Maybe this example gives you an idea how to achieve what you're looking for.也许这个例子让你知道如何实现你正在寻找的东西。

A <- read.table("path/to/your/dataset1", sep = " ", header = TRUE)
B <- read.table("path/to/your/dataset2", sep = " ", header = TRUE)

C <- merge(A, B, by = "Date")

C <- data.frame(C, day.type = NA)

for (i in 1:nrow(C)){
  if (i == 1) {
    if (C$PrecipitationIn[i] >= 0.2) {
      C$day.type[i] <- "wet day"
    } else {
      C$day.type[i] <- "dry day"
    }
  } else if (i == 2) {
    if (C$PrecipitationIn[i] >= 0.2 & C$PrecipitationIn[i-1] >= 0.2) {
      C$day.type[i] <- "wet day"
    } else {
      C$day.type[i] <- "dry day"
    }
  } else if (i > 2) {
    if (C$PrecipitationIn[i] >= 0.2 & C$PrecipitationIn[i-1] >= 0.2 & C$PrecipitationIn[i-2] >= 0.2) {
      C$day.type[i] <- "wet day"
    } else {
      C$day.type[i] <- "dry day"
    }
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM