首页 文章

dplyr()分组和获取计数 - 错误消息评估错误:没有适用于'summarise_'的方法应用于类“逻辑”的对象

提问于
浏览
0

我有一个数据框(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 回答

  • 0

    Krzysztof的解决方案是要走的路,但如果你坚持使用 tidyverse ,这里有一个 dplyr tidyr 的解决方案:

    library(dplyr)
    library(tidyr)
    
    df %>%
      group_by(location, weather) %>%
      summarize(count = count(weather)) %>%
      spread(weather, count, sep="_") %>%
      mutate_all(funs(coalesce(., 0L)))
    

    Result:

    # A tibble: 3 x 4
    # Groups:   location [3]
      location weather_1 weather_2 weather_3
        <fctr>     <int>     <int>     <int>
    1       az         2         1         1
    2       bi         0         2         1
    3       ca         2         2         1
    
  • 3

    它的功能非常简单,称为table:

    df %>% table  
    
            weather
    location 1 2 3
          az 2 1 1
          bi 0 2 1
          ca 2 2 1
    
  • 1

    Krzysztof的答案很简单,但是如果你想要一个只有整数的解决方案( dplyrtidyr ):

    df %>% 
        group_by(location, weather) %>% 
        summarize(bin = sum(weather==weather)) %>%
        spread(weather, bin, fill = 0, sep='_')
    

    这导致:

    location    weather_1   weather_2   weather_3
    az  2   1   1
    bi  0   2   1
    ca  2   2   1
    

相关问题