Skip to content

Instantly share code, notes, and snippets.

@solmos
Created October 20, 2020 14:05
Show Gist options
  • Save solmos/0e4323422dc67be03891a035042aef83 to your computer and use it in GitHub Desktop.
Save solmos/0e4323422dc67be03891a035042aef83 to your computer and use it in GitHub Desktop.
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