计算r中气象站数据的平均值

计算r中气象站数据的平均值,r,R,我正在使用大约800个气象站的数据集,其中包括1986年至2014年每个气象站的每月气温值。数据分为三列:(1)站点名称,(2)日期(年和月)和(3)温度。一般来说,数据如下所示: STATION DATE TEMP Station 1 198601 -15 Station 1 198602 -16 Station 1 201401 -10 Station 1 201402 -14 Station 2 198601 -11 Station 2 198602 -

我正在使用大约800个气象站的数据集,其中包括1986年至2014年每个气象站的每月气温值。数据分为三列:(1)站点名称,(2)日期(年和月)和(3)温度。一般来说,数据如下所示:

STATION    DATE    TEMP
Station 1  198601  -15
Station 1  198602  -16
Station 1  201401  -10
Station 1  201402  -14
Station 2  198601  -11
Station 2  198602  -9
Station 2  201401  -5
Station 2  201402  -4
我需要提取各个气象站在不同年份范围内给定月份的平均温度。例如,如果我需要知道1986-1990年间每个气象站7月的平均温度。我的理想输出是一个新的列表或数据框,根据我指定的日期范围给出每个站点的平均温度


我确信这可以通过使用for循环来完成,但我并不擅长创建这样的代码。如有任何建议,我们将不胜感激。

诸如此类

> df$month <- substr(df$DATE, 5, 6)
> result <- aggregate(TEMP~STATION+month, mean, data=df)
> data.frame(Year=unique(substr(df$DATE, 1, 4)), result)
  Year  STATION month  TEMP
1 1986 Station1    01 -12.5
2 2014 Station2    01  -8.0
3 1986 Station1    02 -15.0
4 2014 Station2    02  -6.5
df$month结果data.frame(年=唯一(substr(df$DATE,1,4)),结果) 年站月温度 1 1986车站1 01-12.5 2 2014站点2 01-8.0 3 1986站1 02-15.0 4 2014站点2 02-6.5
像这样的东西

> df$month <- substr(df$DATE, 5, 6)
> result <- aggregate(TEMP~STATION+month, mean, data=df)
> data.frame(Year=unique(substr(df$DATE, 1, 4)), result)
  Year  STATION month  TEMP
1 1986 Station1    01 -12.5
2 2014 Station2    01  -8.0
3 1986 Station1    02 -15.0
4 2014 Station2    02  -6.5
df$month结果data.frame(年=唯一(substr(df$DATE,1,4)),结果) 年站月温度 1 1986车站1 01-12.5 2 2014站点2 01-8.0 3 1986站1 02-15.0 4 2014站点2 02-6.5 或者

library(data.table)
setDT(df)[, list(MeanTemp = mean(TEMP)), 
                by = list(STATION, Mon = substr(DATE, 5, 6))]

#      STATION Mon MeanTemp
# 1: Station 1  01    -12.5
# 2: Station 1  02    -15.0
# 3: Station 2  01     -8.0
# 4: Station 2  02     -6.5
或许

library(data.table)
setDT(df)[, list(MeanTemp = mean(TEMP)), 
                by = list(STATION, Mon = substr(DATE, 5, 6))]

#      STATION Mon MeanTemp
# 1: Station 1  01    -12.5
# 2: Station 1  02    -15.0
# 3: Station 2  01     -8.0
# 4: Station 2  02     -6.5

使用dplyr代替数据表

weather <- data.frame(station = c("Station 1", "Station 1", "Station 1", "Station 1",
                              "Station 2", "Station 2", "Station 2", "Station 2"),
                  date = c(198601, 198602, 201401, 201402, 198601, 198602, 201401, 201402),
                  temp = c(-15, -16, -10, -14, -11, -9, -5, -4))


library(dplyr)
library(stringr)
# get month and year columns in data
weather <- mutate(weather,
              year = str_extract(date, "\\d{4}"),
              month = str_extract(date, "\\d{2}$"))

# get the mean for each station for each month
mean_station <- group_by(weather, station, month) %>%
  summarise(mean_temp = mean(temp, na.rm = T))

weather使用dplyr代替数据表

weather <- data.frame(station = c("Station 1", "Station 1", "Station 1", "Station 1",
                              "Station 2", "Station 2", "Station 2", "Station 2"),
                  date = c(198601, 198602, 201401, 201402, 198601, 198602, 201401, 201402),
                  temp = c(-15, -16, -10, -14, -11, -9, -5, -4))


library(dplyr)
library(stringr)
# get month and year columns in data
weather <- mutate(weather,
              year = str_extract(date, "\\d{4}"),
              month = str_extract(date, "\\d{2}$"))

# get the mean for each station for each month
mean_station <- group_by(weather, station, month) %>%
  summarise(mean_temp = mean(temp, na.rm = T))

weather我也在学习R,可能不会直接回答您的问题,但我想说的是seas软件包有助于分析此类数据

比如说

require(seas)
pdf( paste("test",".pdf", sep="") )  
for (i in 1: length(STATION)){
d1 <-mksub(mdata,id=STATION[i]) # making a subset for each station based on name/unique id
dat.ss <- seas.sum(d1)
plot(dat.ss)  
}    
graphics off ()
require(seas)
pdf(粘贴(“测试“,.pdf”,sep=”“))
用于(i/1:长度(站)){

d1我也在学习R,可能不会直接回答您的问题,但我想说的是seas软件包有助于分析此类数据

比如说

require(seas)
pdf( paste("test",".pdf", sep="") )  
for (i in 1: length(STATION)){
d1 <-mksub(mdata,id=STATION[i]) # making a subset for each station based on name/unique id
dat.ss <- seas.sum(d1)
plot(dat.ss)  
}    
graphics off ()
require(seas)
pdf(粘贴(“测试“,.pdf”,sep=”“))
用于(i/1:长度(站)){
d1