[R] coercing columns

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Jan 7 17:32:42 CET 2005


On Fri, 2005-01-07 at 15:47 +0100, Christoph Scherber wrote:
> Dear all,
> 
> I have a data frame that looks like this:
> 
> c1   c2   c3
> A    B    C
> B    C    A
> A    A    B
> 
> and so on;
> 
> I´d like to produce one single vector consisting of the columns c1,c2, 
> c3, such that
> 
> vector=("A","B","A","B","C","A","C","A","B")
> 
> I guess it´s easy to do but I don´t know how...Can anyone help me?
> 
> Thanks a lot!
> Christoph


> as.vector(as.matrix(df))
[1] "A" "B" "A" "B" "C" "A" "C" "A" "B"

If you review the help for as.matrix, it indicates:

 'as.matrix' is a generic function. The method for data frames will
  convert any non-numeric/complex column into a character vector
  using 'format' and so return a character matrix, except that
  all-logical data frames will be coerced to a logical matrix.

So using as.matrix() converts the 'df' columns into a character matrix,
which is then converted into a vector using as.vector().

HTH,

Marc Schwartz




More information about the R-help mailing list