887
887

Reputation: 619

How to Rename Certain Observations in Column Based on Certain String Value?

I'm looking to rename certain values of my column based upon a certain string. My current data resembles this example:

PlayerID

Hank_Aaron+7
Babe Ruth+5
MMM + 7
Willie Mayes+1
MMM + 3

I would like to rename all observations that start with "MMM" to be just "MMM". For example, I want the above table to ultimately look like this:

PlayerID

Hank_Aaron+7
Babe Ruth+5
MMM
Willie Mayes+1
MMM

I also need to keep the column in the same dataframe so that I can use it for regressions. Thank you in advance!

Upvotes: 2

Views: 889

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388982

We can use startsWith :

df$PlayerID[startsWith(df$PlayerID, 'MMM')] <- 'MMM'

We can also use this in replace :

df$PlayerID <- replace(df$PlayerID, startsWith(df$PlayerID, 'MMM'), 'MMM')
df
#        PlayerID
#1   Hank_Aaron+7
#2    Babe Ruth+5
#3            MMM
#4 Willie Mayes+1
#5            MMM

data

df <- structure(list(PlayerID = c("Hank_Aaron+7", "Babe Ruth+5", "MMM + 7", 
"Willie Mayes+1", "MMM + 3")), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 0

akrun
akrun

Reputation: 887118

With grep, find the position index of 'MMM' string, extract those, then do an assignment (assuming 'PlayerID' is character class and not factor class)

df1$PlayerID[grep("^MMM", df1$PlayerID)] <- "MMM"

Upvotes: 2

Related Questions