[R] Data handling
jim holtman
jholtman at gmail.com
Tue Oct 15 18:45:10 CEST 2013
Try this; your time is converted back to a character string if you
want to show the fractional part.
> x <- read.table(text = " Date Time Fraction
+ 06/19/13 22:15:39 0.3205
+ 06/19/13 22:15:44 0.3205
+ 06/19/13 22:15:49 0.3205
+ 06/19/13 22:15:54 0.3205
+ 06/19/13 22:15:59 0.3205
+ 06/19/13 22:16:09 0.3205", as.is = TRUE, header = TRUE)
> x$newTime <- as.POSIXct(
+ paste0(x$Date, ' ', x$Time , '.', substring(x$Fraction, 3))
+ , format = "%m/%d/%y %H:%M:%OS"
+ )
> x$formatted <- format(x$newTime, format = "%m/%d/%y %H:%M:%OS4")
>
>
>
> x
Date Time Fraction newTime formatted
1 06/19/13 22:15:39 0.3205 2013-06-19 22:15:39 06/19/13 22:15:39.3204
2 06/19/13 22:15:44 0.3205 2013-06-19 22:15:44 06/19/13 22:15:44.3204
3 06/19/13 22:15:49 0.3205 2013-06-19 22:15:49 06/19/13 22:15:49.3204
4 06/19/13 22:15:54 0.3205 2013-06-19 22:15:54 06/19/13 22:15:54.3204
5 06/19/13 22:15:59 0.3205 2013-06-19 22:15:59 06/19/13 22:15:59.3204
6 06/19/13 22:16:09 0.3205 2013-06-19 22:16:09 06/19/13 22:16:09.3204
>
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Tue, Oct 15, 2013 at 10:27 AM, Raoni Rodrigues
<caciquesamurai at gmail.com> wrote:
> Hello all,
>
> I'm having a problem with data handling. My input data is (dput in the
> after the signature):
>
> Date Time Fraction
> 06/19/13 22:15:39 0.3205
> 06/19/13 22:15:44 0.3205
> 06/19/13 22:15:49 0.3205
> 06/19/13 22:15:54 0.3205
> 06/19/13 22:15:59 0.3205
> 06/19/13 22:16:09 0.3205
>
> Date in format month/day/year, Time in HH:MM:SS and fraction represents the
> fractions of seconds. I need to have a vector in a format year-month-day
> hh:mm:ss.0000. Or, in format: format = "%F %H:%M:%OS4", as POSIXct class.
>
> I made the the conversion step-by-step to have sure that nothing is missed
> in the way:
>
>> options (digits.sec = 4)
>> getOption ("digits.sec")
> [1] 4
>> teste$Date1 = as.Date (teste$Date, format = "%m/%d/%y")
>> class (teste$Date1)
> [1] "Date"
>> teste$Fraction = sub ("0.", "", teste$Fraction)
>> teste$TimeC = paste (teste$Time, teste$Fraction, sep = ".")
>> teste$TimeCC = paste (teste$Date1, teste$TimeC)
>
>> head (teste)
> Date Time Fraction Date1 TimeC TimeCC
> 1 06/19/13 22:15:39 .325 2013-06-19 22:15:39.325 2013-06-19
> 22:15:39.3205
> 2 06/19/13 22:15:44 .325 2013-06-19 22:15:44.325 2013-06-19
> 22:15:44.3205
> 3 06/19/13 22:15:49 .325 2013-06-19 22:15:49.325 2013-06-19
> 22:15:49.3205
> 4 06/19/13 22:15:54 .325 2013-06-19 22:15:54.325 2013-06-19
> 22:15:54.3205
> 5 06/19/13 22:15:59 .325 2013-06-19 22:15:59.325 2013-06-19
> 22:15:59.3205
> 6 06/19/13 22:16:09 .325 2013-06-19 22:16:09.325 2013-06-19
> 22:16:09.3205
>
> So far so well. The problem is when I tried to convert to POSIXct class. If
> I use just:
>
> teste$TimeCC = format (teste$TimeCC, format = "%F %H:%M:%OS4")
> teste$TimeCC = as.POSIXct (teste$TimeCC)
>
> I lost the fraction of seconds. If I use:
>
> teste$TimeCC = as.POSIXct(strptime (teste$TimeCC, format = "%F %H:%M:%OS4"))
>
> I lost all information and get just <NA>.
>
> Thanks in advanced,
>
> --
> Raoni Rosa Rodrigues
> Research Associate of Fish Transposition Center CTPeixes
> Universidade Federal de Minas Gerais - UFMG
> Brasil
> rodrigues.raoni at gmail.com
>
> dput of input data
>
> structure(list(Date = c("06/19/13", "06/19/13", "06/19/13", "06/19/13",
> "06/19/13", "06/19/13"), Time = c("22:15:39", "22:15:44", "22:15:49",
> "22:15:54", "22:15:59", "22:16:09"), Fraction = c("0.3205", "0.3205",
> "0.3205", "0.3205", "0.3205", "0.3205")), .Names = c("Date",
> "Time", "Fraction"), row.names = c(NA, 6L), class = "data.frame")
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list