【问题标题】:how to get the frequency of unique elements in a column of a dataframe in R?如何获取R中数据框列中唯一元素的频率?
【发布时间】:2021-02-22 13:00:14
【问题描述】:

我有一个数据框:

 X65L X65L.1 X65L.2   X67L X67L.1 X65L.3
 [1,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
 [2,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
 [3,] 0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
 [4,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
 [5,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
 [6,] 0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
 [7,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
 [8,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
 [9,] 0.0067 0.0071 0.0067 0.0067 0.0071 0.0071
[10,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0071
[11,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0084
[12,] 0.0067 0.0084 0.0067 0.0067 0.0084 0.0084

我想计算一列中每个唯一元素的频率并得到如下输出:

     6     3     6     0     0     6
     6     3     6    12     6     2
     0     3     0     0     3     2
     0     3     0     0     3     2

MATLAB 等价物是:

[m1 n1]=hist(s,unique(s));

我想知道,如何在 R 中做到这一点。

【问题讨论】:

    标签: r dataframe unique


    【解决方案1】:

    你可以试试下面的代码

    apply(mat, 2, function(x) table(factor(x, levels = unique(c(mat)))))
    

    给了

           X65L X65L.1 X65L.2 X67L X67L.1 X65L.3
    0.0065    6      3      6    0      0      6
    0.0067    6      3      6   12      6      2
    0.0071    0      3      0    0      3      2
    0.0084    0      3      0    0      3      2
    

    数据

    > dput(mat)
    structure(c(0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067, 
    0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0065, 0.0065, 0.0065,
    0.0067, 0.0067, 0.0067, 0.0071, 0.0071, 0.0071, 0.0084, 0.0084,
    0.0084, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067,
    0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 
    0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067,
    0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0071,
    0.0071, 0.0071, 0.0084, 0.0084, 0.0084, 0.0065, 0.0065, 0.0065,
    0.0065, 0.0065, 0.0065, 0.0067, 0.0067, 0.0071, 0.0071, 0.0084,
    0.0084), .Dim = c(12L, 6L), .Dimnames = list(NULL, c("X65L",
    "X65L.1", "X65L.2", "X67L", "X67L.1", "X65L.3")))
    

    【讨论】:

      【解决方案2】:

      为了获得所需的输出,当我们在列中没有某些值时,我们需要填写 0:

      代码

      # First obtain all possible values
      name <- levels(as.factor(unlist(df)))
      tmp1 <- rep(0, length(name))
      names(tmp1) <- name
      
      tmp1
      # 0.0065 0.0067 0.0071 0.0084 
      #      0      0      0      0 
      
      # Now fill this table whenever we have additional information within a column
      
      sapply(df, function(x){
        tmp1[names(table(x))] <- table(x) 
        tmp1
      })
      
      #        X65L X65L.1 X65L.2 X67L X67L.1 X65L.3
      # 0.0065    6      3      6    6      6      6
      # 0.0067    6      3      6   12      6      2
      # 0.0071    0      3      0    0      3      2
      # 0.0084    0      3      0    0      3      2
      

      数据

      df <- read.table(text = "X65L X65L.1 X65L.2   X67L X67L.1 X65L.3
      0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
      0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
      0.0065 0.0065 0.0065 0.0067 0.0067 0.0065
      0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
      0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
      0.0065 0.0067 0.0065 0.0067 0.0067 0.0065
      0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
      0.0067 0.0071 0.0067 0.0067 0.0071 0.0067
      0.0067 0.0071 0.0067 0.0067 0.0071 0.0071
      0.0067 0.0084 0.0067 0.0067 0.0084 0.0071
      0.0067 0.0084 0.0067 0.0067 0.0084 0.0084
      0.0067 0.0084 0.0067 0.0067 0.0084 0.0084", header = T)
      

      【讨论】:

        【解决方案3】:

        我们也可以这样做

        table(c(mat), colnames(mat)[col(mat)])
        

        -输出

        #           X65L X65L.1 X65L.2 X65L.3 X67L X67L.1
        #  0.0065    6      3      6      6    0      0
        #  0.0067    6      3      6      2   12      6
        #  0.0071    0      3      0      2    0      3
        #  0.0084    0      3      0      2    0      3
        

        数据

        mat <- structure(c(0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067, 
        0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0065, 0.0065, 0.0065,
        0.0067, 0.0067, 0.0067, 0.0071, 0.0071, 0.0071, 0.0084, 0.0084,
        0.0084, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0065, 0.0067,
        0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 
        0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067,
        0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0067, 0.0071,
        0.0071, 0.0071, 0.0084, 0.0084, 0.0084, 0.0065, 0.0065, 0.0065,
        0.0065, 0.0065, 0.0065, 0.0067, 0.0067, 0.0071, 0.0071, 0.0084,
        0.0084), .Dim = c(12L, 6L), .Dimnames = list(NULL, c("X65L",
        "X65L.1", "X65L.2", "X67L", "X67L.1", "X65L.3")))
        

        【讨论】:

        • 多么出色的解决方案!一直向你学习!喜欢 +1!
        【解决方案4】:

        table() 对向量执行该功能。您可以使用apply() 为每一列运行此函数。 apply(data.frame,2,table)。如果列之间的值不同,结果可能会以列表的形式呈现。

        【讨论】:

        • 输出如下:$X65L 0.0065 0.0067 6 6 $X65L.1 0.0065 0.0067 0.0071 0.0084 3 3 3 3 $X65L.2 0.0065 0.0067 6 6 $X67L 1 0.0067 0.0071 0.0084 6 3 3 $X65L.3 0.0065 0.0067 0.0071 0.0084 6 2 2 2 我想要一个数据框,其中没有像问题中所示的元素为 0
        猜你喜欢
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多