2
我有一個數據框,每年的降水量在1997-2016年之間。下面是一個例子:使用tidyverse對變異係數進行分組和總結
df<- data.frame(year= as.character(c("1997", "1997","1997","1997","1997","1997","1998","1998","1998")),month= as.character(c("1", "1","1","2","2","2","1","1","1")),cont_month= as.character(c("1", "1","1","2","2","2","13","13","13")),precip= as.numeric(c(5, 2,4,5,6,2,1,3,7)))
我想計算變異係數爲每日沉澱爲每個Cont_Month
子集。我使用library(raster)
包使用函數cv()
。新的數據幀應該是這樣的:
output<- data.frame(year= as.character(c("1997",
"1997","1998")),month= as.character(c("1", "2","1")),cont_month=
as.character(c("1", "2","13")),cv= as.numeric(c(41.6, 48.03,83.31))
我有下面的代碼的麻煩,它沒有成功由cont_month
變量分組日常precip
。相反,在precip
列中重複相同的值。任何想法我的錯誤是什麼?
output<-
df %>%
group_by(year, month,cont_month)%>%
summarise(cv= cv(df$precip))
不要使用'$' - 只需調用'summarize(cv = cv(precip))' - 這就是管道的全部要點,所以你不必再引用源對象。 – thelatemail