{if…else..} statement after group_by in dplyr chai

2019-08-12 05:36发布

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条回答
Evening l夕情丶
2楼-- · 2019-08-12 06:07
diamonds %>%
    group_by(cut) %>%
    do(model=if(mean(.$depth) > 60)
                lm(price ~ x, data=.)
             else lm(price ~ y, data=.))
查看更多
爷、活的狠高调
3楼-- · 2019-08-12 06:12

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>
查看更多
登录 后发表回答