【问题标题】:Calculating frequency for each column and calculating the frequency for the whole matrix计算每列的频率并计算整个矩阵的频率
【发布时间】:2019-05-07 19:39:39
【问题描述】:

我的矩阵如下表

v1  v2  v3
M   Z   P
Z   Z   P
P   Z   M
Z   P   Z

我想计算每列的频率,然后计算整个数据的频率。期望的结果是:

Factor  V1  V2  V3  Freq  Percentage
M       1   0   1   2     16.66666667
P       1   1   2   4     33.33333333
Z       2   3   1   6     50

我试过sapply(df, table),但没用。任何帮助都会很棒,尤其是使用tidyverse

【问题讨论】:

    标签: r tidyverse frequency


    【解决方案1】:

    这是tableaddmargins 的一种方式

    out <- addmargins(table(unlist(df1), c(col(df1))), 2)
    cbind(out, Percentage = 100 *out[,4]/sum(out[, 4]))
    #  1 2 3 Sum Percentage
    #M 1 0 1   2  16.66667
    #P 1 1 2   4  33.33333
    #Z 2 3 1   6  50.00000
    

    或者以更紧凑的方式

    library(qdapTools)
    transform(as.data.frame(addmargins(t(mtabulate(df1)), 2)),
                     Percentage = 100 * Sum/sum(Sum))
    

    或使用tidyverse

    library(tidyverse)
    gather(df1, key, Factor) %>%
        dplyr::count(key, Factor) %>% 
        spread(key, n, fill = 0) %>% 
        mutate(Freq = rowSums(.[-1]), 
               Percentage = 100 * Freq/sum(Freq))
    # A tibble: 3 x 6
    #  Factor    v1    v2    v3  Freq Percentage
    #  <chr>  <dbl> <dbl> <dbl> <dbl>      <dbl>
    #1 M          1     0     1     2       16.7
    #2 P          1     1     2     4       33.3
    #3 Z          2     3     1     6       50  
    

    数据

    df1 <- structure(list(v1 = c("M", "Z", "P", "Z"), v2 = c("Z", "Z", "Z", 
     "P"), v3 = c("P", "P", "M", "Z")), class = "data.frame",
      row.names = c(NA, -4L))
    

    【讨论】:

    • @USER2313456 这很简单gather(df1, key, Factor) %&gt;% mutate(key = factor(key, levels = unique(key))) %&gt;% spread
    • @USER2313456 我的意思是在上一条评论之后使用spread(key, n, fill = 0)
    • @USER2313456 你是说我发布的所有 3 个解决方案都是错误的
    • @USER2313456 gather(df1, key, Factor) %&gt;% mutate(key = factor(key, levels = unique(key))) %&gt;% dplyr::count(key, Factor) %&gt;% spread(key, n, fill = 0) %&gt;% mutate(Freq = rowSums(.[-1]), Percentage = 100 * Freq/sum(Freq)) 为我工作
    • 我想早先,我忘记了count之间的步骤
    猜你喜欢
    • 2018-10-10
    • 2016-09-03
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多