Giuseppe Minardi
Giuseppe Minardi

Reputation: 411

Rename all other levels to "Other"

I have a dataframe containing all the calls that I have done in the last year. Under the column "Name" there are the names of the people in my contact list. In R this column contains 30 factors, I want to have only 3 factors: Mom, Dad, BestFriend and Others. I'm using this snippet:

library(plyr) call$Name <- mapvalues(call$Name, from = 'Mikey Mouse', to = 'BFF') call$Name <- mapvalues(call$Name, from = c('Rocky Balboa','Uma Thurman'), to = c('Dad','Mom'))

How can I rename all other levels aside those 3 to Other?

Upvotes: 1

Views: 71

Answers (2)

dave-edison
dave-edison

Reputation: 3736

There is also the fct_other() function in the forcats package for doing exactly this. Using the data akrun provided we could simply do:

library(forcats)

call$Name <- fct_other(call$Name, keep = nm1)

Upvotes: 2

akrun
akrun

Reputation: 887951

We can first create a level 'Others' (assuming it is a factor), assign the levels that are not %in% the vector of levels ('nm1') to 'Other'

levels(call$Name) <- c(levels(call$Name), 'Other'))
levels(call$Name)[!levels(call$Name %in% nm1] <- 'Other'

Or another option is recode from dplyr which also have the .default option to specify other levels that are not in the vector to a given value

library(dplyr)
recode(call$Name, `Mikey Mouse` =  'BFF', `Rocky Balboa` = 'Dad',
    `Uma Thurman` = 'Mom', .default = 'Other')

data

set.seed(24)
call <- data.frame(Name = sample(c('Mikey Mouse', 'Rocky Balboa',
  'Uma Thurman', 'Richard Gere', 'Rick Perry'), 25, replace = TRUE))
nm1 <- c('Mickey Mouse', 'Rocky Balboa', 'Uma Thurman')

Upvotes: 2

Related Questions