Finding frequency of observations in R

0 votes

I have a dataframe in R that includes a column for "fruit name" and whether the fruit is "available" or not, with the value '1' for the fruit being available and the value '0' if the fruit is not available:

Fruit    Available
Apple       0
Apple       1
Apple       1
Banana      1
Banana      0
Pear        1
Pear        0
Guava       0

I want to find the frequency of all these fruits and also find the number of times the fruit was "available" i.e. for the above table, i want the output to be like this:

Fruit    frequency    Total_Availability
Apple         3            2
Banana        2            1
Pear          2            1
Guava         1            0

Mar 26, 2018 in Data Analytics by coldcode
• 2,090 points
5,890 views

1 answer to this question.

0 votes

You can use the "dplyr" package to find the solution:

library(dplyr)
df %>% 
    group_by(Fruit) %>% 
    mutate(frequency = n(), Total_Availability = sum(Availability)) %>% 
    select(Fruit, frequency, Total_Availability) %>% 
    distinct()

print(df)
answered Mar 26, 2018 by Bharani
• 4,660 points

Related Questions In Data Analytics

0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
56,632 views
0 votes
1 answer
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,623 views
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,037 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,179 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,112 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,426 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,937 views
+5 votes
3 answers

Counting the frequency of unique values - R

Hi, You can use plyr module. It will give ...READ MORE

answered Dec 14, 2020 in Data Analytics by MD
• 95,460 points
98,473 views
0 votes
1 answer

Changing the order of bars in a bar-plot - ggplot2 - R

You can use the scale_x_discrete() function with ...READ MORE

answered May 28, 2018 in Data Analytics by Bharani
• 4,660 points
9,723 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