1

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?

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
EconGradKI
  • 67
  • 4
  • I'm struggling if to close this a dupe of this http://stackoverflow.com/questions/13594223/number-values-include-comma-how-do-i-make-these-numeric – David Arenburg Apr 07 '15 at 11:17

3 Answers3

3

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)))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

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)
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
0

You can try to use function str_replace in package stringr.

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

dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
Xinting WANG
  • 1,905
  • 2
  • 15
  • 20