user3900661
user3900661

Reputation: 133

sum with unite function tidyr

I was reading through the tidyr documentation. I'm trying to make use of the unite function. Is it possible to use the unite function to sum specified columns? Using the example from the documentation.

mtcars %>%
  unite(vs_am, vs, am)

                   mpg cyl disp  hp drat    wt  qsec vs_am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46   0_1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02   0_1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61   1_1    4    1

I'm trying to figure how to get it so the vs_am isn't just the values combined as characters, rather it would add the values of the columns? Eg. for Mazda RX4, vs_am = 1 (because 0+1 = 1)

Upvotes: 3

Views: 2000

Answers (1)

user3900661
user3900661

Reputation: 133

@Tyler is absolutely correct, unite is not the appropriate function for this task

Here is the code I was looking for mutate(vs_am = vs + am)

   mpg cyl disp  hp drat    wt  qsec vs am gear carb vs_am
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     1
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     1

Upvotes: 4

Related Questions