[R] Why is conversion not working?

From: Jonas Malmros <jonas.malmros_at_gmail.com>
Date: Tue, 18 Dec 2007 01:52:51 +0100

I have a data frame, where two last columns - "month" and "year" - are character vectors. The "year" vector is made of two numbers (i.e. "97" for 1997, "07" for 2007, etc)
What I want to do is to create a variable "Year" that is mode numeric and where each record is a four-figure number (1997, 2007,...) I have about 40000 rows in the dataframe, the observations are for 10 years (so there are multiple rows for each year). I tried the following, but the program runs and runs, and if I abort it all the years in "Year" are 1997:

for(i in 1:dim(database)[1]){

    if(database$year[i]>90) {

           database$Year[i] <- as.numeric(database$year[i])+1900 } else {
           database$Year[i] <- as.numeric(database$year[i])+2000

Thanks in advance for explanations.


Jonas Malmros
Stockholm University
Stockholm, Sweden

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 18 Dec 2007 - 00:57:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Dec 2007 - 01:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.