R每周平均值

时间:2016-12-02 14:33:41

标签: r csv average dayofweek

我有一组网格海面温度的日常值为34年(12418每日文件x 4248点)并且假装计算每周值。我几乎成功完成了这篇文章https://stackoverflow.com/a/15102394/709777。但是日期和周之间存在一些分歧。我找不到重点,我希望确定我能得到正确的日期来计算每周的平均值。

我使用这段R脚本来读取每日数据并构建一个大数据框,其中包含列中单个点的所有每日值(12418行/天乘4248列/温度)

# Paths
ruta_datos_diarios<-"/home/meteo/PROJECTES/VERSUS/DATA/SST/CSV/"
ruta_files<-"/home/meteo/PROJECTES/VERSUS/SCRIPTS/CLUSTER/FILES/"
ruta_eixida<-"/home/meteo/PROJECTES/VERSUS/OUTPUT/DATA/SEMANAL/"

# List of daily files
files <- list.files(path = ruta_datos_diarios, pattern = "SST-diaria-MED")

output <- matrix(ncol=4248, nrow=length(files))
fechas <- matrix(ncol=1, nrow=length(files))

for (i in 1:length(files)){
  # read data
  datos<-read.csv(paste0(ruta_datos_diarios,files[i],sep=""),header=TRUE,na.strings = "NA")
  datos<-datos[complete.cases(datos),]

  # Extract dates from daily file names
  yyyy<-substr(files[i],16,19)
  mm<-substr(files[i],20,21)
  dd<-substr(files[i],22,23)
  dates[i,]<-paste0(yyyy,"-",mm,"-",dd,sep="")

  output[i,]<-t(datos$sst)
}

datos.df<-as.data.frame(output)

# Build a dataframe with the dates  (day, week and year)
fechas<-as.data.frame(fechas)
fechas$V1<-as.Date(fechas$V1)
fechas$Week <- week(fechas$V1)
fechas$Year <- year(fechas$V1)

# Extract day of the week (Saturday = 6)
fechas$Week_Day <- as.numeric(format(fechas$V1, format='%w'))
# Adjust end-of-week date (first saturday from the original Date)
fechas$End_of_Week <- fechas$V1 + (6 - fechas$Week_Day)

# new dataframe from End_of_Week
fechas.semana<-fechas[!duplicated(fechas$End_of_Week),]
fechas.semana<-as.data.frame(fechas.semana)

colnames(fechas)<-c("Day","Week","Year","Week_Day","End_of_Week")
colnames(fechas.semana)<-c("Day","Week","Year","Week_Day","End_of_Week")

这是我阅读数据和日期的方式。举一个简短的例子,我已经在这个文件temp-sst.csv中保存了数据帧的子集(10个变量中的1000个,包括&#34; Day&#34;,&#34; Week&#34; &#34;年份&#34;&#34; WEEK_DAY&#34;&#34; End_of_Week&#34;。)

sst.dat <- read.csv("temp-dat.csv",header=TRUE)

# Join dates and SST values
sst.dat <- cbind(fechas, sst.dat)

# Build new dates data frame
fechas<-as.data.frame(sst.dat$Day)
colnames(fechas)<-c("Day")
fechas$Day<-as.Date(fechas$Day)
fechas$Week <- week(fechas$Day)
fechas$Year <- year(fechas$Day)
# Extract day of the week (Saturday = 6)
fechas$Week_Day <- as.numeric(format(fechas$Day, format='%w'))
# Adjust end-of-week date (first saturday from the original Date)
fechas$End_of_Week <- fechas$Day + (6 - fechas$Week_Day)

fechas.semana<-fechas[!duplicated(fechas$End_of_Week),]
fechas.semana<-as.data.frame(fechas.semana)

colnames(fechas)<-c("Day","Week","Year","Week_Day","End_of_Week")
colnames(fechas.semana)<-c("Day","Week","Year","Week_Day","End_of_Week")

# Weekly aggregation function from the referred post
media.semanal <- function(x, column){
  a<-aggregate(x[,column]~End_of_Week+Year, FUN=mean, data=x, na.rm=TRUE)
  colnames(a)<-c("End_of_Week","Year","SSTmean")
  return(a)
}

# Matrix to be populated by weekly function
SST.mat<-matrix(nrow=nrow(fechas.semana), ncol=length(sst.dat)-5)  # 5 son las columnas de fecha

for (j in 6:length(sst.dat)){   # comienza en 6 para evitar las columnas de fecha
b<-media.semanal(sst.dat,j)
SST.mat[,j-5]<-b$SSTmean
}

但问题出现了。 &#34; B&#34;循环中的数据帧有145行,而SST.mat和fechas.semana只有144行。我还没有发现这种分歧的来源。

非常感谢任何帮助,我被困在这里。 感谢

1 个答案:

答案 0 :(得分:1)

您在b$End_of_Week的一个值中有重复。

首先我注意到集合成员资格没有区别:

setdiff(as.character(b$End_of_Week),as.character(fechas.semana$End_of_Week))
  

字符(0)

然后我意识到必须是因为重复并确认它是这样的:

table(table(as.character(b$End_of_Week))>1)
143    1 
FALSE  TRUE

查看表格显示欺骗是1983-01-01

似乎根本原因是您按End_of_Week + Year汇总,其中Year是不必要的,因为End_of_Week也包含年份,如果您只汇总End_of_Week你得到144而不是145。

# Weekly aggregation function from the referred post
media.semanal <- function(x, column){
  a<-aggregate(x[,column]~End_of_Week, FUN=mean, data=x, na.rm=TRUE)
  colnames(a)<-c("End_of_Week","SSTmean")
  return(a)
}

# Matrix to be populated by weekly function
SST.mat<-matrix(nrow=nrow(fechas.semana), ncol=length(sst.dat)-5)  # 5 son las columnas de fecha

for (j in 6:length(sst.dat)){   # comienza en 6 para evitar las columnas de fecha
  b<-media.semanal(sst.dat,j)
  SST.mat[,j-5]<-b$SSTmean
}
dim(b)
  

144 2