【问题标题】:Table of column counts for a data frame数据框的列计数表
【发布时间】:2017-02-02 16:04:27
【问题描述】:

我有一个数据框,其中包含以字符串形式提供的分类数据列。每列的类别相同,例如:

myDF=data.frame(col1=sample(c("a","b","c"),10,replace=T),
                col2=sample(c("a","b","c"),10,replace=T),
                col3=sample(c("a","b","c"),10,replace=T))

我想按列生成每个类别的计数表。

当所有列都包含所有类别时,这可以通过apply 使用函数table 来完成,例如:

> myDF
   col1 col2 col3
1     a    c    a
2     b    b    b
3     a    a    b
4     b    b    a
5     c    c    a
6     a    a    a
7     a    c    c
8     a    a    c
9     c    a    a
10    a    a    b
> apply(myDF,2,table)
  col1 col2 col3
a    6    5    5
b    2    2    3
c    2    3    2

但是,如果某一列缺少某些类别,这将不起作用,因为 table 不知道预期的类别:

myDF=data.frame(col1=sample(c("a","b","c"),10,replace=T),
                col2=sample(c("a","b","c"),10,replace=T),
                col3=sample(c("a","b"),10,replace=T))

给予:

> myDF
   col1 col2 col3
1     c    a    a
2     a    a    b
3     b    a    a
4     c    c    a
5     c    a    a
6     c    c    a
7     c    b    a
8     c    b    b
9     a    a    a
10    b    b    a
> apply(myDF,2,table)    
$col1

a b c 
2 2 6 

$col2

a b c 
5 3 2 

$col3

a b 
8 2 

如何生成一个看起来像第一个的表格,其中任何缺失的类别都为 0?

【问题讨论】:

  • 另见table(stack(lapply(myDF, as.character)))

标签: r dataframe apply tabular


【解决方案1】:

您可以收集所有因子水平并使用apply

#get the levels from the whole data.frame
all_levels <- levels(unlist(myDF))

#convert each column to factor using the levels from above
#and then use table (which will return a zero for any missing levels)
apply(myDF, 2, function(x) table(factor(x, levels = all_levels)))

输出:

  col1 col2 col3
a    1    4    7
b    5    2    3
c    4    4    0

> myDF
   col1 col2 col3
1     b    a    a
2     c    b    a
3     c    c    b
4     b    a    b
5     b    c    a
6     c    c    a
7     c    b    a
8     b    a    b
9     a    c    a
10    b    a    a

【讨论】:

  • 在示例中效果很好。对于真实数据,列尚未强制转换为因子,但我能够成功地使用unique 代替levels 的相同方法。
  • 听起来不错!乐于助人:)
【解决方案2】:

我们可以使用mtabulate

library(qdapTools)
t(mtabulate(myDF))
#    col1 col2 col3
#a    2    5    8
#b    2    3    2
#c    6    2    0

它适用于 OP 帖子中提到的两种情况

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多