1
votes

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.

2
I'd put the if inside the do() (probably in an anonymous function)Gregor Thomas
How about do(model = lm( if(mean(.$depth) >60){price~x}else{price~y}, data = .). It's just if-ing the formulasIRTFM

2 Answers

2
votes
diamonds %>%
    group_by(cut) %>%
    do(model=if(mean(.$depth) > 60)
                lm(price ~ x, data=.)
             else lm(price ~ y, data=.))
1
votes

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>