user3475602
user3475602

Reputation: 1217

Using sum in dplyr mutate with filter

My dataframe looks like this:

"count","stock"
5,"Google"
21,"Google"
...
27,"Google"
30,"Google"
22,"Google"
4,"Google"
18,"Google"
4,"Google"
12,"Apple"
27,"Apple"
5,"Apple"
25,"Apple"
...

I want to add another column named freq with the relative frequency of each count value:

df_stock %>% mutate(freq = count/sum(df_stock[df_stock$stock == (.)$stock, ]$count))

My problem is that the selection for the current stock value is not working, meaning that sum(df_stock[df_stock$stock == (.)$stock, ]$count) returns the sum of all count values not just the ones for the corresponding stock under evaluation (e.g., Google or Apple).

Upvotes: 0

Views: 738

Answers (2)

Bruno
Bruno

Reputation: 4151

This should work

library(tidyverse)

df_stock %>%
  group_by(stock) %>% 
  mutate(freq = count/sum(count)

Upvotes: 1

hello_friend
hello_friend

Reputation: 5788

Base R solution:

cbind(df, freq = with(df,  ave(count, stock, FUN = prop.table)))

Upvotes: 0

Related Questions