I am trying to convert SPLUS
data with timeSpan
class to R
data with POSIXlt
class. I looked into lubridate
package, but could not find a way to do it. R
is unable to detect timeSpan
class, so it spits error when I tried to create a variable with the data.
The lubridate
package help file ?'lubridate-package'
says that
Lubridate distinguishes between moments in time (known as instants) and spans of time (known as time spans, see Timespan-class). Time spans are further separated into Duration-class, Period-class and Interval-class objects.
Also I looked for help from this article on lubridate
written by the package developer, but could not find a way to solve it. Is it possible to convert the timeSpan
class of SPLUS
to posixlt
class in R
?
SPLUS Data:
"span" = new("timeSpan", .Data = list(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7,
7, 30, 30, 91, 91, 365, 1826, 9131, 36525),
c(1, 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 5000, 15000, 60000, 60000,
300000, 300000, 900000, 900000, 3600000, 10800000, 10800000, 21600000,
0, 0, 0, 37800000, 37800000, 27000000, 27000000, 21600000, 21600000,
21600000, 0)),
.Data.names = c("julian.day", "milliseconds"),
.Data.classes = new("CLASS",c("integer", "integer")),
format = "%dd %Hh %Mm %Ss %NMS")
Appearance of data after evaluation on SPLUS workbench:
span
[1] 0d 0h 0m 0s 1MS 0d 0h 0m 0s 1MS 0d 0h 0m 0s 2MS
[4] 0d 0h 0m 0s 5MS 0d 0h 0m 0s 10MS 0d 0h 0m 0s 25MS
[7] 0d 0h 0m 0s 50MS 0d 0h 0m 0s 100MS 0d 0h 0m 0s 250MS
[10] 0d 0h 0m 0s 500MS 0d 0h 0m 1s 0MS 0d 0h 0m 5s 0MS
[13] 0d 0h 0m 15s 0MS 0d 0h 1m 0s 0MS 0d 0h 1m 0s 0MS
[16] 0d 0h 5m 0s 0MS 0d 0h 5m 0s 0MS 0d 0h 15m 0s 0MS
[19] 0d 0h 15m 0s 0MS 0d 1h 0m 0s 0MS 0d 3h 0m 0s 0MS
[22] 0d 3h 0m 0s 0MS 0d 6h 0m 0s 0MS 1d 0h 0m 0s 0MS
[25] 7d 0h 0m 0s 0MS 7d 0h 0m 0s 0MS 30d 10h 30m 0s 0MS
[28] 30d 10h 30m 0s 0MS 91d 7h 30m 0s 0MS 91d 7h 30m 0s 0MS
[31] 365d 6h 0m 0s 0MS 1826d 6h 0m 0s 0MS 9131d 6h 0m 0s 0MS
[34] 36525d 0h 0m 0s 0MS
Error on R console:
Error in getClass(Class, where = topenv(parent.frame())) :
“timeSpan” is not a defined class
Taking the core list
.Data
object out of the S object, you can then manipulate it into an R difftime
, which can then be added/subtracted from a POSIXct/lt
or Date
object:
out <- Reduce(`+`,
Map(as.difftime, Map(`/`, x, c(1,1000)), units=list("days","secs"))
)
units(out) <- "days"
round(out,4)
#Time differences in days
# [1] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
# [7] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
#[13] 0.0002 0.0007 0.0007 0.0035 0.0035 0.0104
#[19] 0.0104 0.0417 0.1250 0.1250 0.2500 1.0000
#[25] 7.0000 7.0000 30.4375 30.4375 91.3125 91.3125
#[31] 365.2500 1826.2500 9131.2500 36525.0000
Where x
was:
x <- list(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 7, 7, 30, 30, 91, 91, 365, 1826, 9131, 36525
), c(1, 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 5000, 15000,
60000, 60000, 3e+05, 3e+05, 9e+05, 9e+05, 3600000, 10800000,
10800000, 21600000, 0, 0, 0, 37800000, 37800000, 2.7e+07, 2.7e+07,
21600000, 21600000, 21600000, 0))