Reputation: 2333
Assume I have a function extract_surname()
which turns "Surname, Firstname" into "Surname". How can use this function to change the levels of a factor variable?
The distinct levels of that factor are not known in advance, just that they fulfill the described naming pattern. AFAIK dplyr's recode()
cannot be applied here, because it requires an explicit list of all substitutions to be made. Or is there a way to generate such a list?
Upvotes: 1
Views: 638
Reputation: 5017
A data.frame df
with 2 names as factor
df <- data.frame('NAME' = c('Bob', 'Anna'))
levels(df$NAME)
[1] "Anna" "Bob"
I add a new name as.factor
levels(df$NAME)<-c(levels(df$NAME),"Terru_theTerror")
levels(df$NAME)
[1] "Anna" "Bob" "Terru_theTerror"
You can use your function extract_surname()
in the attribution of new levels.
Upvotes: 0
Reputation: 9087
You can set the levels in the following way
x <- as.factor(head(letters))
x
# [1] a b c d e f
# Levels: a b c d e f
levels(x) <- toupper(levels(x))
x
# [1] A B C D E F
# Levels: A B C D E F
Upvotes: 1