Reputation: 73
I am trying to create a data.frame that contains 'datetime' and 'numeric' fields and insert data into it. Being new to R, and having a 'novice' level of skill, I'm not sure how to format it...
Here's the code I'm trying to work. Note the 'commented out' sections are variations. I am getting all kinds of strange output for the 'Time' field.
#df.02 <- data.frame(Time = as.Date(character())
#df.02 <- data.frame(Time = as.Date(numeric())
#df.02 <- data.frame(Time = as.POSIXct(strptime(character())
df.02 <- data.frame(Time = as.POSIXct(strptime(character())
#df.02 <- data.frame(Time = as.Date(character())
, Open = numeric()
, High = numeric()
, Low = numeric()
, Close = numeric()
, Volume = numeric()
, stringsAsFactors = FALSE)
time <- as.Date("01.02.2014 00:00:00.000")
#time <- as.Date("01.02.2014 00:00:00.000", format = "%d.%m.%Y %H:%M:%S")
#time <- strptime("01.02.2014 00:00:00.000", "%d.%m.%Y %H:%M:%OS")
#time <- as.POSIXct("01.02.2014 00:00:00.000", format = "%d.%m.%Y %H:%M:%S")
open <- 1111.111
high <- 2222.222
low <- 3333.333
close <- 4444.444
volume <- 0.0000
df.02 <- cbind(Time = time, Open = open, High = high, Low = low, Close = close, Volume = volume)
df.02
Any help would be greatly appreciated!
Upvotes: 0
Views: 250
Reputation: 1928
I agree with jarfa. lubridate is a GREAT package for this. For example, you could do this:
library(lubridate)
time <- mdy_hms("01.02.2014 00:00:00.000")
and now, if you check the class of "time", it is "POSIXct" "POSIXt".
Upvotes: 2