2017-08-10 54 views
1

爲了說明我正在嘗試執行的操作,我以鑽石數據集爲例。在group_by(cut)之後,我想對每個組做lm,具體取決於每個組的平均深度,然後將模型保存在數據框中。{if ... else ..}在dplyr鏈中的group_by之後的語句

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=.))} 

這是我得到:

Error in function_list[[k]](value) : object 'mean.depth' not found 

花了一個小時,也沒修好。欣賞它,如果任何人都可以提供幫助。

+2

我把'if'內'做()'(可能是在一個匿名函數) – Gregor

+0

怎麼辦' (model = lm(if(mean(。$ depth)> 60){price〜x} else {price〜y},data =。)'。只是如果公式 –

回答

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

試試這個:

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>