*Hi all,* I'm trying to filter a file through the columns. This file below is a example of my data frame. My true data frame has seven hundred thousand columns and 500 hundred lines. I need to identify and to remove all columns that all elements equal a number 1. In this my case, columns were deleted are number 1,5 and 10. file <-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) the result after the filter will be 0 2 2 1 5 1 1 0 2 2 1 5 1 1 0 2 2 2 5 2 2 0 2 2 1 5 1 1 0 2 2 0 5 0 2 0 2 2 1 5 1 0 I used this commands idlength<-sapply(file,function(x) length(unique(x)) ), but came an error message: caught segfault *** address 0x4, cause 'memory not mapped' *My question: is it possible to remove the all columns from above file to *achieve* the desired result?* * * * **Thank you for help* Kate Dresh [[alternative HTML version deleted]]
Hello, Try this: idx<-sapply(file,function(x) all(unique(x)==1)) file[!idx] #? V2 V3 V4 V6 V7 V8 V9 #1? 0? 2? 2? 1? 5? 1? 1 #2? 0? 2? 2? 1? 5? 1? 1 #3? 0? 2? 2? 2? 5? 2? 2 #4? 0? 2? 2? 1? 5? 1? 1 #5? 0? 2? 2? 0? 5? 0? 2 #6? 0? 2? 2? 1? 5? 1? 0 A.K. ----- Original Message ----- From: Kate Dresh <dreshkate at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, August 24, 2012 8:11 PM Subject: [R] remove column *Hi all,* I'm trying to filter a file through the columns. This file below is a example of my data frame. My true data frame has seven hundred thousand columns and 500 hundred lines. I need to identify and to remove all columns that all elements equal a number 1. In this my case, columns were deleted are number 1,5 and 10. file <-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) the result after the filter will be 0? 2? 2? 1? 5? 1? 1 0? 2? 2? 1? 5? 1? 1 0? 2? 2? 2? 5? 2? 2 0? 2? 2? 1? 5? 1? 1 0? 2? 2? 0? 5? 0? 2 0? 2? 2? 1? 5? 1? 0 I used this commands idlength<-sapply(file,function(x) length(unique(x)) ), but came an error message: caught segfault *** address 0x4, cause 'memory not mapped' *My question: is it possible to remove the all columns from above file to *achieve* the desired result?* * * * **Thank you for help* Kate Dresh ??? [[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.
On 25-08-2012, at 02:11, Kate Dresh wrote:> *Hi all,* > > > > I'm trying to filter a file through the columns. This file below is a > example of my data frame. My true data frame has seven hundred thousand > columns and 500 hundred lines. I need to identify and to remove all columns > that all elements equal a number 1. In this my case, columns were deleted > are number 1,5 and 10. > > > > > > file <-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) > > > > the result after the filter will be > > > > 0 2 2 1 5 1 1 > > 0 2 2 1 5 1 1 > > 0 2 2 2 5 2 2 > > 0 2 2 1 5 1 1 > > 0 2 2 0 5 0 2 > > 0 2 2 1 5 1 0 > > > > I used this commands idlength<-sapply(file,function(x) length(unique(x)) > ), > > but came an error message: > > > > caught segfault *** > > address 0x4, cause 'memory not mapped' > >This works for me without errors. But it won't do what you want.> > *My question: is it possible to remove the all columns from above file > to *achieve* the desired result?*Yes. For example like this k <- sapply(file,function(x) all(x==1)) file[,-which(k)] or file[,sapply(file,function(x) !all(x==1))] Berend