merge two vectors of the same length into one?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

merge two vectors of the same length into one?

Zhiqiang Ye
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?

Thank you very much!


--
Zhiqiang Ye

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: merge two vectors of the same length into one?

Sarah Goslee
What about this:

> X <- c("A", "B", "C", "A", "D", "A")
> Y <- c("B", "A", "C", "A", "C", "B")

> XY <- paste(X, Y, sep="")

> sort(table(XY), decreasing=TRUE)
XY
AB AA BA CC DC
 2  1  1  1  1



On 3/26/06, Zhiqiang Ye <[hidden email]> wrote:
>
> Dear all,
>
>        I have a question about merge two character vectors of the same
> length into one.
>
>
--
Sarah Goslee
http://www.stringpage.com

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: merge two vectors of the same length into one?

Duncan Murdoch
In reply to this post by Zhiqiang Ye
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: merge two vectors of the same length into one?

Zhiqiang Ye
> AB AA BA CC DC
>   2  1  1  1  1
>

Yeah, thank you two for your help.  BTW, can I change the output like this way:

AB  2
AA  1
BA  1
CC  1
DC  1

Thanks a lot!

--
Zhiqiang Ye

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: merge two vectors of the same length into one?

Peter Dalgaard
"Zhiqiang Ye" <[hidden email]> writes:

> > AB AA BA CC DC
> >   2  1  1  1  1
> >
>
> Yeah, thank you two for your help.  BTW, can I change the output like this way:
>
> AB  2
> AA  1
> BA  1
> CC  1
> DC  1

Just feed it to cbind()

--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html