Skip to content
Prev 303191 / 398503 Next

choosing multiple columns

On Aug 11, 2012, at 6:01 AM, Ista Zahn wrote:

            
I would counsel greater consideration of the possible ranges of the  
column names. Even using a variation on Ista Zahn's method intended to  
deliver on the first 8 will fail if the range of possible values is  
greater than 10 in number or the numbers do not start from 1.

If the numbers of the columns do start from 1, you could try this

grep("^OFB[1-8]", paste0("OFB", 1:100) , value=TRUE )[1:8]

Otherwise  consider these efforts;

 > set.seed(123); test <- sample( paste0("OFB", 1:100), 20)
 > sort(test)[1:8]
[1] "OFB21" "OFB27" "OFB29" "OFB4"  "OFB41" "OFB42" "OFB5"  "OFB50

 > grep("^OFB[1-8]", test , value=TRUE )[1:8]
[1] "OFB29" "OFB79" "OFB41" "OFB86" "OFB5"  "OFB50" "OFB83" "OFB51"


Note that even this does not get what you want which is =

 > test[order(as.numeric( sub("OFB", "", test)))][1:8]
[1] "OFB4"  "OFB5"  "OFB9"  "OFB21" "OFB27" "OFB29" "OFB41" "OFB42"

There is also a function named mixedsort in Greg Warnes package gtools  
which automatically splits the alpha and numeric components of of an  
alphanumeric vector and then orders by the two of them separately.

Something like this might achieve:

 > test[ order( sub("[0-9]+","", test),   # an alpha sort .. followed  
by numeric sort
                as.numeric(gsub("[[:alpha:]]*([[:digit:]]*)", '\\1',  
test) ) )]

  [1] "OFB4"  "OFB5"  "OFB9"  "OFB21" "OFB27" "OFB29" "OFB41" "OFB42"  
"OFB50" "OFB51" "OFB60" "OFB77" "OFB78"
[14] "OFB79" "OFB83" "OFB86" "OFB87" "OFB91" "OFB94" "OFB98"


gtools::ixedsort is based on gtools::mixedorder and has more  
sophistication, for instance the attempt to identify spaces and  
delimiters.