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,020 points
187 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
789 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,690 points
576 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 13, 2018 in Data Analytics by darklord
• 6,170 points
191 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
77 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
121 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
61 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 in Data Analytics by anonymous
100 views
0 votes
2 answers

Counting the frequency of unique values - R

Try this, lapply(mtcars,function(x){ length(unique(x))}) READ MORE

answered Aug 20 in Data Analytics by anonymous
• 31,840 points
5,182 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
3,461 views