[R] another do.call() problem.
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Fri Mar 12 11:45:38 CET 2004
Robin Hankin <rksh at soc.soton.ac.uk> writes:
> Hi everyone
>
> suppose I have
>
> a <- array(1:256,rep(4,4))
>
> and want to access a[1,2,3,1] by the vector c(1,2,3,1). As per
> yesterday, I can use do.call():
>
> a[1,2,3,1] == do.call("[",c(list(a),c(1,2,3,1)))
>
> Now how do I apply the above technique (or indeed any other technique!) to get
>
> a[1,2,3,]
>
> [1] 37 101 165 229
>
> from a vector like c(1,2,3,0) or c(1,2,3,NULL) or c(1,2,3,NA)?
The middle one is impossible. The other two can be done like this
v <- c(1,2,3,NA)
al <- lapply(v,function(x) if(is.na(x)) alist(a=)$a else x )
do.call("[",c(list(a),al))
This is in treacherous territory: The missing argument indicator has
some very strange semantics, for instance if you save it in a
variable, then any subsequent use of that variable will trigger the
same error as when you access a missing argument in a function:
mis <- alist(a=)$a
lapply(v,function(x)if(is.na(x))mis else x )
Error in FUN(X[[4]], ...) : Argument "mis" is missing, with no default
Technically, the missing argument is implemented as a zero-length
variable name
> as.character(alist(a=)$a)
[1] ""
> mode(alist(a=)$a)
[1] "name"
However, I don't think it is a good idea to rely on that, and anyway,
as.name("") is forbidden:
> as.name("")
Error in as.name("") : attempt to use zero-length variable name
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list