[R] Finding an average time spent

erinbspace erin.braswell at gmail.com
Fri Aug 12 22:23:32 CEST 2011


Hello R help! 

I am extremely new to R (as in 3 just days) and I've been using it to do
some pretty basic things. I am frustratingly stuck on one point, and am so
so so close to figuring it out, but just far enough away to ask for some
(perhaps embarrassingly easy) help.

I have a dataset, visitors, that has a variable called Time.Spent.
Time.Spent consists of times in the format hh:mm:ss , and it is a
measurement, kind of like a timer, of the amount of time someone spent in a
museum exhibit. 

I need to find the average time spent.  I've figured the easiest way to do
this would be to convert it into seconds. I found a function that someone
wrote on how to do this here:
http://stackoverflow.com/questions/1389428/dealing-with-time-periods-such-as-5-minutes-and-30-seconds-in-r

I thought this would be the answer! However, when I run the code, it works
perfectly for the first variable in the first observation, but then repeats
the same answer all the way down the rows. 

Sorry for the wordiness, here's the code I have:

# The function to convert hh:mm:ss into just seconds:

time.to.seconds <- function(time) {
   time <- strsplit(time, ":")[[1]]
   return ((as.numeric(time[1]) * 60 * 60) + (as.numeric(time[2]) * 60) +
(as.numeric(time[3])))
}

# I've tried many things to then create a new variable in the dataset
visitors:

visitors$TimeInSeconds <- time.to.seconds(time=c(visitors$Time.Spent))

# Or

visitors$TimeInSeconds <- time.to.seconds(visitors$Time.Spent)


I figure it has something to do with the fact that strsplit() makes a list?
Do I need a loop to go through each variable? I know this is a huge question
but any hints at al would be very much appreciated. 


--
View this message in context: http://r.789695.n4.nabble.com/Finding-an-average-time-spent-tp3740391p3740391.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list