【问题标题】:frequency count of states in multiple columns in a dataframe in RR中数据帧中多列状态的频率计数
【发布时间】:2015-02-09 03:56:28
【问题描述】:

我有一张如下所示的表格:

City  Browser   Device    Channel        Category
A     IE        mobile    International  Fashion
A     safari    mobile    Link           Furniture
B     chrome    desktop   Link           Fashion
B     opera     tablet    Direct         Gift
C     IE        desktop   Direct         Health
C     Kindle    console   Email          Health
C     Chrome    console   Email          Fashion

我需要得到每个子标题的频率与主标题 Category:

Category  Sub Category  Variable        Value 
Health    Device        mobile          25%
Health    Device        console         40%
Health    Device        desktop         25%
Health    Device        tablet          10% 
Health    Channel       International   80%
Health    Channel       Direct          20%
Fashion
Gift

其他类别依此类推。发布这个我想创建一个基于Category 和每个子类别的分段条形图。如果我没有得到频率,计数很好,因为我可以在 ggplot 中转换为频率。

【问题讨论】:

    标签: r ggplot2 multiple-columns plyr frequency


    【解决方案1】:

    (我在数据文件示例中添加了几行)

    这是你想要的吗?:

    dat <- read.table(text="City  Browser   Device    Channel        Category
    A     IE        mobile    International  Fashion
    A     safari    mobile    Direct         Furniture
    A     safari    console   Email          Health
    B     chrome    desktop   Link           Fashion
    B     opera     tablet    Direct         Gift
    B     opera     mobile    Link           Furniture
    C     opera     console   Direct         Gift
    C     IE        desktop   Direct         Health
    C     Kindle    console   Email          Health
    C     Chrome    console   Email          Fashion", 
                      header=TRUE, stringsAs=FALSE)
    
    library(dplyr)
    library(tidyr)
    
    dat %>%
      gather(`Sub Category`, `Variable`, Device, Channel) %>%
      count(`Category`, `Sub Category`, `Variable`)
    
    ## Source: local data frame [16 x 4]
    ## Groups: Category, Sub Category
    ## 
    ##     Category Sub Category      Variable n
    ## 1    Fashion       Device       console 1
    ## 2    Fashion       Device       desktop 1
    ## 3    Fashion       Device        mobile 1
    ## 4    Fashion      Channel         Email 1
    ## 5    Fashion      Channel International 1
    ## 6    Fashion      Channel          Link 1
    ## 7  Furniture       Device        mobile 2
    ## 8  Furniture      Channel        Direct 1
    ## 9  Furniture      Channel          Link 1
    ## 10      Gift       Device       console 1
    ## 11      Gift       Device        tablet 1
    ## 12      Gift      Channel        Direct 2
    ## 13    Health       Device       console 2
    ## 14    Health       Device       desktop 1
    ## 15    Health      Channel        Direct 1
    ## 16    Health      Channel         Email 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2021-10-07
      • 1970-01-01
      • 2012-06-08
      • 2021-06-27
      • 2020-06-30
      相关资源
      最近更新 更多