Little Bee
Little Bee

Reputation: 1225

Summing several columns and keep the summed value only

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

Answers (1)

GGamba
GGamba

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

Related Questions