How to remove a character (asterisk) in column values in r?

Viewed 14556

so I have a dataframe that looks like this but has 6k rows:

AWC, LocationID
333, *Yukon
485, *Lewis Rich
76, *Kodiak
666, Kodiak
54, *Rays

I would like to remove the asterisks from the LocationID values if thats possible and just keep the original name. So *Yukon -> Yukon. If thats not possible, could you help me with a way to rename a column value? I'm new to r.

3 Answers

This can be achieved using the mutate verb from the tidyverse package. Which in my opinion is more readable. So, to exemplify this, I create a dataset called DT with a focus on the LocationID to mimic the problem at hand.

library(tidyverse)
DT <- data.frame('AWC'= c(333, 485, 76, 666, 54), 
                 'LocationID'= c('*Yukon','*Lewis Rich', '*Kodiak', 'Kodiak', '*Rays'))

head(DT)
  AWC  LocationID
1 333      *Yukon
2 485 *Lewis Rich
3  76     *Kodiak
4 666      Kodiak
5  54       *Rays

In what follows, mutate allows one to alter the column content, gsub does the desired substitution (of * with ""), keeping the data cleaning flow followable.

DT <- DT %>% mutate(LocationID = gsub("\\*", "", LocationID))
head(DT)
  AWC LocationID
1 333      Yukon
2 485 Lewis Rich
3  76     Kodiak
4 666     Kodiak
5  54       Rays

NOTE that \\ is placed before * as the escape character

Related