0

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))
Beto
  • 21
  • 4
  • Hi Beto, welcome to Stack Overflow. Could you provide `dput(my_date[1:10])` and `dput(my_time[1:10])`? You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jun 15 '20 at 22:28
  • I've just shown the dput result. TKS! – Beto Jun 15 '20 at 22:46

1 Answers1

0

It seems like your my_date is of class Date and you my_time is a character string. One convenient option is to use the POSIXct class which combines the date and time into one column. To do this, simply paste the two together and use as.POSIXct. It will be vectorized, you can do multiple at once.

my_date <- structure(15431, class = "Date")
my_time <- "18:59:39"
data.frame(time = as.POSIXct(paste(my_date,my_time)))
                 time
1 2012-04-01 18:59:39
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • My problem continues in assigning values to data.frame. It's the same of format as.Date, outside of data.frame it works, but when I assign value for the data.frame it messes format. – Beto Jun 16 '20 at 14:16