[R] how to order each element according to alphabet

Dennis Murphy djmuser at gmail.com
Fri Jul 15 11:40:22 CEST 2011


Hi:

Is this what you're looking for?

Lines <- "
ASG,UXW,AFODJEL
E,TDIWE,ROFD"

# Read in the above lines (for purposes of this example only)
# Note the stringsAs Factors = FALSE option!
df <- read.csv(textConnection(Lines), header = FALSE, stringsAsFactors = FALSE)
closeAllConnections()
dm <- as.matrix(df)   # convert to a character matrix

# Function to sort a character string in alphabetical (lexical) order
sortfun <- function(x)
   paste(sort(unlist(strsplit(x, ''))), collapse = '')

# Apply to the rows of the matrix
t(apply(df, 1, function(x) sapply(x, sortfun)))

Result:
     V1    V2      V3
[1,] "AGS" "UWX"   "ADEFJLO"
[2,] "E"   "DEITW" "DFOR"

If you need to do this for only a subset of your variables, create a
character submatrix and follow the script above on that, after which
you would need to do some post-processing on your own.


HTH,
Dennis

On Thu, Jul 14, 2011 at 6:18 PM, onthetopo <jint83 at sina.com> wrote:
> Hi there,
>
>  I have a large amino acid csv file like this:
>
> input.txt:
> P,LV,Q,Z
> P,VL,Q,Z
> P,ML,QL,Z
>
> There is a problem with this file, since LV and VL are in fact the same
> thing.
> How do I order each element according to alphabetical order so that the
> desired output would look like:
>
> output.txt:
> P,LV,Q,Z
> P,LV,Q,Z
> P,LM,LQ,Z
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-order-each-element-according-to-alphabet-tp3668997p3668997.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list