[R] Convert a column of numbers to a column of strings

Erik Iverson eriki at ccbr.umn.edu
Mon Feb 1 20:15:11 CET 2010


Also, although you don't need it to accomplish what you want, you were 
probably looking for the as.character function instead of toString...

Tam Le wrote:
> Hello,
> 
> Please excuse me if this question has been asked before. I'm new to R, and
> have been trying to google the answers without any success.
> 
> I would like to convert a set of date and time into R date-time class. Right
> now, the dates and times are in integer format, so I first need to convert
> them into string, and then to R date-time using strptime. However, I have a
> problem converting them from integer to string:
> 
> data:
> 
> Date Time
> 20091209 1200
> 20091209 1500
> 20091209 1800
> ....
> 20091210 800
> 20091210 1000
> 20091210 1600
> ....
> 
> I used:
> 
> tempdate=toString(Date)
> day=substr(tempdate, 7, 8)
> month=substr(tempdate, 5, 6)
> year=substr(tempdate, 1, 2)
> rDate=strptime(paste(year, month, day, sep="-"))
> 
> the 1st command does not create a new column with string dates in it, it
> creates a single row that has all the dates in a single string, like this:
> "20091209, 20091209, .... 20091210, 20091210"
> "day" ends up being 1 number, 09, instead of a column of days. I ended up
> having all rDate=2009/12/09, which is the first row.
> 
> Could you please tell me how to do this correctly?
> 
> Thanks,
> Tam
> 
>



More information about the R-help mailing list