2

我正在尝试将长格式风数据转换为宽格式。风速和风向都列在 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)
4

3 回答 3

0

您正在使用wind.w定义的代码内部wind.w- 那是行不通的!

您还使用了斜引号 (`) 而不是直引号 (')。应该使用直引号来描述字符串。

于 2017-02-09T22:37:37.007 回答
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)
于 2017-02-10T00:49:54.573 回答
0

好的,感谢您的所有帮助,我设法解决了这个讨厌的风向问题。有时解决问题只是知道要问的正确问题。就我而言,我只需要学习“向量平均”这个术语!circular.averaging()从包中调用了一个内置的矢量平均函数SDMTools,它平均风向并产生仍在 0-359 度之间的输出!我最终做的是附加 tjjjohnson 的脚本。我将fun.aggregate参数从 这里更改mean(circular(c(x), units = "degrees"), na.rm = TRUE)原始数据和聚合数据的直方图!一切看起来都很好,谢谢大家!circular.averaging(x, deg = TRUE)

于 2017-02-12T20:20:17.417 回答