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_dataa 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')> iua 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_dataa 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]]
Hi Ben, Try all_data[, colSums(iu) == 0] <- NA all_data HTH, Jorge.- On Wed, Nov 23, 2011 at 2:41 PM, Ben quant <> wrote:> 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. >[[alternative HTML version deleted]]
Matrix multiplication, maybe?> all_data %*% iua b c [1,] 1 4 0 [2,] 2 5 0 [3,] 3 6 0 I have no idea if this is a general solution or not, but it works in this case. If you need something else, perhaps a more realistic example would help. Dennis On Wed, Nov 23, 2011 at 11:41 AM, Ben quant <ccquant at gmail.com> wrote:> 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 at 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. >