基于这个问题:dplyr: how to reference columns by column index rather than column name using mutate?
我想使用mutate的源和目标的列索引进行变异:
iris %>% head %>% mutate(.[[1]] = .[[1]] + .[[2]])
给出:
Error: unexpected '=' in "iris %>% head %>% mutate(.[[1]] =".
但是,以下工作:
iris %>% head %>% mutate(sum = .[[1]] + .[[2]])
答案 0 :(得分:2)
我们可以在base R
iris[[1]] <- iris[[1]] + iris[[2]]