CTPJ
CTPJ

Reputation: 3

Rename multiple levels of a factor, all of which contain the same word

For example:

x1 <- data.frame("ID" = c(1:6), 
                  "Position" = c("Left-Striker", 
                                 "Goalkeeper", 
                                 "Right-Striker",
                                 "Striker",
                                 "Centerback",
                                 "Right-Midfield"))

returns:

      ID       Position
   1  1    Left-Striker
   2  2      Goalkeeper
   3  3   Right-Striker
   4  4         Striker
   5  5      Centerback
   6  6  Right-Midfield

I would like to rename all factors with the word "Striker" inside the factor to "Striker" as the factor name. So the data frame looks like:

      ID       Position
   1  1         Striker
   2  2      Goalkeeper
   3  3         Striker
   4  4         Striker
   5  5      Centerback
   6  6  Right-Midfield

Upvotes: 0

Views: 224

Answers (4)

moodymudskipper
moodymudskipper

Reputation: 47350

This keeps the variable stored as factor and removes the unused levels, in base R :

x1$Position[grepl("Striker",x1$Position)] <- "Striker"
x1$Position <- droplevels(x1$Position)
x1$Position
# [1] Striker        Goalkeeper     Striker        Striker        Centerback     Right-Midfield
# Levels: Centerback Goalkeeper Right-Midfield Striker

You could also do the following for a similar effect (levels might be sorted differently) :

levels(x1$Position)[grepl("Striker",levels(x1$Position))] <- "Striker"
x1$Position
# [1] Striker        Goalkeeper     Striker        Striker        Centerback     Right-Midfield
# Levels: Centerback Goalkeeper Striker Right-Midfield

Upvotes: 2

RobJan
RobJan

Reputation: 1441

Using regex

library(stringr)

x1$Position <- str_replace_all(x1$Position, "[:alpha:]+-?Striker", "Striker")

From stringr

There are a number of pre-built classes that you can use inside []:

  • [:alpha:]: letters.

The + means one or more. The - is in your string.

Upvotes: 1

A. Suliman
A. Suliman

Reputation: 13135

Using gsub we can remove the first word using

x1$Position <- gsub('(^\\w+-)(Striker)','\\2',x1$Position)

  • () used to group expressions
  • (^\\w+-) first group search for one or more word followed with a -
  • (Striker) seconed group search for a literal Striker.
  • \\2 means discard the first group and return the second group which is Striker.

    Upvotes: 1

  • hplieninger
    hplieninger

    Reputation: 3504

    This can also be done with the forcats package:

    forcats::fct_collapse(x1$Position, "Striker" = c("Left-Striker", "Striker", "Right-Striker"))
    

    Upvotes: 1

    Related Questions