To illustrate what I'm trying to do, I'm using diamond dataset as an example. After group_by(cut), I want to do lm on each group, depending on the mean depth of each group, and then save the model in the dataframe.
diamonds %>% group_by(cut) %>%
mutate(mean.depth=mean(depth)) %>%
{if (.$mean.depth>60) do(model=lm(price~x, data=.))
else do(model=lm(price~y, data=.))}
This is what I got:
Error in function_list[[k]](value) : object 'mean.depth' not found
Spent an hour to fix it but failed. Appreciate it if anyone can help.
if
inside thedo()
(probably in an anonymous function) – Gregor Thomasdo(model = lm( if(mean(.$depth) >60){price~x}else{price~y}, data = .)
. It's justif
-ing the formulas – IRTFM