Skip to content

Commit

Permalink
probably fixes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 23, 2018
1 parent b8a8d2b commit a7e4b47
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions R/rec.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,24 +422,30 @@ rec_helper <- function(x, recodes, as.num, var.label, val.labels) {
if (!is.null(dir.label) && !sjmisc::is_empty(dir.label)) val_lab <- dir.label

# remove possible direct labels from recode pattern
recodes <- gsub(pattern = "\\[([^\\[]*)\\]", replacement = "", x = recodes, perl = T)
recodes <- gsub(
pattern = "\\[([^\\[]*)\\]",
replacement = "",
x = recodes,
useBytes = TRUE,
perl = TRUE
)

# prepare and clean recode string
# retrieve each single recode command
rec_string <- unlist(strsplit(recodes, ";", fixed = TRUE))

# remove spaces
rec_string <- gsub(" ", "", rec_string, fixed = TRUE)
rec_string <- gsub(" ", "", rec_string, useBytes = TRUE, fixed = TRUE)

# remove line breaks
rec_string <- gsub("\n", "", rec_string, fixed = F)
rec_string <- gsub("\r", "", rec_string, fixed = F)
rec_string <- gsub("\n", "", rec_string, useBytes = TRUE, fixed = F)
rec_string <- gsub("\r", "", rec_string, useBytes = TRUE, fixed = F)

# replace min and max placeholders
rec_string <- gsub("(min)\\b", as.character(min_val), rec_string, perl = TRUE)
rec_string <- gsub("(lo)\\b", as.character(min_val), rec_string, perl = TRUE)
rec_string <- gsub("(max)\\b", as.character(max_val), rec_string, perl = TRUE)
rec_string <- gsub("(hi)\\b", as.character(max_val), rec_string, perl = TRUE)
rec_string <- gsub("(min)\\b", as.character(min_val), rec_string, useBytes = TRUE, perl = TRUE)
rec_string <- gsub("(lo)\\b", as.character(min_val), rec_string, useBytes = TRUE, perl = TRUE)
rec_string <- gsub("(max)\\b", as.character(max_val), rec_string, useBytes = TRUE, perl = TRUE)
rec_string <- gsub("(hi)\\b", as.character(max_val), rec_string, useBytes = TRUE, perl = TRUE)

# retrieve all recode-pairs, i.e. all old-value = new-value assignments
rec_pairs <- strsplit(rec_string, "=", fixed = TRUE)
Expand Down

0 comments on commit a7e4b47

Please sign in to comment.