Workhorse
Workhorse

Reputation: 1560

Create a table for each factor in dataframe

I have a dataframe that looks like this:

>head(df)
                  Number          Label
1                      2            1-5
2                      2            1-5
3                      0            1-5
4                      2            1-5
5                      1           1-12
6                      0            1-1

I want to create a table for each label. So the end product is like this:

Label   Number   Frequency
1-5     2            3
1-5     0            1
1-12    1            1
1-1     0            1

Currently I can do this in multiple lines with base R, something like

df2 <- df[df$Label=="1-1", ]
final.df <- as.data.frame(table(df2$Number))

I do this for each label iteratively. It's super inefficient. Is there a way to do this more efficiently?

Appreciate the help!

Upvotes: 0

Views: 46

Answers (2)

717
717

Reputation: 36

In addition to Samet's method, you can also use count in dplyr

library(dplyr)

df %>%
  count(Label, Number, name="Frequency", sort=TRUE)

output:

 Label Number Frequency
  <chr>  <dbl>     <int>
1 1-5        2         3
2 1-1        0         1
3 1-12       1         1
4 1-5        0         1

Upvotes: 1

Samet S&#246;kel
Samet S&#246;kel

Reputation: 2670

it can be obtained easily with dplyr

library(dplyr)

df %>%
group_by(Label,Number) %>%
summarise(Frequency=n(),.groups='drop')

output;

  Label Number Frequency
  <fct>  <int>     <int>
1 1-1        0         1
2 1-12       1         1
3 1-5        0         1
4 1-5        2         3

Upvotes: 1

Related Questions