-3

Below is the str of my data set.

'data.frame':   9995 obs. of  10 variables:
 $ Count           : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Gates    : Factor w/ 5 levels "B6","B9","I1",..: 3 3 4 4 3 4 4 4 4 4 ...
 $ Entry_Date           : Date, format: "0006-10-20" "0006-10-20" "0006-10-20" ...
 $ Entry_Time           : Factor w/ 950 levels "00:01:00","00:04:00",..: 347 366 450 550 563 700 701 350 460 506 ...
 $ Exit_Date          : Date, format: "0006-10-20" "0006-10-20" "0006-10-20" ...
 $ Exit_Time          : Factor w/ 1012 levels "00:00:00","00:01:00",..: 618 556 637 694 770 936 948 590 640 655 ...
 $ Type_of_entry    : Factor w/ 3 levels "Manual","Pass",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ weekday     : Factor w/ 7 levels "Friday","Monday",..: 2 2 2 2 2 2 2 6 6 6 ...
 $ Ticket.Loss: Factor w/ 2 levels "N","Y": 1 1 1 1 1 2 2 1 1 1 ...
 $ Duration  : Factor w/ 501 levels "00:01:00","00:02:00",..: 223 142 139 96 159 188 199 192 132 101 ...

I am using below function:

W <- aggregate(Duration ~ Gates, data=parking, FUN=mean)

But getting below error:

Warning messages: 1: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA

Jaap
  • 81,064
  • 34
  • 182
  • 193
ESR
  • 1
  • 2
    @Priyanka The levels of 'Duration' seems to be `character` (possibly time). In that case, do you want the `mean` time. – akrun Jun 26 '16 at 04:53

2 Answers2

2

Duration is a factor of strings that look like time durations, "00:01:00", etc.

The chron package works with character strings such as this.

library(chron)
aggregate(chron(times=Duration) ~ Gates, data=parking, FUN=mean)

This will give the average time for each level in Gates.

See also convert character to time in R

Community
  • 1
  • 1
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
0

If the OP's dataset is infact time column, we can use as.POSIXct to convert it to 'DateTime' class

parking$Duration <- as.POSIXct(parking$Duration, format = "%H:%M:%S")
transform(aggregate(Duration ~ Gates, data = parking, FUN = mean), 
                               Duration = sub("\\S+\\s+", "", Duration))
#  Gates Duration
#1    B6 11:08:34
#2    B9 11:07:31
#3    I1 11:07:10

NOTE: No external packages used.

data

set.seed(24)
parking <- data.frame(Gates = sample(c("B6", "B9", "I1"), 20, replace=TRUE),
  Duration = format(seq(Sys.time(), length.out=20, by = "1 min") , "%H:%M:%S"))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662