Hi, I have a matrix (pwdiff in the example below) with ~480000 rows and 780 columns. For each row, I want to get the percentage of columns that have an absolute value above a certain threshold "t". I then want to allocate that percentage to matrix 'perc' in the corresponding row. Below is my attempt at doing this, but it does not work: I get 'replacement has length zero'. Any help would be much appreciated!! perc<-matrix(c(1:nrow(pwdiff))) for (x in 1:nrow(pwdiff)) perc[x]<-(((ncol(pwdiff[,abs(pwdiff[x,]>=t)]))/ncol(pwdiff))*100) I should add that my data has NAs in some rows and not others (but I do not want to just ignore rows that have NAs) Thanks! Paul -- View this message in context: http://r.789695.n4.nabble.com/counting-columns-that-fulfill-specific-criteria-tp3622265p3622265.html Sent from the R help mailing list archive at Nabble.com.
Hi> Hi, > > I have a matrix (pwdiff in the example below) with ~480000 rows and 780 > columns. > For each row, I want to get the percentage of columns that have anabsolute> value above a certain threshold "t". I then want to allocate thatpercentage> to matrix 'perc' in the corresponding row. Below is my attempt at doing > this, but it does not work: I get 'replacement has length zero'. Anyhelp> would be much appreciated!! > > perc<-matrix(c(1:nrow(pwdiff))) > for (x in 1:nrow(pwdiff)) > perc[x]<-(((ncol(pwdiff[,abs(pwdiff[x,]>=t)]))/ncol(pwdiff))*100)As> matrix(c(1:nrow(pwdiff)))Error in nrow(pwdiff) : object 'pwdiff' not found>gives an error we cannot directly check your code.>From what you say it seems to me that you want something likerowSums(pwdiff>=t, na.rm=T)/ncol(pwdiff)*100 or maybe rowSums(abs(pwdiff)>=t, na.rm=T)/ncol(pwdiff)*100 but I can be completely wrong. Regards Petr> > I should add that my data has NAs in some rows and not others (but I donot> want to just ignore rows that have NAs) > > Thanks! > > Paul > > -- > View this message in context: http://r.789695.n4.nabble.com/counting- > columns-that-fulfill-specific-criteria-tp3622265p3622265.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hello Paul. You could try something like perc<-apply(pwdiff, 1 function(currow){ mean(abs(currow) > t, na.rm=TRUE)*100 }) I haven't tested this, as you did not provide a sample pwdiff. You should probably check ?apply for more info. Two suggestions: probably best not to name any variable t, as this is also the function for transposing a matrix, and could end up being confusing at the least. Second: for most practical purposes, it's better to leave out the *100. Good luck, Nick Sabbe -- ping: nick.sabbe at ugent.be link: http://biomath.ugent.be wink: A1.056, Coupure Links 653, 9000 Gent ring: 09/264.59.36 -- Do Not Disapprove> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of pguilha > Sent: vrijdag 24 juni 2011 13:15 > To: r-help at r-project.org > Subject: [R] counting columns that fulfill specific criteria > > Hi, > > I have a matrix (pwdiff in the example below) with ~480000 rows and 780 > columns. > For each row, I want to get the percentage of columns that have an > absolute > value above a certain threshold "t". I then want to allocate that > percentage > to matrix 'perc' in the corresponding row. Below is my attempt at doing > this, but it does not work: I get 'replacement has length zero'. Any > help > would be much appreciated!! > > perc<-matrix(c(1:nrow(pwdiff))) > for (x in 1:nrow(pwdiff)) > perc[x]<-(((ncol(pwdiff[,abs(pwdiff[x,]>=t)]))/ncol(pwdiff))*100) > > I should add that my data has NAs in some rows and not others (but I do > not > want to just ignore rows that have NAs) > > Thanks! > > Paul > > -- > View this message in context: http://r.789695.n4.nabble.com/counting- > columns-that-fulfill-specific-criteria-tp3622265p3622265.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.