Last active
January 10, 2018 18:44
-
-
Save achekroud/82ec777c36f7d1298f736768ec6bff04 to your computer and use it in GitHub Desktop.
sort diagnoses in alphabetical order for each patient
This file contains hidden or 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(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