Skip to content

Instantly share code, notes, and snippets.

@achekroud
Last active January 10, 2018 18:44
Show Gist options
  • Save achekroud/82ec777c36f7d1298f736768ec6bff04 to your computer and use it in GitHub Desktop.
Save achekroud/82ec777c36f7d1298f736768ec6bff04 to your computer and use it in GitHub Desktop.
sort diagnoses in alphabetical order for each patient
library(tidyverse)
tryThis <- data.frame(x = c("A, B ",
"A",
"B",
"C, A, B",
"C, B",
"C",
"A, B"))
remove_spaces <- function(x){ gsub(x, pattern = " ", replacement = "")}
split_unlist_and_sort <- function(x){ sort(unlist(strsplit(x, split = ","))) }
tryThis <- tryThis %>%
mutate(x = as.character(x)) %>%
mutate(x = remove_spaces(x)) %>%
mutate(x = sapply(x, split_unlist_and_sort)) %>%
mutate(x = sapply(x, function(i) paste(i, sep = ","))) %>%
mutate(has_B <- str_detect(x, pattern = "B"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment