jotrocken
jotrocken

Reputation: 2333

R - How to rename factor levels using a function?

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

Answers (2)

Terru_theTerror
Terru_theTerror

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

Paul
Paul

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

Related Questions