0

I have the following dataset:

dput(tt2)
structure(c(1371.25, NA, 1373.95, NA, NA, 1373, NA, 1373.95, 
1373.9, NA, NA, 1374, 1374.15, NA, 1374, 1373.85, 1372.55, 1374.05, 
1374.15, 1374.75, NA, NA, 1375.9, 1374.05, NA, NA, NA, NA, NA, 
NA, NA, 1375, NA, NA, NA, NA, NA, 1376.35, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1376.25, NA, 1378, 1376.5, NA, NA, NA, 1378, 
1378, NA, NA, 1378.8, 231.9, 231.85, NA, 231.9, 231.85, 231.9, 
231.8, 231.9, 232.6, 231.95, 232.35, 232, 232.1, 232.05, 232.05, 
232.05, 231.5, 231.3, NA, NA, 231.1, 231.1, 231.1, 231, 231, 
230.95, 230.6, 230.6, 230.7, 230.6, 231, NA, 231, 231, 231.45, 
231.65, 231.4, 231.7, 231.3, 231.25, 231.25, 231.4, 231.4, 231.85, 
231.75, 231.5, 231.55, 231.35, NA, 231.5, 231.5, NA, 231.5, 231.25, 
231.15, 231, 231, 231, 231.05, NA), .Dim = c(60L, 2L), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "Asia/Calcutta", tzone = "Asia/Calcutta", index = structure(c(1459482300, 
1459483766.38983, 1459485231.77966, 1459486697.16949, 1459488162.55932, 
1459489627.94915, 1459491093.33898, 1459492558.72881, 1459494025.11864, 
1459495490.50847, 1459496955.89831, 1459498421.28814, 1459499887.67797, 
1459501353.0678, 1459502818.45763, 1459504283.84746, 1459505749.23729, 
1459507214.62712, 1459508680.01695, 1459510145.40678, 1459511610.79661, 
1459513076.18644, 1459514541.57627, 1459516007.9661, 1459517474.35593, 
1459518939.74576, 1459520405.13559, 1459521870.52542, 1459523335.91525, 
1459524804.30508, 1459526269.69492, 1459527735.08475, 1459529200.47458, 
1459530667.86441, 1459532134.25424, 1459533600.64407, 1459535066.0339, 
1459536531.42373, 1459537996.81356, 1459539702.20339, 1459541167.59322, 
1459542634.98305, 1459544100.37288, 1459545565.76271, 1459547031.15254, 
1459548496.54237, 1459549961.9322, 1459551429.32203, 1459552894.71186, 
1459554360.10169, 1459555829.49153, 1459557294.88136, 1459558760.27119, 
1459560225.66102, 1459561691.05085, 1459563160.44068, 1459564625.83051, 
1459566091.22034, 1459567557.61017, 1459569028), tclass = c("POSIXct", 
"POSIXt"), tzone = "Asia/Calcutta"), .Dimnames = list(NULL, c("A", 
"B")), class = c("xts", "zoo"))

I want to learn how to pass arguments to a function inside the apply family functions. 1st example: Since there are NAs in the data, mean function returns NA. So I want to pass na.rm=TRUE:

tt<-apply.daily(tt2, function(x) sapply(x,mean(na.rm=TRUE)))

But it returns:

Error in mean.default(na.rm = TRUE) : 
  argument "x" is missing, with no default

2nd example: I want to use period.sum function that takes only single column values and requires index of the column.

tt<-lapply(tt2, period.sum, endpoints(tt2))

Error in FUN(X[[i]], ...) : NA/NaN/Inf in foreign function call (arg 3)

I know the 2nd example can me solved with period.apply but as lapply is a general type function can the 2nd example be solved using lapply also?

runjumpfly
  • 319
  • 1
  • 10
  • 1
    `sapply(x, mean, na.rm=TRUE)` I presume. Or possibly even just: `apply.daily(tt2, mean, na.rm=TRUE)` – thelatemail Oct 19 '16 at 06:26
  • 1
    You could try to curry your existing function to create a new function that you apply. http://stackoverflow.com/questions/2228544/higher-level-functions-in-r-is-there-an-official-compose-operator-or-curry-fun – OneCricketeer Oct 19 '16 at 06:28
  • @cricket_007 nice reference but sadly I am still stuck at beginner level. – runjumpfly Oct 19 '16 at 06:56
  • @Procrastinatus Maximus I have gone through the referred question. And my 2nd example show that I applied it exactly. But I still get the error and hence my question is not a duplicate. – runjumpfly Oct 19 '16 at 08:22

1 Answers1

2

You can pass arguments in all the functions of the apply family through the ellipsis (...) argument, cf. the help page on sapply. Now, apply.daily is just an extension to xts objects, see ?apply.daily.

 apply.daily(tt2, mean, na.rm=TRUE)
#apply.daily(  x,  FUN,        ...)
Therkel
  • 1,379
  • 1
  • 16
  • 31
  • See my 2nd example – runjumpfly Oct 19 '16 at 07:11
  • @runjumpfly the answer also applies to your 2nd example. – Hong Ooi Oct 19 '16 at 07:13
  • @HongOoi Error in FUN(X[[i]], ...) : NA/NaN/Inf in foreign function call (arg 3) – runjumpfly Oct 19 '16 at 07:16
  • 1
    @runjumpfly [you should not ask more than one question per post](http://meta.stackoverflow.com/questions/275908/more-than-one-question-per-post). But you could do `period.apply(tt2,endpoints(tt2),colSums,na.rm=TRUE)` – Therkel Oct 19 '16 at 07:37
  • What I wanted to ask using 2nd example was how to pass argument to endpoints in the period.sum in the lapply. – runjumpfly Oct 19 '16 at 07:41
  • If asking sub questions is not acceptable then I will ask it as a separate question. But What I asked is essentially how to pass different arguments to apply functions. – runjumpfly Oct 19 '16 at 07:44
  • @runjumpfly For that there already exist answered questions: [passing several arguments to FUN of lapply (and others *apply)](http://stackoverflow.com/questions/14427253/passing-several-arguments-to-fun-of-lapply-and-others-apply). For you, `lapply(tt2,colSums,na.rm=TRUE)` is equivalent to the above but the return type is a list (as that is what `lapply` returns). – Therkel Oct 19 '16 at 07:58
  • I have gone through the referred question. And my 2nd example show that I applied it exactly. But I still get the error and hence my question is not a duplicate. – runjumpfly Oct 19 '16 at 08:20
  • Yes and I will accept your answer also, But I was thinking is it possible to use period.sum within lapply function. I am confused becasue ellipsis (...) is not taking endpoints(tt2). This is my last query – runjumpfly Oct 19 '16 at 08:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126077/discussion-between-therkel-and-runjumpfly). – Therkel Oct 19 '16 at 08:45