[R] Vector-subsetting with ZERO - Is behavior changeable?

1 view
Skip to first unread message

Johannes Graumann

unread,
Oct 5, 2011, 7:29:15 AM10/5/11
to r-h...@stat.math.ethz.ch
Dear All,

I have trouble generizising some code.

> index <- 0
> sapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)]})
Will yield a wished for vector like so:
[1] 3 2 1

But in this case (trying to select te second to last element in each vector
of the list)
> index <- 1
> sapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)]})
I end up with
[[1]]
[1] 2

[[2]]
[1] 1

[[3]]
numeric(0)

I would (massively) prefer something like
[1] 2 1 NA

My current implementation looks like
> index <- 1
> unlist(
> sapply(
> list(c(1,2,3),c(1,2),c(1)),
> function(x){
> value <- x[max(length(x)-index,0)]
> if(identical(value,numeric(0))){return(NA)} else {return(value)}
> }
> )
> )
[1] 2 1 NA

Quite the inelegant eyesore.

Any hints on how to do this better?

Thanks, Joh

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

William Dunlap

unread,
Oct 5, 2011, 12:44:08 PM10/5/11
to Johannes Graumann, r-h...@stat.math.ethz.ch
You can use [1] on the output of FUN to ensure that
exactly one value (perhaps NA from numeric(0)[1]) is
returned. E.g.

> index <- 1
> sapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)][1]})
[1] 2 1 NA

I'll also put in a plug for vapply, which throws an
error if FUN does not return what you expect it to:

> vapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)]}, FUN.VALUE=numeric(1))
Error in vapply(list(c(1, 2, 3), c(1, 2), c(1)), function(x) { :
values must be length 1,
but FUN(X[[3]]) result is length 0
> vapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)][1]}, FUN.VALUE=numeric(1))
[1] 2 1 NA

For long input vectors vapply can save a fair bit of
memory and time over sapply.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

Johannes Graumann

unread,
Oct 10, 2011, 3:43:45 AM10/10/11
to r-h...@stat.math.ethz.ch
Thank you very much. Learned something again!

Joh

Reply all
Reply to author
Forward
0 new messages