【问题标题】:Merge table by aggregating a 3 year average from second df通过聚合第二个 df 的 3 年平均值来合并表
【发布时间】:2014-12-29 15:53:29
【问题描述】:

我正在模拟各种变量对特定拖运中鱼类生长的影响。我想根据地理网格位置计算每次运输的三年平均捕获率。我需要一个公式来获取“Stat_sq”(地理网格正方形)和钓鱼之旅的“年份”(包含在第一个数据框中),并通过将平均捕获率汇总为前三年的平均捕获率来自第二个数据帧的“Stat_sq”和“Year”。

年份范围是93,94,95,96,97,98,99,00,01,02,03,04,05,06,07,08,09,10,11,12,13,14

如果存在 NA/缺失值,我还希望它仅返回两年平均值或仅返回一年值。

Dataframe1
Trip_Id Stat_sq Year
0113A_1   48E8  13
0113A_10  49E8  13
0113A_11  49E8  12
0113A_12  49E8  13
0113A_13  49E8  12
0113A_15  49E8  11
0113A_16  49E8  10
0113A_18  50E8  13
0113A_19  50E8  12

Dataframe2
Stat_sq Year    Catch_Rate
48E8    13      12.353719
48E8    12      16.508482
48E8    11      2.841493  
42E8    10      12.721584
50E8    13      12.419484
50E9    12      22.461538
50E8    11      28.141433
44E7    00      29.681110
44E8    00      5.427430   
45E6    00      13.259770
45E7    00      18.250903
45E8    00      62.222222  

例如,对于 0113A_1 运输,我希望 13、12、11 年(包括当年在内的 3 年)的 48E8 广场的平均捕获率 = 10.567898

任何帮助表示赞赏。

【问题讨论】:

    标签: r


    【解决方案1】:

    这可能有帮助

     df1$Yr <- with(df1, as.numeric(ifelse(as.numeric(Year)>=93,
                                  paste0(19,Year), paste0(20,Year))))
    
     df2$Yr <-  with(df2, as.numeric(ifelse(as.numeric(Year)>=93,
                                  paste0(19,Year), paste0(20,Year))))
    
    
     res <-   unsplit(lapply(split(df1, df1$Stat_sq),
                function(x) {
               x1 <- df2[df2$Stat_sq %in% unique(x$Stat_sq),]
                 x$Avg <- sapply(seq_len(nrow(x)), function(i) {
                       x2 <- x[i,]
                       indx <- x1$Yr %in% seq(x2$Yr-2, x2$Yr)
                       if(length(indx)>0) mean(x1$Catch_Rate[indx], na.rm=TRUE)
                        else NA})
                  x}),
                df1$Stat_sq)
    
     head(res[,-4],2)
     #   Trip_Id Stat_sq Year     Avg
     #1  0113A_1    48E8   13 10.5679
     #2 0113A_10    49E8   13      NA
    

    数据

     df1 <- structure(list(Trip_Id = c("0113A_1", "0113A_10", "0113A_11", 
     "0113A_12", "0113A_13", "0113A_15", "0113A_16", "0113A_18", "0113A_19"
     ), Stat_sq = c("48E8", "49E8", "49E8", "49E8", "49E8", "49E8", 
     "49E8", "50E8", "50E8"), Year = c("13", "13", "12", "13", "12", 
     "11", "10", "13", "12")), .Names = c("Trip_Id", "Stat_sq", "Year"
     ), class = "data.frame", row.names = c(NA, -9L))
    
     df2 <- structure(list(Stat_sq = c("48E8", "48E8", "48E8", "42E8", "50E8", 
     "50E9", "50E8", "44E7", "44E8", "45E6", "45E7", "45E8"), Year = c("13", 
     "12", "11", "10", "13", "12", "11", "00", "00", "00", "00", "00"
     ), Catch_Rate = c(12.353719, 16.508482, 2.841493, 12.721584, 
     12.419484, 22.461538, 28.141433, 29.68111, 5.42743, 13.25977, 
     18.250903, 62.222222)), .Names = c("Stat_sq", "Year", "Catch_Rate"
     ), class = "data.frame", row.names = c(NA, -12L))
    

    【讨论】:

      【解决方案2】:

      这里,这应该会给你想要的结果:

      df1 <- structure(list(Trip_Id = c("0113A_1", "0113A_10", "0113A_11", 
                                        "0113A_12", "0113A_13", "0113A_15", "0113A_16", "0113A_18", "0113A_19"
      ), Stat_sq = c("48E8", "49E8", "49E8", "49E8", "49E8", "49E8", 
                     "49E8", "50E8", "50E8"), Year = c(13, 13, 12, 13, 12, 11, 10, 
                                                       13, 12)), .Names = c("Trip_Id", "Stat_sq", "Year"), class = "data.frame", 
      row.names = c(NA, -9L))
      
      df2 <- structure(list(Stat_sq = c("48E8", "48E8", "48E8", "42E8", "50E8", 
                                        "50E9", "50E8", "44E7", "44E8", "45E6", "45E7", "45E8"), Year = c(13, 
                                                                                                          12, 11, 10, 13, 12, 11, 0, 0, 0, 0, 0), Catch_Rate = c(12.353719, 
                                                                                                                                                                 16.508482, 2.841493, 12.721584, 12.419484, 22.461538, 28.141433, 
                                                                                                                                                                 29.68111, 5.42743, 13.25977, 18.250903, 62.222222)), .Names = c("Stat_sq", 
                                                                                                                                                                                                                                 "Year", "Catch_Rate"), class = "data.frame", row.names = c(NA, -12L))
      
      combined <- merge(df1,df2,all.x=TRUE)
      
      findRate <- function(ggs,year){
      
        # ggs - geographic grid square (stat_sq)
        # year - desired year
      
        filter1 <- combined[combined$Stat_sq==ggs,]
        last3years <- c(year:(year-2))
        filter2 <- filter1[is.element(filter1$Year,last3years),]
        output <- aggregate(Catch_Rate~Year,data=filter2,mean)
        print(output)
      }
      
      findRate("50E8",14)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-13
        • 2023-02-02
        • 1970-01-01
        • 2021-08-12
        • 2020-11-03
        • 2018-01-17
        • 2019-05-05
        • 2020-04-04
        相关资源
        最近更新 更多