dears...
I need to get data in a data.frame considering date and time formats. For it I've created the data.frame structure like this:
df <- data.frame(action = character(),
date = as.Date(character(), format = "%Y%m%d"),
time = charcter()) #I didn't find a way to format time :(
I am reading the file using read_lines command and when I get the data from file I manage to format the date like this:
# Date position in line is 44-51
my_date <- as.Date(as.character(str_sub(line, 44, 51)), format = "%Y%m%d")
#Time position in line is 52-57
my_time <- paste(str_sub(l,52,53),":",str_sub(l,54,55),":",str_sub(l,56,57), sep="")
If I print my_date and my_time, the formats are perfect, but when I assign to data.frame it messes the format. I wish to use the date and time data in data.frame to compute information. It would be good if I have formats to compute the number of days, amount of hours, for example.
#dput of my_date
.structue(15431, class = "Date")
#dput of my_time
"18:59:39"
How can you see, I've put my_date in date format, but it messes when I assign in data.frame. About my_time I just put in character format to became visual, but I don't have an idea to format time in HH:MM:SS.
Tks for some help.
Hi, Ian First of all, thanks for spending your time to help me. The problem continues when I assign the value to data.frame. I think I am doing something wrong there. See...
When I collect the date and format:
[1] "2012-04-01"
[1] "18:59:30"
[1] "2012-04-01 18:59:00 -03"
structure(1333317540, class = c("POSIXct", "POSIXt"), tzone = "")
The first line is the result for the print command over date variable formatted by
date <- as.Date(as. character(str_sub(l, 44,51), format = "%Y%m%d").
It's Ok! The second line is the result for the print command over time variable formatted by
time <- paste(str_sub(l,52,53),":",str_sub(l,54,55),":",str_sub(l,56,57), sep="")
It's OK!
The 3dr line is the result for print command over date_time variable formatted by
date_time <- as.POSIXct(paste(date, time), format="%Y-%m-%d %H:%M%OS")
It's perfect, according to your hint!
The 4th line is the result for dput(date_time), and the value passed to data.frame is 1333317540 3dr line instead.
I am declaring the blank data.frame like this:
df <- data.frame(ass_source = character(),
eot_ass_source = character(),
area_ass_source = character(),
#date = as.Date(character(), format = "%Y%m%d"),
#time = character(),
date_time = as.POSIXct(character(), format = "%Y-%m%d %H:M%OS"))
And I am inputting data like this (into a for loop):
df <- rbind(df, list(ass_source=ass_source,
eot_ass_source=eot_ass_source,
area_ass_source=area_ass_source,
#date=as.Date(as.character(date), format= "%Y%m%d"),
#time=time,
date_time=date_time))