Created
October 20, 2020 14:05
-
-
Save solmos/0e4323422dc67be03891a035042aef83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
## Create similar but simpler data frame to yours | |
df <- data.frame( | |
scenario = rep(1:3, 2), | |
year = rep(2010:2011, each = 3), | |
age_25_29 = rnorm(6), | |
age_30_34 = rnorm(6), | |
age_35_39 = rnorm(6) | |
) | |
## Subset the columns you want to sum over and use apply over margin 1. See ?apply | |
row_sum <- apply(df[, 3:5], 1, sum) | |
## Add new column with sums to the original data frame | |
df$row_sum <- row_sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment