Find frequency of each column in data.frame

后端 未结 2 570
小蘑菇
小蘑菇 2021-01-22 12:17

I have a data.frame as below, and i would like to find the frequency for each column.

data.frame ;

No      Location     Age         Size         Gender
1         


        
相关标签:
2条回答
  • 2021-01-22 12:43

    Maybe your looking for lapply and table

    > lapply(df[, -1], function(x) as.data.frame(table(x)))
    $Location
             x Freq
    1     Asin    3
    2 Trinidad    4
    
    $Age
       x Freq
    1 23    3
    2 25    2
    3 33    2
    
    $Size
      x Freq
    1 2    2
    2 3    2
    3 4    1
    4 5    2
    
    $Gender
      x Freq
    1 1    3
    2 2    4
    
    0 讨论(0)
  • 2021-01-22 12:44

    You can use table:

    > t <- read.table(text="No      Location     Age         Size         Gender
    + 1        Trinidad     25          3              1
    + 2        Asin         33          2              2
    + 3        Trinidad     33          5              2
    + 4        Trinidad     23          3              1
    + 5        Asin         25          5              1
    + 6        Asin         23          2              2
    + 7        Trinidad     23          4              2",head=T)
    > table(t$Location)
    
        Asin Trinidad 
           3        4 
    > table(t$Age)
    
    23 25 33 
     3  2  2 
    > table(t$Size)
    
    2 3 4 5 
    2 2 1 2 
    > table(t$Gender)
    
    1 2 
    3 4
    
    0 讨论(0)
提交回复
热议问题