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.
On Fri, Aug 12, 2011 at 4:23 PM, erinbspace <erin.braswell at gmail.com> wrote:> 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> library(chron) > Time.Spent <- c("12:12:10", "13:12:10") > times(mean(as.numeric(times(Time.Spent))))[1] 12:42:10 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On 08/13/2011 06:23 AM, erinbspace wrote:> 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. >Hi erinbspace, By hard coding the [[1]] in your function, you are automatically taking the first element of any list. If you want to convert a vector of times, try this: time.to.seconds<- function(time) { time<-strsplit(time, ":")[[1]] return(as.numeric(time[1]) * 3600 + as.numeric(time[2]) * 60 + as.numeric(time[3])) } watch.times<-c("0:2:31","0:4:12","0:0:47") # use sapply to step through the vector of times sapply(watch.times,time.to.seconds) 0:2:31 0:4:12 0:0:47 151 252 47 Jim