【问题标题】:Average column in daily information at every n-th row每日信息中每第 n 行的平均列
【发布时间】:2016-04-22 16:02:43
【问题描述】:

我是 R 的新手。我每天观察 12 年的温度和 PP(6574 行,6col,一些 NA)。例如,我想计算2001年1月1日到10日的平均值,然后是11-20日,最后是21日到31日,依此类推,直到我之前提到的每年12月为止。

但我也有问题,因为二月有时有 28 或 29 天(闰年)。

这就是我打开文件的方式,它是 CSV,带有read.table

# READ CSV
setwd ("C:\\Users\\GVASQUEZ\\Documents\\ESTUDIO_PAMPAS\\R_sheet")

huancavelica<-read.table("huancavelica.csv",header = TRUE, sep = ",",
                         dec = ".", fileEncoding = "latin1", nrows = 6574 )

这是我的 CSV 文件的输出

     Año Mes Dia PT101 TM102 TM103    
1   1998  1   1   6.0  15.6   3.4
2   1998  1   2   8.0  14.4   3.2
3   1998  1   3   8.6  13.8   4.4
4   1998  1   4   5.6  14.6   4.6
5   1998  1   5   0.4  17.4   3.6
6   1998  1   6   3.4  17.4   4.4
7   1998  1   7   9.2  14.6   3.2
8   1998  1   8   2.2  16.8   2.8
9   1998  1   9   8.6  18.4   4.4
10  1998  1  10   6.2  15.0   3.6 
 .   .    .   .    .     .     .

【问题讨论】:

  • 欢迎来到 stackOverflow。如果您将代码作为文本而不是图像,人们会欣赏它,而不是发布您的代码图片。这使得检查变得更加容易。
  • 谢谢你的建议我会做@lmo
  • 我想一个简单的方法是创建一个新列,1 表示第 1 到 10 天,然后 2 表示第 11 到 20 天,3 表示 > 20。调用列 x,然后尝试类似aggregate(TM102 ~ Mes + x, data = huancavelica, mean)。可能有更好的方法,但这种方法很简单。另请参阅?aggregatethis one 之类的问题。
  • 谢谢@Laterow 我会看到“聚合”来练习。

标签: r loops csv average read.table


【解决方案1】:

通过数据设置,您有一个经过充分尝试且真实的方法应该可以工作:

# add 0 in front of single digit month variable to account for 1 and 10 sorting
huancavelica$MesChar <- ifelse(nchar(huancavelica$Mes)==1, 
                    paste0("0",huancavelica$Mes), as.character(huancavelica$Mes))

# get time of month ID
huancavelica$timeMonth <- ifelse(huancavelica$Dia < 11, 1,   
                          ifelse(huancavelica$Dia > 20, 3, 2)
# get final ID
huancavelica$ID <- paste(huancavelica$Año, huancavelica$MesChar, huancavelica$timeMonth, sep=".")
# average stat
huancavelica$myStat <- ave(huancavelica$PT101, huancavelica$ID, FUN=mean, na.rm=T)

【讨论】:

  • 非常感谢。这是工作。最后一个问题,如果我想要总和而不是平均值,我可以使用另一个具有“对因素的水平组合进行分组总和”的函数。
【解决方案2】:

我们可以试试

library(data.table)
setDT(df1)[, Grp := (Dia - 1)%/%10+1, by = .(Ano, Mes)
       ][Grp>3, Grp := 3][,lapply(.SD, mean, na.rm=TRUE), by = .(Ano, Mes, Grp)]

【讨论】:

    【解决方案3】:

    它增加了一点复杂性,但您可以将每个月分成三分之一,然后得到三分之一的平均值。例如:

    library(dplyr)
    library(lubridate)
    
    # Fake data
    set.seed(10)
    df = data.frame(date=seq(as.Date("2015-01-01"), as.Date("2015-12-31"), by="1 day"), 
                    value=rnorm(365))
    
    # Cut months into thirds
    df = df %>% 
      mutate(mon_yr = paste0(month(date, label=TRUE, abbr=TRUE) , " ", year(date))) %>%
      group_by(mon_yr) %>%
      mutate(cutMonth = cut(day(date), 
                            breaks=c(0, round(1/3*n()), round(2/3*n()), n()),
                            labels=c("1st third","2nd third","3rd third")),
             cutMonth = paste0(mon_yr, ", ", cutMonth)) %>%
      ungroup %>%
      mutate(cutMonth = factor(cutMonth, levels=unique(cutMonth)))
    
              date       value            cutMonth
      1 2015-01-01  0.01874617 Jan 2015, 1st third
      2 2015-01-02 -0.18425254 Jan 2015, 1st third
      3 2015-01-03 -1.37133055 Jan 2015, 1st third
    ...
    363 2015-12-29  -1.3996571 Dec 2015, 3rd third
    364 2015-12-30  -1.2877952 Dec 2015, 3rd third
    365 2015-12-31  -0.9684155 Dec 2015, 3rd third
    
    # Summarise to get average value for each 1/3 of a month  
    df.summary = df %>%  
      group_by(cutMonth) %>%
      summarise(average.value = mean(value))
    
                  cutMonth average.value
    1  Jan 2015, 1st third   -0.49065685
    2  Jan 2015, 2nd third    0.28178222
    3  Jan 2015, 3rd third   -1.03870698
    4  Feb 2015, 1st third   -0.45700203
    5  Feb 2015, 2nd third   -0.07577199
    6  Feb 2015, 3rd third    0.33860882
    7  Mar 2015, 1st third    0.12067388
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多