Reputation: 1225
I'm practicing data manipulation with iris
. I'm trying to sum the Sepal.Width
and Petal.Width
to one column. I referred to this question In R, how to replace values in multiple columns with a vector of values equal to the same width?
got the sum but the Sepal.Width
and Petal.Width
are not removed. Please help. Thanks a lot.
My code was
iris <- iris %>% mutate(rowSums(iris[,c(2,4)]))
Upvotes: 0
Views: 47
Reputation: 13680
Use the native dplyr
way to sum columns, and as specified in ?mutate
, Use NULL to drop a variable.
:
iris %>%
mutate(Sepal.Length + Petal.Length, Sepal.Length = NULL, Petal.Length = NULL)
Upvotes: 1