1

This code works (takes hours minutes and seconds and converts to seconds only):

library(lubridate)
library(tidyverse)


original_date_time<-"2018-01-3111:59:59"
period_to_seconds(hms(paste(hour(original_date_time), minute(original_date_time),second(original_date_time), sep = ":")))

I have this tibble:

  df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 07:04","1999-12-31 03:05:07"))
    tib<-as_tibble(df)
    tib

result:

# A tibble: 5 x 2
     id Time               
  <dbl> <fct>              
1     1 1999-12-31 10:10:10
2     2 1999-12-31 09:05:13
3     3 1999-12-31 00:05:25
4     4 1999-12-31 07:04   
5     5 1999-12-31 03:05:07

Now I want to apply that code that changes time above to every cell of tib$Time. I tried like:

time_converted_data_<-lapply(tib$Time, period_to_seconds(hms(paste(hour(tib$Time), minute(tib$Time),second(tib$Time), sep = ":"))))

But it gives me the error:

Error in match.fun(FUN) : 
  c("'period_to_seconds(hms(paste(hour(tib$Time), minute(tib$Time), ' is not a function, character or symbol", "'    second(tib$Time), sep = \":\")))' is not a function, character or symbol")

How to fix that? I want both, R basic and tidyverse versions.

1
  • 1
    lapply(tib$Time, function(x) period_to_seconds(hms(paste(hour(x), minute(x), second(x), sep = ":")))). For each tib$Time apply function(x). Commented Jul 18, 2019 at 16:27

1 Answer 1

2

Base R

Your function is vectorized. So, you could just do

period_to_seconds(hms(paste(hour(tib$Time), minute(tib$Time),second(tib$Time), sep = ":")))
#[1] 36600 32700   300 25440 11100

For non vectorized functions, you could try something like

foo = function(x){
    period_to_seconds(hms(paste(hour(x), minute(x),second(x), sep = ":")))
}
lapply(tib$Time, foo)
#[[1]]
#[1] 36610

#[[2]]
#[1] 32713

#[[3]]
#[1] 325

#[[4]]
#[1] 25440

#[[5]]
#[1] 11107
Sign up to request clarification or add additional context in comments.

4 Comments

The function period_to_seconds is a vectorized function, there is no need for lapply. foo(tib$Time) will work.
How to receive the result as changed column of original tibble instead of list?
Thank you. Both variants woked: 1st. time_converted_data<-tib time_converted_data$Time<-sapply(tib$Time, function(x) period_to_seconds(hms(paste(hour(x), minute(x), second(x), sep = ":")))) and 2nd time_converted_data<-tib time_converted_data$Time<-unlist(lapply(tib$Time, function(x) period_to_seconds(hms(paste(hour(x), minute(x), second(x), sep = ":")))))
@vasili111, note your 4th time does not have the seconds defined "07:04" this lead to the differences in the values as d.b. showed in his answer. It is important that all of your input is in the identical format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.