user2165907
user2165907

Reputation: 1451

Change value name in data frame

My data frame (mydf):

PL YEAR
a X2013
a X2012
a X2011
b X2013
b X2012
b X2011

How could we remove all the "X" in the YEAR column?

I tried without success :

mydf$YEAR <- as.character(mydf$YEAR)
mydf$YEAR[mydf$YEAR == "X"] <- NULL  # or just "" instead of NULL

Upvotes: 2

Views: 79

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

You can use gsub to remove the unwanted X from the YEAR column.

> mydf
##   PL  YEAR
## 1  a X2013
## 2  a X2012
## 3  a X2011
## 4  b X2013
## 5  b X2012
## 6  b X2011
> mydf$YEAR <- gsub("X", "", mydf$YEAR)     ## or gsub("[^0-9]", "", mydf$YEAR)
> mydf
##   PL YEAR
## 1  a 2013
## 2  a 2012
## 3  a 2011
## 4  b 2013
## 5  b 2012
## 6  b 2011

As pointed out in the comments, it can also be done with substring

> mydf$YEAR <- substring(mydf$YEAR, 2, 5)   ## or substr(...) returns the same

Upvotes: 3

Related Questions