rsumcol

What R function can I use to sum two columns and save the new variable as a new column?


I summed two colums, Sweden.pymen and Swede.pywomen, and created Sweden.pyall - the entries are numbers. This is the code i used:

Sweden_py.all <- (Sweden$py.men + Sweden$py.women)

I got this as a result: enter image description here

but i need to have it stored as a column with one value under the other and not all in the same cell.

Any help would be great! Cheers


Solution

  • I think you want dplyr::mutate for this:

    require(dplyr)
    
    Sweden <- Sweden %>%
    mutate(py.all = py.men + py.women)
    

    You should check out this intro for basic data wrangling: https://dplyr.tidyverse.org/articles/dplyr.html