[R] extracting columns

David Winsemius dwinsemius at comcast.net
Sun Aug 29 00:49:34 CEST 2010


On Aug 28, 2010, at 1:24 PM, Laetitia Schmid wrote:

> Hi,
> Can anybody show me how to extract all columns in my dataset that  
> are polymorphic? Or phrased in another way I would like to delete  
> all columns that have no more than one letter in it (that are  
> monomorphic).

Assuming you read this in with read.table:

 > table(sapply(txt.in, function(x) length(levels(x))))

   0   1   2   3   4
  18 112  31  10   1

I am further assuming you do not want the 18 columns with no levels  
(presumably numeric or logical, ooops .... only ones with all "T"'s,  
and got converted to logical, so no matter ) then:

 > names(txt.in)[ sapply(txt.in, function(x) length(levels(x))) >1]
  [1] "V1"   "V2"   "V5"   "V23"  "V27"  "V29"  "V30"  "V34"  "V45"   
"V47"  "V48"  "V49"
[13] "V50"  "V53"  "V56"  "V62"  "V63"  "V64"  "V66"  "V67"  "V71"   
"V73"  "V81"  "V90"
[25] "V95"  "V104" "V107" "V109" "V114" "V116" "V121" "V124" "V126"  
"V131" "V133" "V139"
[37] "V156" "V158" "V163" "V166" "V168" "V172"

so:

newdata <- txt.in[ , <the above expression here> ]

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list