R: How to calculate year-wise mean and other operations on daily data for elements in a column

我只是一个虾纸丫 提交于 2019-12-13 07:27:21

问题


Please help me out. I have been stuck for a long time. I am an R beginner, and I want to know how to perform operations on data frame elements using multiple indices e.g. firms and time as in the below case elegantly? I want to perform some typical operations e.g year-wise mean on some other variables for each firm, and here is an example: I am want to calculate year-wise mean from daily data of variables A and F for all firms in the dataset. This is my dataset. I have used dplyr and hydroTSM packages. Following is my code:

MeanA<- df %>% select(Firm, Date, A,) %>% group_by(Firm) %>% do(daily2annual(., A, FUN=mean, na.rm = TRUE, out.fmt="%Y", dates = 2))

It returns the following error:

Error in eval(expr, envir, enclos) : argument is missing, with no default

I have also tried the data.table package with adding an additional Year column. Code:

MeanA <- df[ , A, by = "Firm" & "Year"]

Result:

Error in `[.data.frame`(df, , A, by = "Firm" & "Year") : unused argument(by = "Firm" & "Year")

Please suggest a way out (with or without dplyr). The table of means, as well as other year-wise values I would be calculating, is input for panel regression analysis using plm package. Thanks in advance.


回答1:


Well this isn't a pretty answer but I'm tired of working at it. If you want the yearly mean by firm, I extracted the year from the date field and then used this in the aggregate function.

df$Date = as.Date(df$Date, "%m/%d/%Y")
df$F = as.numeric(df$F)
df$Year = as.Date(paste(substr(df$Date, 1, 4), "1", "1",sep="-"), "%Y-%m-%d")
newDat = aggregate(cbind(A, E, F) ~ Firm + Year, df, FUN = mean)



回答2:


Try

MeanA<- df %>%
    select(Firm, Date, A) %>%
    group_by(Firm) %>%
    do(daily2annual(., A, mean, na.rm = TRUE, dates = .$Date))


来源:https://stackoverflow.com/questions/35817981/r-how-to-calculate-year-wise-mean-and-other-operations-on-daily-data-for-elemen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!