# NOT RUN {
data(efc)
table(efc$e42dep, useNA = "always")
# replace NA with 5
table(rec(efc$e42dep, rec = "1=1;2=2;3=3;4=4;NA=5"), useNA = "always")
# recode 1 to 2 into 1 and 3 to 4 into 2
table(rec(efc$e42dep, rec = "1,2=1; 3,4=2"), useNA = "always")
# keep value labels. variable label is automatically preserved
library(dplyr)
efc %>%
select(e42dep) %>%
rec(rec = "1,2=1; 3,4=2",
val.labels = c("low dependency", "high dependency")) %>%
str()
# works with mutate
efc %>%
select(e42dep, e17age) %>%
mutate(dependency_rev = rec(e42dep, rec = "rev")) %>%
head()
# recode 1 to 3 into 4 into 2
table(rec(efc$e42dep, rec = "min:3=1; 4=2"), useNA = "always")
# recode 2 to 1 and all others into 2
table(rec(efc$e42dep, rec = "2=1; else=2"), useNA = "always")
# reverse value order
table(rec(efc$e42dep, rec = "rev"), useNA = "always")
# recode only selected values, copy remaining
table(efc$e15relat)
table(rec(efc$e15relat, rec = "1,2,4=1; else=copy"))
# recode variables with same category in a data frame
head(efc[, 6:9])
head(rec(efc[, 6:9], rec = "1=10;2=20;3=30;4=40"))
# recode multiple variables and set value labels via recode-syntax
dummy <- rec(efc, c160age, e17age,
rec = "15:30=1 [young]; 31:55=2 [middle]; 56:max=3 [old]")
frq(dummy)
# recode variables with same value-range
lapply(
rec(efc, c82cop1, c83cop2, c84cop3, rec = "1,2=1; NA=9; else=copy"),
table,
useNA = "always"
)
# recode character vector
dummy <- c("M", "F", "F", "X")
rec(dummy, rec = "M=Male; F=Female; X=Refused")
# recode numeric to character
rec(efc$e42dep, rec = "1=first;2=2nd;3=third;else=hi")
# recode non-numeric factors
data(iris)
table(rec(iris, Species, rec = "setosa=huhu; else=copy"))
# recode floating points
table(rec(iris, Sepal.Length, rec = "lo:5=1;5.01:6.5=2;6.501:max=3"))
# preserve tagged NAs
library(haven)
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1),
c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
"Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
# get current value labels
x
# recode 2 into 5; Values of tagged NAs are preserved
rec(x, rec = "2=5;else=copy")
na_tag(rec(x, rec = "2=5;else=copy"))
# use select-helpers from dplyr-package
rec(efc, contains("cop"), c161sex:c175empl, rec = "0,1=0; else=1")
# }
Run the code above in your browser using DataLab