How to create a frequency column for categorical variable in an R data frame?



To create a frequency column for categorical variable in an R data frame, we can use the transform function by defining the length of categorical variable using ave function. The output will have the duplicated frequencies as one value in the categorical column is likely to be repeated. Check out the below examples to understand how it can be done.

Example

Consider the below data frame −

 Live Demo

Country<-sample(c("India","China","Egypt"),20,replace=TRUE) Response<-rnorm(20) df1<-data.frame(Country,Response) df1

Output

   Country  Response 1  Egypt   -0.6591480 2  China   -1.8163343 3  India   -1.0608470 4  Egypt    0.6736968 5  Egypt    0.7686130 6  India   -0.5509014 7  Egypt   -1.4049758 8  India   -0.1783958 9  China   -0.3233439 10 China    0.5749841 11 China    0.3870373 12 China   -0.9342403 13 China    0.2300502 14 Egypt   -0.4034456 15 Egypt   -0.5925468 16 India   -1.0564102 17 India    0.1227065 18 China    1.7980140 19 China   -1.3700720 20 India    0.9327951

Creating a frequency column for Country column in df1 −

Example

transform(df1,Country_Frequency=ave(seq(nrow(df1)),Country,FUN=length))

Output

  Country Response   Country_Frequency 1 Egypt   -0.6591480   6 2 China   -1.8163343   8 3 India   -1.0608470   6 4 Egypt    0.6736968   6 5 Egypt    0.7686130   6 6 India   -0.5509014   6 7 Egypt   -1.4049758   6 8 India   -0.1783958   6 9 China   -0.3233439   8 10 China   0.5749841   8 11 China   0.3870373   8 12 China  -0.9342403   8 13 China   0.2300502   8 14 Egypt  -0.4034456   6 15 Egypt  -0.5925468   6 16 India  -1.0564102   6 17 India   0.1227065   6 18 China   1.7980140   8 19 China  -1.3700720   8 20 India   0.9327951   6

Example

 Live Demo

Temp<-sample(c("Hot","Cold"),20,replace=TRUE) Y<-rpois(20,2) df2<-data.frame(Temp,Y) df2

Output

   Temp   Y 1  Cold   1 2  Hot    1 3  Cold   4 4  Hot    1 5  Hot    2   6  Hot    4 7  Hot    1 8  Hot    1 9  Cold   2 10 Hot    1 11 Cold   1 12 Hot    2 13 Hot    3 14 Cold   2 15 Cold   0 16 Cold   4 17 Hot    4 18 Hot    0 19 Cold   1 20 Cold   1

Creating a frequency column for Temp column in df2 −

Example

transform(df2,Temp_Frequency=ave(seq(nrow(df2)),Temp,FUN=length))

Output

  Temp   Y  Temp_Frequency 1  Cold  1   9 2  Hot   1  11 3  Cold  4   9 4  Hot   1  11 5  Hot   2  11 6  Hot   4  11 7  Hot   1  11 8  Hot   1  11 9  Cold  2   9 10 Hot  1   11 11 Cold 1    9 12 Hot  2   11 13 Hot  3   11 14 Cold 2    9 15 Cold 0    9 16 Cold 4    9 17 Hot  4   11 18 Hot  0   11 19 Cold 1    9 20 Cold 1    9
Updated on: 2021-03-16T12:25:10+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements