Let's say that we have the following dataframe:
data1 <- data.frame(S1 = sample(c("A", "B", "C"), size = 20, replace = TRUE),
S2 = sample(c("A", "B", "C"), size = 20, replace = TRUE),
number = rnorm(20, 10, 100))
There's also this variable:
scenario <- "scenario1" # can take one of thre values: 'scenario1', 'scenario2', 'scenario3'
There are also the following mappings. Please note that level names in different scenarios can't be handled automatically, eg with a regex:
changer_1 <- c("A"="a1", "B"="b1", "C"="c1")
changer_2 <- c("A"="something", "B"="completely", "c"="different")
changer_3 <- c("A"="z2", "B"="d3", "c"="p14")
I want to rename the factor levels based on the scenario
's value (condition). What I've come up so far is a rather primitive if/else
block that checks the condition and then uses one of the changer
s:
if(scenario=="scenario1"){
data1$S1 <- revalue(data1$S1, changer_1)
data1$S2 <- revalue(data1$S2, changer_1)
} else if (scenario=='scenario2'){
data1$S1 <- revalue(data1$S1, changer_2)
data1$S2 <- revalue(data1$S2, changer_2)
} else {
data1$S1 <- revalue(data1$S1, changer_3)
data1$S2 <- revalue(data1$S2, changer_3)
}
I wonder whether there is other (more R-like) way to do this?
One way to do this is to build a list with all the "scenario"'s and their mappings
change_list <- list(scenario1 = c("A"="a1", "B"="b1", "C"="c1"),
scenario2 = c("A"="something", "B"="completely", "C"="different"),
scenario3 = c("A"="z2", "B"="d3", "C"="p14"))
Now create a function which returns the values as per scenario
get_values <- function(change_list, scenario, x) {
change_list[[scenario]][x]
}
and now you can call the function
get_values(change_list, "scenario1", data1$S1)
# A C B B B C B B C A A C C A B ...
#"a1" "c1" "b1" "b1" "b1" "c1" "b1" "b1" "c1" "a1" "a1" "c1" "c1" "a1" "b1" ...
get_values(change_list, "scenario2", data1$S1)
# A C B B B C ..
# "something" "different" "completely" "completely" "completely" "different"..
In this way with only one function you can change all the values in any column.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.