Filipe Correia
Filipe Correia

Reputation: 5677

Converting seconds to durations

I'm just starting with R.

My data is in a csv file. This is an excerpt:

p,   t,  a,           b,           c
p01, t1, 4292.226015, 4292.226015, 0.0
p01, t3, 693.795607,  693.795607,  0.0
p02, t1, 262.798468,  262.798468,  0.0
p02, t2, 113.381499,  113.381499,  0.0
p02, t3, 325.854107,  325.854107,  0.0
p02, t4, 428.073246,  428.073246,  0.0
p02, t5, 319.010848,  319.010848,  0.0

... and I'm loading it like this:

data <- read.csv(file='data.csv',head=TRUE,sep=',')

The last three columns are duration values (in seconds), and I'd like them to be lubridate's duration objects. How can I achieve this?

I've tried converting the columns after loading the csv, like this:

data$a <- dseconds(data$a)
data$b <- dseconds(data$b)
data$c <- dseconds(data$c)

... and at first it seems to work. Except, when I ask for a summary, there are several warnings that I'm having some trouble interpreting:

> summary(tasks)
group  task  a               b               c
p01:2  t1:2  Min.   : 113.4  Min.   : 113.4  Min.   :0  
p01:5  t2:1  1st Qu.: 290.9  1st Qu.: 290.9  1st Qu.:0  
       t3:2  Median : 325.9  Median : 325.9  Median :0  
       t4:1  Mean   : 919.3  Mean   : 919.3  Mean   :0  
       t5:1  3rd Qu.: 560.9  3rd Qu.: 560.9  3rd Qu.:0  
             Max.   :4292.2  Max.   :4292.2  Max.   :0  
Warning messages:
1: In quantile.default(object) :
  Class 'Duration' has no 'names' slot; assigning a names attribute will create an invalid object
2: In summary.default(X[[3L]], ...) :
  Class 'Duration' has no 'names' slot; assigning a names attribute will create an invalid object
3: In summary.default(X[[3L]], ...) :
  Setting class(x) to multiple strings ("summaryDefault", "table", ...); result will no longer be an S4 object
> 

Upvotes: 2

Views: 324

Answers (1)

agstudy
agstudy

Reputation: 121568

It is a warning. The summary try to convert the duration objects to class c("summaryDefault", "table") You can reproduce it like this :

   class(data$a) <- c("summaryDefault", "table")
Warning message:
In class(dat$da) <- c("summaryDefault", "table") :
  Setting class(x) to multiple strings ("summaryDefault", "table", ...); 
          result will no longer be an S4 object.

You can define how to coerce the Duration class to a table class for example.

    setAs("Duration", "table", function(from) [email protected]) ## dummy coercion here

Now I can apply summary without warnings

 summary(dat)
      p                  t                   a                b                c           da      
 Length:7           Length:7           Min.   : 113.4   Min.   : 113.4   Min.   :0   n.vars :0     
 Class :character   Class :character   1st Qu.: 290.9   1st Qu.: 290.9   1st Qu.:0   n.cases:6435  
 Mode  :character   Mode  :character   Median : 325.9   Median : 325.9   Median :0                 
                                       Mean   : 919.3   Mean   : 919.3   Mean   :0                 
                                       3rd Qu.: 560.9   3rd Qu.: 560.9   3rd Qu.:0                 
                                       Max.   :4292.2   Max.   :4292.2   Max.   :0  

Upvotes: 2

Related Questions