Hi,, Can I get help with this. I need to remove a column form the matrix if that specific column has his all entry zero, here the code I wrote but it is not working can you help me please namVar <- c("TrlgWSST","TrlgWSSE","TrlgWSSR","SSdiff","TrlgWMSE","TrlgWR2","TrlgWR2adj","TrlgSSE","TrlgMSE","TrlgR2","TrlgR2adj","TrSSE","TrMSE","TrR2","TrR2adj","rdf2", "TelgSSE","TelgMSE","TelgR2","TelgR2adj","TelgWSSE","TelgWMSE" ,"TelgWR2","TelgWR2adj","TeSSE","TeMSE","TeR2","TeR2adj","edf2","Runtime") Asse <- matrix(0,nrow=5,ncol=length(namVar)) Asse[,1:3]<-2 Asse[2,4] <-2000 Asse[,5:10]<-3 Asse[,11:20]<-5 Asse[,21:30]<-12 if(all(Asse[,"SSdiff"]==0)==TRUE){ Asse <- Asse[,-which(colnames(Asse)%in%"SSdiff")]} -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334.html Sent from the R help mailing list archive at Nabble.com.
Hi frespider, I think the problem is first that you are referring to column names that you haven't yet defined. To add the column names you can use the dimnames argument of the matrix function. Asse <- matrix(0,nrow=5,ncol=length(namVar), dimnames = list(NULL, namVar)) JoAnn -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334p4650344.html Sent from the R help mailing list archive at Nabble.com.
Hi, I edited the code sorry, I forgot the line before Can you have look again please? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334p4650348.html Sent from the R help mailing list archive at Nabble.com.
Hi, In this particular example, you may not be able to remove any columns: because: namVar <- c("TrlgWSST","TrlgWSSE","TrlgWSSR","SSdiff","TrlgWMSE","TrlgWR2","TrlgWR2adj","TrlgSSE","TrlgMSE","TrlgR2","TrlgR2adj","TrSSE","TrMSE","TrR2","TrR2adj","rdf2", "TelgSSE","TelgMSE","TelgR2","TelgR2adj","TelgWSSE","TelgWMSE" ,"TelgWR2","TelgWR2adj","TeSSE","TeMSE","TeR2","TeR2adj","edf2","Runtime") Asse <- matrix(0,nrow=5,ncol=length(namVar), dimnames = list(NULL, namVar)) #JoAnn's suggestion Asse[,1:3]<-2 Asse[2,4] <-2000 Asse[,5:10]<-3 Asse[,11:20]<-5 Asse[,21:30]<-12 Asse[,1:5] #???? TrlgWSST TrlgWSSE TrlgWSSR SSdiff TrlgWMSE #[1,]??????? 2??????? 2??????? 2????? 0??????? 3 #[2,]??????? 2??????? 2??????? 2?? 2000??????? 3 #[3,]??????? 2??????? 2??????? 2????? 0??????? 3 #[4,]??????? 2??????? 2??????? 2????? 0??????? 3 #[5,]??????? 2??????? 2??????? 2????? 0??????? 3 So, if you are not assigning : #Asse[2,4] <-2000 Then, Asse[,4] will be all zeros. Asse[,!colSums(abs(Asse))==0] #will remove the columns which are all zeros #or Asse[,!apply(Asse,2,function(x) all(x==0))] #will get the results you wanted. A.K. ----- Original Message ----- From: frespider <frespider at hotmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, November 21, 2012 12:52 PM Subject: [R] Remove Column from matrix Hi,, Can I get help with this. I need to remove a column form the matrix if that specific column has his all entry zero, here the code I wrote but it is not working can you help me please namVar <- c("TrlgWSST","TrlgWSSE","TrlgWSSR","SSdiff","TrlgWMSE","TrlgWR2","TrlgWR2adj","TrlgSSE","TrlgMSE","TrlgR2","TrlgR2adj","TrSSE","TrMSE","TrR2","TrR2adj","rdf2", "TelgSSE","TelgMSE","TelgR2","TelgR2adj","TelgWSSE","TelgWMSE" ,"TelgWR2","TelgWR2adj","TeSSE","TeMSE","TeR2","TeR2adj","edf2","Runtime") Asse <- matrix(0,nrow=5,ncol=length(namVar)) Asse[,1:3]<-2 Asse[2,4] <-2000 Asse[,5:10]<-3 Asse[,11:20]<-5 Asse[,21:30]<-12 if(all(Asse[,"SSdiff"]==0)==TRUE){ Asse <- Asse[,-which(colnames(Asse)%in%"SSdiff")]} ? ? ? ? ? ? -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334.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.
Hello, Three things: 1. You don't need an explicit comparison to TRUE, if(all(Asse[,"SSdiff"]==0)){ will do the same. 2. Your matrix Asse doesn't have colnames, try to see the output of colnames(Asse) You forgot to assign colnames(Asse) <- namVar. 3. Even if it did, SSdiff is the 4th column, which you have set to 2000 so the if statement would return FALSE. Hope this helps, Rui Barradas Em 21-11-2012 17:52, frespider escreveu:> Hi,, > Can I get help with this. I need to remove a column form the matrix if that > specific column has his all entry zero, here the code I wrote but it is not > working can you help me please > > namVar <- > c("TrlgWSST","TrlgWSSE","TrlgWSSR","SSdiff","TrlgWMSE","TrlgWR2","TrlgWR2adj","TrlgSSE","TrlgMSE","TrlgR2","TrlgR2adj","TrSSE","TrMSE","TrR2","TrR2adj","rdf2", > "TelgSSE","TelgMSE","TelgR2","TelgR2adj","TelgWSSE","TelgWMSE" > ,"TelgWR2","TelgWR2adj","TeSSE","TeMSE","TeR2","TeR2adj","edf2","Runtime") > Asse <- matrix(0,nrow=5,ncol=length(namVar)) > Asse[,1:3]<-2 > Asse[2,4] <-2000 > Asse[,5:10]<-3 > Asse[,11:20]<-5 > Asse[,21:30]<-12 > if(all(Asse[,"SSdiff"]==0)==TRUE){ > Asse <- Asse[,-which(colnames(Asse)%in%"SSdiff")]} > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334.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.