0
votes

Another newbie question, I'm afraid. I'm trying to create new variables with "lubridate" and have run into 3 issues.

I start with a variable "time" imported as a factor in the format H:MM:SS or HH:MM:SS. I also created "timeC" as a character.

1. Display issue

    temp$time2 <- hms(temp$time) #take my original variable "time" and convert

Problem: only the seconds portion of "time2" displays on the screen, although it appears to be correct "under the hood":

 time2
 ----
  4

> temp$time2[1,]
[1] "7H 0M 4S"

2. Extract hours Issue

     temp$hour <- hour(temp$time2) #extract just the hours portion

Problem: this works intermittently; sometimes it's fine, sometimes I get the following error:

Error in as.POSIXlt.character(as.character(x), ...) : 
   character string is not in a standard unambiguous format

This is concerning to me because this doesn't seem like the kind of message lubridate should be throwing.

3. Extract hour and minutes Issue

temp$hourMins <- hm(temp$time2)

Problem: every value of hourMins is "NA"

I've probably invested 4+ hours trying to figure this out and am wondering if there is another package that can support what I'm trying to do.

Any advice would be most appreciated. Thanks in advance and regards!

1
I think you're confusing parsing a string into a time (i.e. hms() and hm()) with extracting components from that time (hours(), minutes(), seconds())hadley
@hadley - quite right your are (as you would be); still getting the hang of R documentation: complete, but sometimes hard to tell "what matters" and what's under the hood/not important for day to day use.user2621147

1 Answers

0
votes

First, make a "toy" data for illustration,

require(lubridate)
temp <- data.frame(time=factor(c("01:01:01","02:02:02","03:03:03")))
temp$time2 <- hms(temp$time)
temp

##       time    time2
## 1 01:01:01 1H 1M 1S
## 2 02:02:02 2H 2M 2S
## 3 03:03:03 3H 3M 3S

1) Display issue

You need attach

attach(temp)
## The following objects are masked from temp (position 3):

##     time, time2

time2
## [1] "1H 1M 1S" "2H 2M 2S" "3H 3M 3S"

## detach(temp) # after final use.

Otherwise, the time2 is the variable already in the environment, not the column of temp.

Edit: You may check with strto see what are exactly in your time2.

str(time2)
## Formal class 'Period' [package "lubridate"] with 6 slots
##   ..@ .Data : num [1:3] 1 2 3
##   ..@ year  : num [1:3] 0 0 0
##   ..@ month : num [1:3] 0 0 0
##   ..@ day   : num [1:3] 0 0 0
##   ..@ hour  : num [1:3] 1 2 3
##   ..@ minute: num [1:3] 1 2 3

If your time2 has a similar structure but only the "seconds" part shows when you typing time2, which means your print function for this type of objects has been overridden by accident.

2) Extract hours Issue

temp$hour <- hour(temp$time2)

No problem

3) Extract hour and minutes Issue

time3 <- gsub("^(.+):[^:]+$","\\1",as.character(temp$time))
time3
## [1] "01:01" "02:02" "03:03"

hm(time3)
## [1] "1H 1M 0S" "2H 2M 0S" "3H 3M 0S"

Or use {hms} directly to include seconds as well,

hms(temp$time)
## [1] "1H 1M 1S" "2H 2M 2S" "3H 3M 3S"