[R] Concatenating two vectors into one

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon May 18 13:23:59 CEST 2009


On 18-May-09 11:09:45, Henning Wildhagen wrote:
> Dear users, 
> a very simple question: 
> 
> Given two vectors x and y
> 
> x<-as.character(c("A","B","C","D","E","F"))
> y<-as.factor(c("1","2","3","4","5","6"))
> 
> i want to combine them into a single vector z as A1, B2, C3 and so on.
> 
> z<-x*y is not working, i tried several others function, but did not
> get to the solution.
> 
> Thanks for your help,
> Henning

And a very simple solution! Use paste():

  x<-as.character(c("A","B","C","D","E","F"))
  y<-as.factor(c("1","2","3","4","5","6"))
  paste(x,y)
  # [1] "A 1" "B 2" "C 3" "D 4" "E 5" "F 6"
  paste(x,y,sep="")
  # [1] "A1" "B2" "C3" "D4" "E5" "F6"

Ted.
PS: 'x*y' will attempt to perform a numerical multiplication.
    This cannot work for character vectors.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-May-09                                       Time: 12:23:56
------------------------------ XFMail ------------------------------




More information about the R-help mailing list