I am trying to transform four character variables in this dataset into ordered factors. The logic I am trying to apply is shown for the variable "A" below:
df$A = factor(ifelse(df$A %in% c('NOT CONDUCTED AT ALL','RARELY'),'L1',
ifelse(df$A == 'OCCASIONALLY', 'L2',
ifelse(df$A == 'QUITE FREQUENTLY', 'L3', 'L4'))))
df$A = ordered(factor(df$A), levels=c('L1','L2','L3','L4'))
Is there a way to transform all the variables in one shot using the same condition?
# first, make sure each column is an ordered factor with all of the levels
# if the columns of df are character, replace levels(unlist(df)) with unique(unlist(df))
df <- data.frame(lapply(df, factor, ordered=TRUE, levels=levels(unlist(df))))
# create mapping to the new levels following the order
# imposed in the previous step
new.lvl.mapping <- c('L4', 'L2', 'L3', 'L1', 'L1')
# make the replacement using the mapping
data.frame(lapply(df, function(col) new.lvl.mapping[col]))
You could define a function that creates the factor you want and apply that to each of the columns of the data frame that you want at once using sapply().
# some fake data for example
dat <- c("NOT CONDUCTED AT ALL", "RARELY", "OCCASIONALLY", "QUITE FREQUENTLY", "ALWAYS")
df <- data.frame(A=sample(dat, 25, TRUE), B=sample(dat, 25, TRUE), D=rnorm(25))
head(df)
A B D
1 NOT CONDUCTED AT ALL QUITE FREQUENTLY -0.04049165
2 QUITE FREQUENTLY QUITE FREQUENTLY 0.74361906
3 ALWAYS OCCASIONALLY -0.93606555
4 ALWAYS ALWAYS 0.56659322
5 RARELY OCCASIONALLY 0.97216491
6 QUITE FREQUENTLY OCCASIONALLY 0.91125383
# define a function to create a new factor variable
newfac <- function(x, oldval, newval, ordered=TRUE) {
factor(newval[match(x, oldval)], ordered=TRUE)
}
# apply the function to each specified element of the data frame
df[, c("A", "B")] <- sapply(df[, c("A", "B")], newfac,
oldval=c("NOT CONDUCTED AT ALL", "RARELY", "OCCASIONALLY", "QUITE FREQUENTLY", "ALWAYS"),
newval=c("L1", "L1", "L2", "L3", "L4")
)
head(df)
A B D
1 L1 L3 -0.04049165
2 L3 L3 0.74361906
3 L4 L2 -0.93606555
4 L4 L4 0.56659322
5 L1 L2 0.97216491
6 L3 L2 0.91125383
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With