Anish_kr
Anish_kr

Reputation: 29

how to convert character type time to int?

I have a data frame containing time as a character type in the following manner.

time=c("5 mins 30 seconds","1 min 46 seconds","6 mins 40 seconds","20 seconds","2 seconds")
student=c("A","B","C")
df<-data.frame(student,time)

I want to sort time taken by them and then plot. How should I proceed?
Also how to convert time to INT type in seconds?

Upvotes: 0

Views: 94

Answers (1)

lorenzofeliz
lorenzofeliz

Reputation: 607

If the format remains same then, we can use strptime function in R

time<- c("5 mins 30 seconds","3 mins 46 seconds","6 mins 40 seconds")
times <- strptime(time, "%M mins %S seconds")
times <- format(times, "%H:%M:%S")
student=c("A","B","C")
df<-data.frame(student,times)

Upvotes: 1

Related Questions