Convert char to time

Asked

Viewed 176 times

2

I need to convert a field from a char file to something that is calculable to average.

It is a field that refers to hours originally.

system.time(cpc_call<-fread("ACIONAMENTO_CALL.csv",header = T , sep= ";")) # Importa tabela

When I see the imported file it appears as Chr.

So in an attempt to convert , I made the code below:

cpc_call$Tempo_Falado = as.numeric(cpc_call$Tempo_Falado) # Transforma tempo falado em tempo

cpc_call %>%
group_by(Outcome) %>%
summarise(median(cpc_call$Tempo_Falado))

But the result of the column referring to media returns only "NA"

Edit : Follows the exit with : dput(head(cpc_call$Spoken, 20))

> dput(head(cpc_call$Tempo_Falado, 20))
c("00:01:20", "00:01:46", "00:01:36", "00:05:26", "00:01:37", 
"00:05:11", "00:02:34", "00:01:32", "00:01:44", "00:02:51", "00:02:45", 
"00:01:45", "00:02:22", "00:02:04", "00:03:17", "00:01:59", "00:01:44", 
"00:01:31", "00:01:23", "00:01:43")
  • 1

    How are you cpc_call$Tempo_Falado before the code starts? Give some examples that make it easier for people to help.

  • It helped, because before that I only have the lib and the setwd?

  • Put the exit of dput(head(cpc_call$Tempo_Falado, 20)) question. So we can have an exact copy of the data and see what the problem is and the solution.

1 answer

1


Using the package chron you can transform your vector of Character for the guy teams , that will allow you to perform operations as average, median.

cpc_call <- c("00:01:20", "00:01:46", "00:01:36", "00:05:26", "00:01:37", 
"00:05:11", "00:02:34", "00:01:32", "00:01:44", "00:02:51", "00:02:45", 
"00:01:45", "00:02:22", "00:02:04", "00:03:17", "00:01:59", "00:01:44", 
"00:01:31", "00:01:23", "00:01:43")
library(chron)
cpc_call <- chron(times = cpc_call)

With these values, the mean value was

mean(cpc_call)
[1] 00:02:18

And doing the conference (in seconds),

mean(60*minutes(cpc_call) + seconds(cpc_call))
[1] 138.5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.