Dear, I am using R I'm trying to identify and remove columns and rows in a data frame that are has elements equals. For example in dataframe below. The columns 1, 2,3,4,5 ,6 and 10 (file1) has elements equal then should be removed. How can I ask R to remove those columns with same elements in new dataframe (file2) to result a matrix as follows: file1 1 0 2 2 1 1 5 1 1 1 1 0 2 2 1 1 5 1 1 1 1 0 2 2 1 2 5 2 2 1 1 0 2 2 1 1 5 1 1 1 1 0 2 2 1 0 5 0 2 1 1 0 2 2 1 1 5 1 0 1 file2 1 1 1 1 1 1 2 2 2 1 1 1 0 0 2 1 1 0 After I need to know which ones names of columns are remove = 1, 2,3,4,5 ,6 and 10. Similarly this is I need to check for rows in file2. To remove rows and identify all that elements equal. The final dataframe is: file3 0 0 2 1 1 0 row names removed = 1,2,3 and Thanks and Regards [[alternative HTML version deleted]]
This should work if your data consist of integer values:> dput(file1) # This is the preferred way to send your datastructure(list(V1 = c(1L, 1L, 1L, 1L, 1L, 1L), V2 = c(0L, 0L, 0L, 0L, 0L, 0L), V3 = c(2L, 2L, 2L, 2L, 2L, 2L), V4 = c(2L, 2L, 2L, 2L, 2L, 2L), V5 = c(1L, 1L, 1L, 1L, 1L, 1L), V6 = c(1L, 1L, 2L, 1L, 0L, 1L), V7 = c(5L, 5L, 5L, 5L, 5L, 5L), V8 = c(1L, 1L, 2L, 1L, 0L, 1L), V9 = c(1L, 1L, 2L, 1L, 2L, 0L), V10 = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10"), class = "data.frame", row.names = c(NA, -6L))> file2 <- file1[, sapply(file1, function(x) any(diff(x)))] > file2V6 V8 V9 1 1 1 1 2 1 1 1 3 2 2 2 4 1 1 1 5 0 0 2 6 1 1 0 There is a typo in your message. Columns 1, 2, 3, 4, 5, 7, and 10 have all elements duplicated and 6, 8, and 9 do not. ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Fabiane Silva > Sent: Tuesday, August 14, 2012 8:24 PM > To: r-help at r-project.org > Subject: [R] to remove columns and rows > > Dear, > > > > I am using R I'm trying to identify and remove columns and rows in a > data > frame that are has elements equals. For example in dataframe below. The > columns 1, 2,3,4,5 ,6 and 10 (file1) has elements equal then should be > removed. How can I ask R to remove those columns with same elements in > new > dataframe (file2) to result a matrix as follows: > > > > file1 > > 1 0 2 2 1 1 5 1 1 1 > > 1 0 2 2 1 1 5 1 1 1 > > 1 0 2 2 1 2 5 2 2 1 > > 1 0 2 2 1 1 5 1 1 1 > > 1 0 2 2 1 0 5 0 2 1 > > 1 0 2 2 1 1 5 1 0 1 > > > > file2 > > 1 1 1 > > 1 1 1 > > 2 2 2 > > 1 1 1 > > 0 0 2 > > 1 1 0 > > > > After I need to know which ones names of columns are remove = 1, > 2,3,4,5 ,6 > and 10. > > > > Similarly this is I need to check for rows in file2. To remove rows and > identify all that elements equal. > > The final dataframe is: > > > > file3 > > > > 0 0 2 > > 1 1 0 > > > > row names removed = 1,2,3 and > > > > Thanks and Regards > > [[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.
Hello, Try this: ?idlength<-sapply(file1,function(x) length(unique(x)) ) ?which(idlength>1) #V6 V8 V9 ?#6? 8? 9? #the ones that will be removed which(idlength==1) # V1? V2? V3? V4? V5? V7 V10 ? #1?? 2?? 3?? 4?? 5?? 7? 10 idlength2<-apply(file2,1,function(x) length(unique(x))) ?which(idlength2>1) #[1] 5 6 #the ones that will be removed ?which(idlength2==1) [1] 1 2 3 4 In addition to the method i posted earlier, you could also use: file2<-file1[apply(file1,2, function(x) any(is.na(match(x,x[duplicated(x)]))))] file2 ?#V6 V8 V9 #1? 1? 1? 1 #2? 1? 1? 1 #3? 2? 2? 2 #4? 1? 1? 1 #5? 0? 0? 2 #6? 1? 1? 0 file3<-file2[t(apply(t(file2),2, function(x) any(is.na(match(x,x[duplicated(x)]))))),] ? file3 #? V6 V8 V9 #5? 0? 0? 2 #6? 1? 1? 0 A.K. ________________________________ From: Fabiane Silva <fabianezte at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, August 14, 2012 10:46 PM Subject: Re: [R] to remove columns and rows Thanks a lot . The program below remove columns and row, but I would like know which ID of columns and rows that? were remove. 2012/8/14 arun <smartpink111 at yahoo.com> Hello,>Try this: > >file1<-read.table(text=" > >1? 0? 2? 2? 1? 1? 5? 1? 1? 1 >1? 0? 2? 2? 1? 1? 5? 1? 1? 1 >1? 0? 2? 2? 1? 2? 5? 2? 2? 1 >1? 0? 2? 2? 1 1? 5? 1? 1? 1 >1? 0 2? 2? 1? 0? 5? 0? 2? 1 >1? 0? 2 2? 1? 1? 5? 1? 0? 1 >",sep="",header=FALSE) >?idlength<-sapply(file1,function(x) length(unique(x)) ) >?file2<-subset(file1,select=idlength>1) >?file2 >#? V6 V8 V9 >#1? 1? 1? 1 >#2? 1? 1? 1 >#3? 2? 2? 2 >#4? 1? 1? 1 >#5? 0? 0? 2 >#6? 1? 1? 0 > >idlength2<-apply(file2,1,function(x) length(unique(x))) > >?file3<-subset(t(file2),select=idlength2>1) >file3<-t(file3) >?file3 >#? V6 V8 V9 >#5? 0? 0? 2 >#6? 1? 1? 0 >A.K. > > > > >----- Original Message ----- >From: Fabiane Silva <fabianezte at gmail.com> >To: r-help at r-project.org >Cc: >Sent: Tuesday, August 14, 2012 9:24 PM >Subject: [R] to remove columns and rows > >Dear, > > > >I am using R I'm trying to identify and remove columns and rows in a data >frame that are has elements equals. For example in dataframe below. The >columns 1, 2,3,4,5 ,6 and 10 (file1) has elements equal then should be >removed. How can I ask R to remove those columns with same elements in new >dataframe (file2) to result a matrix as follows: > > > >file1 > >1? 0? 2? 2? 1? 1? 5? 1? 1? 1 > >1? 0? 2? 2? 1? 1? 5? 1? 1? 1 > >1? 0? 2? 2? 1? 2? 5? 2? 2? 1 > >1? 0? 2? 2? 1 1? 5? 1? ?1? 1 > >1? 0 2? 2? 1? 0? 5? 0? 2? 1 > >1? 0? 2 2? 1? 1? 5? 1? 0? 1 > > > >file2 > >1? ?1? 1 > >1? ?1? 1 > >2? ?2? 2 > >1? ?1? 1 > >0? ?0? 2 > >1? ?1? 0 > > > >After I need to know which ones names of columns are remove = 1, 2,3,4,5 ,6 >and 10. > > > >Similarly this is I need to check for rows in file2. To remove rows and >identify all that elements equal. > >The final dataframe is: > > > >file3 > > > >0? ?0? 2 > >1? ?1? 0 > > > >row names removed = 1,2,3 and > > > >Thanks and Regards > >??? [[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. > >