stackinator
stackinator

Reputation: 5819

dplyr piping produces different results than base R syntax utilizing same code

I'm performing an academic exercise that demonstrates an issue with my understanding of dplyr. Let me reconstruct the iris data set using base-R syntax:

library(dplyr)

bind_cols(iris[1], iris[-1])

OK, great that works. Now, I'll pipe everything with dplyr - and - it doubles every column in the iris data set. Shouldn't these two pieces of code produce identical results?

iris %>% bind_cols(.[1], .[-1])

Upvotes: 3

Views: 114

Answers (1)

www
www

Reputation: 39154

Please see the following. iris %>% bind_cols(.[1], .[-1]) is the same as bind_cols(iris, iris[1], iris[-1]) because the first argument of bind_cols is the one before %>%. So the result makes sense to me.

aa <- iris %>% bind_cols(.[1], .[-1])

bb <- bind_cols(iris, iris[1], iris[-1])

identical(aa, bb)
# [1] TRUE

Upvotes: 4

Related Questions