我正在尝试将长格式风数据转换为宽格式。风速和风向都列在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'论点。我认为一个简单的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)
答案 0 :(得分:0)
您在定义wind.w
的代码中使用wind.w
- 这不起作用!
您还使用了带角度的引号(`)而不是直引号(')。应使用直引号标记字符串。
答案 1 :(得分:0)
你可以在dcast中使用子集来应用这两个函数并获得单独的数据帧然后合并它们
library(reshape2)
library(dplyr)
library(circular)
#cast into wide format:
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")
)
wind_direction <- dcast(wind,
Local.Site.Name + Date.Local ~ Parameter.Name,
value.var = "Arithmetic.Mean",
fun.aggregate = function(x) {
if(length(x) > 0)
mean(circular(c(x), units="degrees"), na.rm=TRUE)
else
-1
},
subset=.(Parameter.Name == "Wind Direction - Resultant")
)
wind.w <- merge(wind_speed, wind_direction)
答案 2 :(得分:0)
好的,多亏了你的帮助,我设法解决了这个讨厌的风向问题。有时解决问题只是要知道要问的正确问题。在我的例子中,学习术语“矢量平均”&#39;就是我所需要的一切!有一个内置的矢量平均函数circular.averaging()
来自SDMTools
包,它平均风向并产生一个仍在0-359度之间的输出!我最终做的是附加tjjjohnson的脚本。我将fun.aggregate
参数从mean(circular(c(x), units = "degrees"), na.rm = TRUE)
更改为circular.averaging(x, deg = TRUE)
以下是raw and aggregated数据的直方图!一切都很好看,谢谢大家!