Thursday, 19 November 2009

R and Datetimes and stuff

You may have figured out at this point that these posts' primary purpose is so I don't have to figure out this stuff again if I stop using R for 6 months.

Anyway, if you've got some strings and you want dates, use
as.Date(x)


But if you want datetimes (i.e. don't want to lose the time component), use
as.POSIXct(x)


Also, subset is nice:
subset(dat1, (t > '2009-01-01') & (t < '2009-01-05'))


Create a frame:
#make up some data
x = seq(-2*pi, 2*pi, by = 0.05)
#create the frame
datx = data.frame(x=x)
#add another column
datx$sinx = sin(datx$x)
#show the columns
names(datx)
#gives: [1] "x" "sinx"

See also http://www.statmethods.net/input/datatypes.html for a nice brief intro to R's different structures.

No comments:

Post a Comment