Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 15, 2021 11:33
Show Gist options
  • Select an option

  • Save simonthompson99/38d77ee66fbdb1463419d43071e685be to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/38d77ee66fbdb1463419d43071e685be to your computer and use it in GitHub Desktop.
[Concatenate flag columns] Concatenate flag columns into a single column #r
# take a load of boolean flag columns (names beginning with f_) and produce a column that lists which flags the row is true for
concat_flags <- function(x){
separator = ';'
a <- paste0(names(x)[as.logical(x)], sep = separator)
ifelse(a == separator, NA, a)
}
data$concat_flags <- apply(data[,colnames(data)[startsWith(colnames(data), "f_")]], 1, concat_flags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment