每第n行的每日信息中的平均列

时间:2016-04-22 16:02:44

标签: r loops csv average read.table

我是R的新手。我每天观察温度和PP 12年(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 
 .   .    .   .    .     .     .

3 个答案:

答案 0 :(得分:1)

我们可以尝试

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

答案 1 :(得分: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 :(得分:0)

它增加了一点复杂性,但你可以将每个月减少到三分之一并获得每三分之一的平均值。例如:

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
...