[R] Return a Vector of Positions

From: allan miller <amiller_at_a2software.com>
Date: Wed 11 Jan 2006 - 04:56:18 EST


Hello,

I wrote a version of which.na (similar to S's) in R that returns a vector of positions at which NA appears in the target vector v:

which.na<-function(v) {
  retv <- c()
  for (i in 1:length(v)) {

     if (is.na(v[i])) {
       retv<-append(i, retv, after = length(retv))
     } 
     

  }
  return(retv)
}

nv <- c(2,4,NA,NA)
nas<-which.r(nv)
print(nas)

[run]

> source("which.r")

[1] 4 3
>

Two questions about this:

(1) It seems that append actually "prepends"

(2) Is there a more simpler way to build the vector of positions (versus using append in a loop, as above), using functions such as match() and apply()?

Thank You.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 11 05:02:55 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:59 EST