[R] remove column

Berend Hasselman bhh at xs4all.nl
Sat Aug 25 09:06:43 CEST 2012


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




More information about the R-help mailing list