Building on this question: dplyr: how to reference columns by column index rather than column name using mutate?
I want to mutate using column indexes for both the source and the destination of the mutate:
iris %>% head %>% mutate(.[[1]] = .[[1]] + .[[2]])
gives:
Error: unexpected '=' in "iris %>% head %>% mutate(.[[1]] =".
However, the following works:
iris %>% head %>% mutate(sum = .[[1]] + .[[2]])
We can do this in base R
iris[[1]] <- iris[[1]] + iris[[2]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With