[R] merge two vectors of the same length into one?

Duncan Murdoch murdoch at stats.uwo.ca
Sun Mar 26 18:39:51 CEST 2006


On 3/26/2006 11:26 AM, Zhiqiang Ye wrote:
> Dear all,
> 
>        I have a question about merge two character vectors of the same
> length into one.
> 
> For example:
> 
>       X= c('A', 'B', 'C', 'A', 'D',   .......)      # 26 possible
> alphabet values with length 1000
>       Y=c('B', 'A', 'C', 'A', 'C',   ........)      # 26 possible
> aphabet values with length  1000
> 
>      after using table ( X,Y ), I will get a matrix of 26*26.  But I
> want to sort these values from large to small, that is to say, I want
> to get an output like this;
> 
>       AB   75
>       AC   60
>       CC   58
>       ....
> 
> How can I get this kind of output?

I am not sure I understand what you want (e.g. I would have expected to 
see BA in your list).

But you can get the table of pairs via

table(paste(X, Y, sep=""))

and you can sort it if you want, e.g.

 > X= c('A', 'B', 'C', 'A', 'D', 'A')
 > Y= c('B', 'A', 'C', 'A', 'C', 'B')
 > table(paste(X,Y,sep=""))

AA AB BA CC DC
  1  2  1  1  1
 > sort(table(paste(X,Y,sep="")), dec=TRUE)

AB AA BA CC DC
  2  1  1  1  1




More information about the R-help mailing list