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,010 points
74 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,550 points

Related Questions In Data Analytics

0 votes
1 answer

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

Yes, we can find the index of ...READ MORE

answered Apr 13, 2018 in Data Analytics by zombie
• 3,690 points
152 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,680 points
393 views
0 votes
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 13, 2018 in Data Analytics by darklord
• 6,140 points
89 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,550 points
45 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

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

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
80 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,550 points
41 views
+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

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

Counting the frequency of unique values - R

table() function will help you out in ...READ MORE

answered May 18, 2018 in Data Analytics by Bharani
• 4,550 points
2,229 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,550 points
1,822 views