我有一个数据框(df),有两个变量,位置和天气 .
我想要一个宽数据框(dfgoal),其中数据按位置分组,其中有三个新变量(weather_1到weather_3),其中包含原始天气变量中的观测值 .
问题在于,当我尝试使用dplyr():: mutate()时,我只得到TRUE / FALSE输出而不是计数,或者是错误消息: Evaluation error: no applicable method for 'summarise_' applied to an object of class "logical"
.
任何帮助将非常感激 .
起点(df):
df <- data.frame(location=c("az","az","az","az","bi","bi","bi","ca","ca","ca","ca","ca"),weather=c(1,1,2,3,2,3,2,1,2,3,1,2))
期望的结果(df):
dfgoal <- data.frame(location=c("az","bi","ca"),weather_1=c(2,0,2),weather_2=c(1,2,2),weather_3=c(1,1,1))
当前代码:
library(dplyr)
df %>% group_by(location) %>% mutate(weather_1 = (weather == 1)) %>% mutate(weather_2 = (weather == 2)) %>% mutate(weather_3 = (weather == 3))
df %>% group_by(location) %>% mutate(weather_1 = summarise(weather == 1)) %>% mutate(weather_2 = summarise(weather == 2)) %>% mutate(weather_3 = summarise(weather == 3))
3 回答
Krzysztof的解决方案是要走的路,但如果你坚持使用
tidyverse
,这里有一个dplyr
tidyr
的解决方案:Result:
它的功能非常简单,称为table:
Krzysztof的答案很简单,但是如果你想要一个只有整数的解决方案(
dplyr
和tidyr
):这导致: