[R] Convert date to integer

Dieter Menne dieter.menne at menne-biomed.de
Tue Mar 31 11:45:00 CEST 2009


thoeb <t.hoebinger <at> gmail.com> writes:

> Hello, I have a dataframe containing dates, times and other parameters. The
> times have the format "h:m", e.g. 13:00 or 5:30, R classes them as factors.

Probably you have read in the data from a file with read.table; check
stringsAsFactors in the docs to avoid the conversion from the beginning.


> I want to change the times to integers e.g. 13:00 -> 1300. I tried to use
> "chron" to create a chronological object, but it didn't work for the times
> (yust for the dates). 
> 
If that's all (no NA?) a simple replace might work

df = data.frame(tstr=c("13:00","5:30"))
df$tint = as.integer(gsub(":","",as.character(df$tstr)))


Dieter




More information about the R-help mailing list