2015-09-22 3 views
-2
library(magrittr) 

myDf <- dataSetSubsetting 
     %>% !is.na(.[,c("colx")]) 
     %>% !is.na(.[,c("coly")]) 
     %>% someFunction(.[,c("colx", "coly")], .) 

Это как я думал, что magrittr работает. Однако это вызывает у меня ошибку:%% оператор R дает непредвиденную ошибку

384: myDf <- dataSetSubsetting %>% !is.na(.[,c("colx")]) 
385: %>% 
    ^

Почему?

+0

Не могли бы вы предоставить информацию о «dataSetSubsetting», что вы пытаетесь достичь и каков ваш ожидаемый результат !? – Cleb

+0

@Cleb Его набор данных I подмножество (dataframe). Затем мне нужно снова подмножить его и применить некоторую функцию. Это не более того. – Stophface

+0

Что такое сообщение об ошибке? Вы проверили в 'sessionInfo()', чтобы случайно не загружать 'dplyr' и маскировать'%>% 'таким образом ... – drmariod

ответ

2

Вот более подробное объяснение, основанное на комментарии erasmortg.

library(dplyr) 

#* Works 
mtcars %>% 
    mutate(am = factor(am, 0:1, c("Automatic", "Manual"))) 

#* Fails: the interpreter looks at the end of the first 
#*  line and sees no operator, so it prints 'mtcars' 
#*  and declares the operation complete. 
#*  It then looks at the second line and can't find 
#*  the left hand side for '%>%', causing an error. 
mtcars 
    %>% mutate(am = factor(am, 0:1, c("Automatic", "Manual"))) 

#* Fails: Similar story to the previous example. In this case, 
#*  printing is delayed until all of the statements within 
#*  the braces successfully run. 
{mtcars 
    %>% mutate(am = factor(am, 0:1, c("Automatic", "Manual")))} 

#* Succeeds: The interpreter sees that you've opened 
#* with parentheses, and tries to connect all of the 
#* lines into a coherent statement. With the parentheses, 
#* when the interpreter sees no operator at the end of 
#* 'mtcars', it says to itself, "I hope I find one on the 
#* next line" and tries to piece it together. 
(mtcars 
    %>% mutate(am = factor(am, 0:1, c("Automatic", "Manual")))) 
Смежные вопросы