简体   繁体   English

我想在 dataframe 中迭代,在另一个 dataframe 中添加值(新列)

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

I have a dataframe with dates in one row: ej:我有一个 dataframe,日期在一行中: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:另一个 Dataframe 包含天数和环境变量列表: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.我需要一个代码来根据当天的比赛将环境变量添加到第一个 dataframe。 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.因此,如果我需要品种 X 从开始日期到结束日期的平均湿度,它会转到第二个 dataframe 并遍历日期,对它们进行处理并在第一个 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一种使用R的方法,通过检查df2的日期是否在df1的给定开始和结束日期范围内,将mean_humidity添加到第一个数据框

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 dput() 数据

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")

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

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