Counting the frequency of unique values - R

+5 votes

I am working with the 'mtcars' data-set:

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

I want to find out the number of frequency of unique values for the 'cyl' column, i.e, number of occurences of each value. 

May 18, 2018 in Data Analytics by nirvana
• 3,130 points
81,810 views

3 answers to this question.

+1 vote

table() function will help you out in this case:

table(mtcars$cyl)

 4  6  8 
11  7 14

 

answered May 18, 2018 by Bharani
• 4,660 points
how to visualize it so that it will that particular element along with it's frequency

You can use plyr module. It will give you the frequency count. You can see the below example.

> library("plyr")
> count(mtcars, 'gear')
  gear freq
1    3   15
2    4   12
3    5    5
+1 vote

Try this,

lapply(mtcars,function(x){ length(unique(x))})

answered Aug 20, 2019 by anonymous
• 33,050 points
+1 vote

Hi,

You can use plyr module. It will give you the frequency count. You can see the below example.

> library("plyr")
> count(mtcars, 'gear')
  gear freq
1    3   15
2    4   12
3    5    5
answered Dec 14, 2020 by MD
• 95,420 points

Related Questions In Data Analytics

+4 votes
0 answers

Counting the frequency of user activities - R

Within a data frame (see below), I ...READ MORE

Dec 3, 2019 in Data Analytics by anonymous
• 160 points
903 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
3,607 views
0 votes
1 answer

Change the order of legend values in a plot in R

@prardhana, Use scale_fill/color/size_discrete/contin....(labels = c()). compare both to see ...READ MORE

answered Nov 4, 2019 in Data Analytics by payal
1,319 views
0 votes
1 answer

Show count of unique values for each value in the field in dataframe

Hi, Use below r code. For one field, you ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,050 points
1,427 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
908 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
423 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,191 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
4,697 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,156 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP