简体   繁体   中英

R dplyr mutate on column index

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]])

我们可以在base R做到这一点

iris[[1]] <- iris[[1]] + iris[[2]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM