首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用dcast在R中重塑EPA风速和风向数据

用dcast在R中重塑EPA风速和风向数据
EN

Stack Overflow用户
提问于 2017-02-09 22:16:19
回答 3查看 491关注 0票数 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”参数中嵌套这个附加函数。我认为简单的如果语句可以做到这一点的话,但是我遇到了以下错误:

代码语言:javascript
复制
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')用于风向。

以下是原始数据(>100‘s):bZ8CGwuUUhGdk9ONTgtT0E

下面是数据的子集(第1 100行):bZ8CGwucVZGT0pBQlFzT2M

这是我的剧本:

代码语言:javascript
复制
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

编辑:这是解决方案:

代码语言:javascript
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-10 00:49:54

您可以使用dcast中的子集来应用这两个函数,并将它们分开,然后合并它们。

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2017-02-09 22:37:37

在定义wind.w的代码中使用wind.w --这是行不通的!

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

票数 0
EN

Stack Overflow用户

发布于 2017-02-12 20:20:17

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42147914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档