sanaz sanaz - 1 year ago 88
R Question

how to convert dplyr::summaries to dplyr::do

I have the following data frame:

label target sale
low 5000 4000
low 11000 9000
low 5500 4500
low 12000 9500
middle 20000 18000
middle 22000 190000
high 100000 90000
high 120000 95000

I need to first group my data frame by label and then calculate: mean(target - sale) only for rows where target is more than 10000, I tried following code:

avg_increase <- df %>%
group_by(labels) %>%
summarise(avg_sale_increase = ifelse(target >= 10000,mean(target - sale), 0))

and I get the following error:

Error: expecting a single value

only returns single values, should I change my code using
(or any other suggestion) to accomplish the task.

Answer Source

The problem is with ifelse, which returns a vector if length equal to the input logical, so the mean is going to be calculated as many times as required.

This should work

avg_increase <- df %>% 
                group_by(labels) %>% 
                filter(target >= 10000) %>% 
                summarise(target = mean(target-sale))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download