Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 8, 2023
1 parent 427c392 commit 28f71e5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions R/utils_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@
"Not all factor levels had a matching value label. Non-matching levels were preserved."
)
}
# when length of value_labels and levels_in_labs is identical, we can simply
# replace the levels with the value labels. Else, we need to select only those
# value labels that have a matching level (labs_in_levels)
if (length(value_labels) == length(levels_in_labs)) {
# when length of value_labels and levels_in_labs is identical, we can simply
# replace the levels with the value labels. This makes sure than levels or
# value labels, which are not sorted or not sequentially numbered, match.
# Example:
# x <- c(5, 5, 1, 3, 1, 7)
# attr(x, "labels") <- c(no = 7, yes = 1, maybe = 3, `don't know` = 5)
# to_factor(x, labels_to_levels = TRUE)
levels(x)[levels_in_labs] <- names(value_labels)
} else {
# else, we need to select only those value labels that have a matching level
# (in labs_in_levels). This is required when not all values that have labels
# appear in the data.
levels(x)[levels_in_labs] <- names(value_labels[labs_in_levels])
}
attr(x, "labels") <- NULL
Expand Down

0 comments on commit 28f71e5

Please sign in to comment.