Marc Coll
Marc Coll

Reputation: 47

How can I count the frequency of string by another column value in a dataframe R

A simplification of the dataframe which I'm working is:

> df1
         Any              nomMun
   1     2010             CADAQUES
   2     2011             CADAQUES
   3     2012             CADAQUES
   4     2010             BEGUR
   5     2011             BEGUR
   6     2012             BEGUR

I've been reading some post and found that count of plyr library returns a dataframe with the strings and it's frequency. But I want the frequency by year. The final result I want to obtain is a dataframe like:

> df2
         nomMun       freq_2010     freq_2011     freq_2012
   1     CADAQUES         1             1             1
   2     BEGUR            1             1             1

Could anyone you help me?

Sorry if my explanation is bad... i'm non-native speaker and it's my first time asking here...

Upvotes: 1

Views: 214

Answers (3)

MKR
MKR

Reputation: 20095

tidyr::spread can be used to get the desired output:

library(tidyverse)
df1 %>% 
  group_by(nomMun, Any) %>%
  mutate(freq = n()) %>%
  spread(Any, freq)

# # A tibble: 2 x 4
# # Groups: nomMun [2]
#    nomMun   `2010` `2011` `2012`
# *  <chr>     <int>  <int>  <int>
# 1 BEGUR         1      1      1
# 2 CADAQUES      1      1      1

Upvotes: 0

Brian Davis
Brian Davis

Reputation: 992

Seems silly to load a package when base R includes the table function.

> table(df1)
      nomMun
Any    BEGUR CADAQUES
  2010     1        1
  2011     1        1
  2012     1        1

Upvotes: 0

C-x C-c
C-x C-c

Reputation: 1321

In data.table, simply use .N:

setDT(df1)
df1[, .N, .(nomMun, Any)]

This will give you the data in long format. In other words, it will look like:

Any      nomMum      N
2010     CADAQUES    1
2011     CADAQUES    1
2012     CADAQUES    1
2010     BEGUR       1
2011     BEGUR       1
2012     BEGUR       1

But then you can dcast it if you'd like:

dcast(df1[, .N, .(nomMun, Any)], nomMum ~ Any, value.var = "N")

Upvotes: 1

Related Questions