EconGradKI
EconGradKI

Reputation: 67

Replace strings in R column

I have a dataframe which includes the factor column "Year".

The observations in "Year" looks like this:

 y1995 
 y1995
 y1997
 y1997
 y1999
 y2007
 y1995

etc

I want to eventually convert "Year" into class integer. First, however, I need to drop the "y" from each observations, so that the data takes the form:

 1995 
 1995
 1997
 1997
 1999
 2007
 1995

etc

The years are all in the interval y1995-y2007.

How would I do this?

Upvotes: 1

Views: 90

Answers (3)

Xinting WANG
Xinting WANG

Reputation: 1985

You can try to use function str_replace in package stringr.

Year <- str_replace(Year, "y", "")

Upvotes: 0

anon
anon

Reputation:

This has a simple solution:

temp <- sub("y", "", df$Year)

and if you'd like to replace the old vector:

df$Year <- sub("y", "", df$Year)

and afterwards if you'd like to convert that column into numeric (it will still hold string, although you won't be able to see it right away):

df$Year <- sub("y", "", df$Year)
df$Year <- as.numeric(df$Year)

Upvotes: 2

akrun
akrun

Reputation: 887118

You can try sub to remove the first non-numeric element.

df1$Year <- as.numeric(sub('y', '', df1$Year, fixed=TRUE))

data

df1 <- data.frame(Year=paste0('y', c(1995, 1995, 1997, 1997, 1999, 2007,
           1995)))

Upvotes: 3

Related Questions