0

I try to run this code to calculate the mean of a column by multiple groups:

mon$sbd <- with(mon, ave(slt, code, tmp, wek, FUN = times(mean(ifelse(slt <= wup, slt+1, slt)))))

Since the variable slt is time in hh:mm format which represents bedtimes before and after 24:00, I added the code times(mean(ifelse(slt <= wup, slt+1, slt))), because this way I usually calculate mean bedtime and it works perfectly.

When I run the code for a normal numeric variable like this

mon$sbd <- with(mon, ave(slt, code, tmp, wek, FUN = mean))

it works without any errors.

Now I receive this message:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  Object 'FUN' with modus 'function' not found

I do not have any objects with similar name than the function or anything (which I figured out as an common problem if this error occurs).

IF the code for calculating mean bedtime is the problem, how can I solve it? Or is there another function for calculating mean time considering the shift after 24:00?

This is the code for the dataframe:

structure(list(code = c("AJH27", "AJH27", "AJH27", "AJH27", "AJH27", 
"AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", 
"AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", 
"AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", "AJH27", 
"AJH27", "ASL07", "ASL07", "ASL07"), tmp = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("T1", 
"T2", "T3"), class = "factor"), wek = c(1, 1, 2, 2, 1, 1, 1, 
1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 
2, 2), slt = structure(c(0.923611111111111, 0.890277777777778, 
0.916666666666667, 0.909722222222222, 0.871527777777778, 0.913194444444444, 
0.899305555555556, 0.951388888888889, 0.899305555555556, 0.850694444444444, 
0.836805555555556, 0.881944444444444, 0.895833333333333, 0.875, 
0.878472222222222, 0.899305555555556, 0.895833333333333, 0.850694444444444, 
0.878472222222222, 0.878472222222222, 0.9375, 0.854166666666667, 
0.822916666666667, 0.927083333333333, 0.861111111111111, 0.871527777777778, 
0.90625, 0.971527777777778, 0.0138888888888889, 0.939583333333333
), format = "h:m:s", class = "times"), wup = structure(c(0.239583333333333, 
0.239583333333333, 0.3125, 0.260416666666667, 0.233333333333333, 
0.229166666666667, 0.229166666666667, 0.239583333333333, 0.229166666666667, 
0.180555555555556, 0.180555555555556, 0.1875, 0.208333333333333, 
0.1875, 0.1875, 0.1875, 0.19375, 0.1875, 0.184027777777778, 0.1875, 
0.25, 0.222222222222222, 0.1875, 0.197916666666667, 0.1875, 0.180555555555556, 
0.194444444444444, 0.238888888888889, 0.208333333333333, 0.291666666666667
), format = "h:m:s", class = "times")), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"))
Nimantha
  • 6,405
  • 6
  • 28
  • 69
psycho95
  • 131
  • 1
  • 12
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You need to pass a function to `FUN=`. In the version that doesn't work, it seems you are passing an expression which is not the same thing. – MrFlick Sep 15 '21 at 20:52
  • I see this problem... but I don't know a function that would calculate the mean for the time variable with considering the time shift by day – psycho95 Sep 15 '21 at 21:02

0 Answers0