How to count rows in a table with two variables

后端 未结 1 1633
执念已碎
执念已碎 2021-01-26 05:15

I have a dataframe called dF with two columns: Name, Region. For instance,

Name Region
a    EU
a    EU
b    AM
C    AP
...  ...

If I do table(d

相关标签:
1条回答
  • 2021-01-26 05:44

    Example:

    result <- table(state.division, state.region)  #Sample data 
    result #will return data like shown below
    
            state.region
    state.division       Northeast South North Central West
      New England                6     0             0    0
      Middle Atlantic            3     0             0    0
      South Atlantic             0     8             0    0
      East South Central         0     4             0    0
      West South Central         0     4             0    0
      East North Central         0     0             5    0
      West North Central         0     0             7    0
      Mountain                   0     0             0    8
      Pacific                    0     0             0    5
    
      sum(result[,2]==4) #to count 4's in second column
    

    So in your case you need to store the resulting table to a variable, and the following should do it:

    result <- table(dF)
    sum(result[,2]==3)
    
    0 讨论(0)
提交回复
热议问题