Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate the frequency in R

Here is my data

> a
 [1] Male   Male   Female Male   Male   Male   Female Female Male   Male   Female Male   Male   Male  
[15] Female Female Female Male   Female Male   Female Male   Male   Female Male   Male   Female Male  
[29] Male   Male   Female Male   Male   Male   Female Female Male   Male   Male   Male   Male  
Levels:  Female Male

> b
[1] 0 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 1 0 1 1 1 1
Levels: 0 1

> table(a,b)
        b
a         0  1
          0  0
  Female 10  4
  Male   12 15

I don't know why the result of table(a,b) has a row of (0 0), my expected result is as follows:

> table(a,b)
        b
a         0  1
  Female 10  4
  Male   12 15

Could you tell me why this happens and how to correct it, thank you!

> dput(a)
structure(c(3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 
3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 
3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("", 
"Female", "Male"), class = "factor")

> dput(b)
structure(c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("0", 
"1"), class = "factor")
like image 295
lightsnail Avatar asked Jul 05 '16 22:07

lightsnail


People also ask

How do you calculate frequency in R?

There are multiple ways to get the count of the frequency of all unique values in an R vector. To count the number of times each element or value is present in a vector use either table(), tabulate(), count() from plyr package, or aggregate() function.

How do you find the frequency of a vector in R?

tabulate() function in R Language is used to count the frequency of occurrence of a element in the vector. This function checks for each element in the vector and returns the number of times it occurs in the vector. It will create a vector of the length of the maximum element present in the vector.

How do you find the frequency distribution of a column in R?

The table() method in R is used to compute the frequency counts of the variables appearing in the specified column of the dataframe. The result is returned to the form of a two-row tabular structure, where the first row indicates the value of the column and the next indicates its corresponding frequencies.


1 Answers

From the comments above:

This is happening because there is an empty factor level in a:

> levels(a)
[1] ""       "Female" "Male" 

You can keep produce a table that disregards empty factor levels (from @lmo's comment):

table(as.character(a), b)

Alternatively, you can easily remove factor levels without observations (from @Dave2e's comment)

a <- droplevels(a)
table(a, b)
like image 195
KenHBS Avatar answered Sep 26 '22 17:09

KenHBS