我正在尝试将长格式风数据转换为宽格式。风速和风向都列在 Parameter.Name 列中。这些值需要由 Local.Site.Name 和 Date.Local 变量转换。
如果每个唯一的 Local.Site.Name + Date.Local 行有多个观察值,那么我想要这些观察值的平均值。内置参数“fun.aggregate = mean”适用于风速,但无法以这种方式计算平均风向,因为值以度为单位。例如,靠近北 (350, 10) 的两个风向的平均值将输出为南 (180)。例如:((350 + 10)/2 = 180),尽管极坐标平均值为 360 或 0。
“圆形”包将允许我们计算平均风向而无需执行任何三角函数,但我在尝试将这个附加函数嵌套在“fun.aggregate”参数中时遇到了麻烦。我认为一个简单的 else if 语句可以解决问题,但我遇到了以下错误:
Error in vaggregate(.value = value, .group = overall, .fun = fun.aggregate, : could not find function ".fun"
In addition: Warning messages:
1: In if (wind$Parameter.Name == "Wind Direction - Resultant") { :
the condition has length > 1 and only the first element will be used
2: In if (wind$Parameter.Name == "Wind Speed - Resultant") { :
the condition has length > 1 and only the first element will be used
3: In mean.default(wind$"Wind Speed - Resultant") :
argument is not numeric or logical: returning NA
目标是能够使用fun.aggregate = mean
风速,但mean(circular(Wind Direction, units = 'degrees')
风向。
这是原始数据(>100MB): https ://drive.google.com/open?id=0By6o_bZ8CGwuUUhGdk9ONTgtT0E
这是数据的子集(第 100 行): https ://drive.google.com/open?id=0By6o_bZ8CGwucVZGT0pBQlFzT2M
这是我的脚本:
library(reshape2)
library(dplyr)
library(circular)
#read in the long format data:
wind <- read.csv("<INSERT_FILE_PATH_HERE>", header = TRUE)
#cast into wide format:
wind.w <- dcast(wind,
Local.Site.Name + Date.Local ~ Parameter.Name,
value.var = "Arithmetic.Mean",
fun.aggregate = (
if (wind$Parameter.Name == "Wind Direction - Resultant") {
mean(circular(wind$"Wind Direction - Resultant", units = 'degrees'))
}
else if (wind$Parameter.Name == "Wind Speed - Resultant") {
mean(wind$"Wind Speed - Resultant")
}),
na.rm = TRUE)
任何帮助将不胜感激!
-spacedSparking
编辑:这是解决方案:
library(reshape2)
library(SDMTools)
library(dplyr)
#read in the EPA wind data:
#This data is publicly accessible, and can be found here: https://aqsdr1.epa.gov/aqsweb/aqstmp/airdata/download_files.html
wind <- read.csv("daily_WIND_2016.csv", sep = ',', header = TRUE, stringsAsFactors = FALSE)
#convert long format wind speed data by date and site id:
wind_speed <- dcast(wind,
Local.Site.Name + Date.Local ~ Parameter.Name,
value.var = "Arithmetic.Mean",
fun.aggregate = function(x) {
mean(x, na.rm=TRUE)
},
subset = .(Parameter.Name == "Wind Speed - Resultant")
)
#convert long format wind direction data into wide format by date and local site id:
wind_direction <- dcast(wind,
Local.Site.Name + Date.Local ~ Parameter.Name,
value.var = "Arithmetic.Mean",
fun.aggregate = function(x) {
if(length(x) > 0)
circular.averaging(x, deg = TRUE)
else
-1
},
subset= .(Parameter.Name == "Wind Direction - Resultant")
)
#join the wide format split wind_speed and wind_direction dataframes
wind.w <- merge(wind_speed, wind_direction)