r - How to nest multiple pipes magrittr -
this starts aestethic question turns functional one, magrittr.
i want add data_frame manually input 1 there so:
cars_0 <- mtcars %>% mutate(brand = row.names(.)) %>% select(brand, mpg, cyl) new_cars <- matrix(ncol = 3, byrow = t, c( "vw beetle", 25, 4, "peugeot 406", 42, 6)) # coercing types not issue here. cars_1 <- rbind(cars_0, set_colnames(new_cars, names(cars_0)))
i'm writing new cars in matrix "increased legibility", , therefore need set column names bound cars_0
.
if likes magrittr as do, might want present new_cars
first , pipe set_colnames
cars_1 <- rbind(cars_0, new_cars %>% set_colnames(names(cars_0)))
or avoid repetition they'll want indicate cars_0
, pipe rbind
cars_1 <- cars_0 %>% rbind(., set_colnames(new_cars, names(.)))
however 1 cannot both there confusion whom being piped
cars_1 <- cars_0 %>% rbind(., new_cars %>% set_colnames(names(.))) ## error in match.names(clabs, names(xi)) : ## names not match previous names
my question: there way distinguish 2 arguments piped?
short answer: no.
longer answer: i'm not sure rationale doing be. philosophy behind magrittr
unnest composite functions, primary intent of making easier read code. example:
f(g(h(x)))
becomes
h(x) %>% g() %>% f()
trying use pipes in manner places 2 objects interpreted .
argument goes against philosophy of simplification. there circumstances in can have nested pipes, environments ought remain distinct. trying cross 2 pipes in same environment can likened crossing streams.
don't cross streams :)