-
Notifications
You must be signed in to change notification settings - Fork 1
/
change_name.R
33 lines (22 loc) · 949 Bytes
/
change_name.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#change to new names in conditional logic column
library(tidyverse)
dataDic <- read_csv('data_dic_new.csv')
namematch <- read_csv('namesmatch.csv')
namematch <- select(namematch, 1:2)
#create named list data dictionary to replace old names with new ones
nameDic <- setNames(paste0('\\[', namematch$name_post_v20, '\\]'), paste0('\\[', namematch$name_pre_v20, '\\]'))
#nameDic <- c("\\[Context1\\]" = "\\[since-activity-monitor\\]", "\\[Thoughts2\\]" = "\\[since-rest-fell-asleep\\]")
#replace strings
newColumn <- dataDic %>%
pull(`Branching Logic (Show field only if...)`) %>%
ifelse(is.na(.), 'NA', .) %>%
str_c(collapse = '---') %>%
str_replace_all(nameDic) %>%
str_split(pattern = '---', simplify = T) %>%
ifelse(. == 'NA', NA, .) %>%
as.character()
#replace column
dataDic <- dataDic %>%
mutate(`Branching Logic (Show field only if...)` = newColumn)
#save data dic
write_csv(dataDic, 'data_dic_new.csv', na = '')