pluke
pluke

Reputation: 4346

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

Upvotes: 9

Views: 9556

Answers (1)

akrun
akrun

Reputation: 887048

We can do this in base R

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

Upvotes: 2

Related Questions