[R] Data handling
arun
smartpink111 at yahoo.com
Tue Oct 15 17:13:42 CEST 2013
Try:
op <- options(digits.secs=4)
TimeCC <- as.POSIXct(paste0(paste(teste[,1],teste[,2]), sub("^0","",teste[,3])),format="%m/%d/%y %H:%M:%OS")
options(op) #reset
A.K.
On Tuesday, October 15, 2013 10:29 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