[R] Converting 1-D array to vector

Ronnen Levinson RML27 at cornell.edu
Wed Aug 27 07:53:03 CEST 2008


   Hi.
   How  do I convert a one-dimensional array of characters to a character
   vector? In the example below I am trying to get the result c("a","d"). The
   function as.vector() returns the same one-dimensional array, and unlist()
   returns something more complicated than I seek.
   Yours truly,
   Ronnen.
   P.S. E-mailed CCs of posted replies appreciated.
   > df=data.frame(x=letters[1:3],y=letters[4:6])
   > df
     x y
   1 a d
   2 b e
   3 c f
   > df[1,]
     x y
   1 a d
   > as.vector(df[1,])
     x y
   1 a d
   > unlist(df[1,])
   x y
   a d
   Levels: a b c d e f
   > c("a","d") # desired result
   [1] "a" "d"
   > version
                  _
   platform       i386-pc-mingw32
   arch           i386
   os             mingw32
   system         i386, mingw32
   status
   major          2
   minor          7.0
   year           2008
   month          04
   day            22
   svn rev        45424
   language       R
   version.string R version 2.7.0 (2008-04-22)

   --
   Ronnen Levinson, Ph.D.
   scientist, Lawrence Berkeley National Lab
   The  Onion  horoscope: Libra September 23 - October 23 Your tactics of
   overwhelming your opposition with spectacular shows of force and choking the
   roads with fleeing refugees will be seen as inappropriate by the other
   electronics wholesalers.


More information about the R-help mailing list