Hello,

Is there a faster way to do this? Basically, I'd like to NA all values in
all_data if there are no 1's in the same column of the other matrix, iu.
Put another way, I want to replace values in the all_data columns if values
in the same column in iu are all 0. This is pretty slow for me, but works:

 all_data = matrix(c(1:9),3,3)
 colnames(all_data) = c('a','b','c')
> all_data
     a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
 iu = matrix(c(1,0,0,0,1,0,0,0,0),3,3)
 colnames(iu) = c('a','b','c')
> iu
     a b c
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 0

    fun = function(x,d){
      vals = d[,x]
      i = iu[,x]
      if(!any(i==1)){
        vals = rep(NA,times=length(vals))
      }else{
        vals
      }
      vals
    }
    all_data = sapply(colnames(iu),fun,all_data)
> all_data
     a b  c
[1,] 1 4 NA
[2,] 2 5 NA
[3,] 3 6 NA

...again, this work, but is slow for a large number of columns. Have
anything faster?

Thanks,

ben

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to