简体   繁体   中英

I want to iterate in dataframe adding value( new column) in another dataframe

I have a dataframe with dates in one row: ej:

CULTIVAR/ START DATE / END DATE

x / 11.02.2020  / 11.02.2021

And Another Dataframe with a list of days and ambiental variables: ej:

 Date                Humidity

01.01.2020 /   80

02.01.2020 /   85

03.01.2020 /   90

I need a code to add the Ambiental Variables to the first dataframe acording to the match in the days. So if i need the mean Humidity from the start date to the end date for the cultivar X, it goes to the second dataframe and iterates over the dates, maches them and add the mean humidity in the first dataframe.

An approach with R that adds mean_humidity to the first data frame by checking if the dates from df2 fall within the given start and end date from df1

Example data

df1
  cul      start        end
1   x 2020-02-11 2020-03-22
2   y 2020-03-11 2020-03-12
3   z 2020-04-11 2020-04-11
4   o 2020-05-11 2020-05-11

df2
        date humidity
1 2020-02-11       34
2 2020-03-11       45
3 2020-03-12       26
4 2020-03-13       65
5 2020-04-11       67
6 2020-05-15       78

Use

df1$mean_humidity <- rowMeans(
  sapply(seq_along(df2$date), function(x) 
    ifelse(df2$date[x] >= df1$start & 
           df2$date[x] <= df1$end, 
           df2$humidity[x], NA)
  ), na.rm=T)

df1
  cul      start        end mean_humidity
1   x 2020-02-11 2020-03-22          42.5
2   y 2020-03-11 2020-03-12          35.5
3   z 2020-04-11 2020-04-11          67.0
4   o 2020-05-11 2020-05-11           NaN

dput() data

df1 <- structure(list(cul = c("x", "y", "z", "o"), start = structure(c(18303, 
18332, 18363, 18393), class = "Date"), end = structure(c(18343, 
18333, 18363, 18393), class = "Date")), class = "data.frame", row.names = c(NA, 
-4L))

df2 <- structure(list(date = structure(c(18303, 18332, 18333, 18334, 
18363, 18397), class = "Date"), humidity = c(34, 45, 26, 65, 
67, 78)), row.names = c(NA, -6L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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