Reputation: 11793
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.
Upvotes: 1
Views: 2976
Reputation: 57686
diamonds %>%
group_by(cut) %>%
do(model=if(mean(.$depth) > 60)
lm(price ~ x, data=.)
else lm(price ~ y, data=.))
Upvotes: 2
Reputation: 8275
Try this:
diamonds %>% group_by(cut) %>%
mutate(mean.depth=mean(depth),
form = ifelse(mean.depth>60,
"price~x",
"price~y")) %>%
do(model = lm(as.formula(.$form), data = .))
Source: local data frame [5 x 2] Groups: <by row> # A tibble: 5 x 2 cut model * <ord> <list> 1 Fair <S3: lm> 2 Good <S3: lm> 3 Very Good <S3: lm> 4 Premium <S3: lm> 5 Ideal <S3: lm>
Upvotes: 1