[R] Converting 1-D array to vector

Gasper Cankar gasper.cankar at ric.si
Wed Aug 27 14:48:58 CEST 2008


 
> c(as.matrix(df[1,]))

will also work in your case.

Gasper Cankar

-----Original Message-----
From: N. Lapidus [mailto:n.lapidus at gmail.com] 
Sent: Wednesday, August 27, 2008 9:06 AM
To: r-help at r-project.org
Subject: Re: [R] Converting 1-D array to vector

You were very close to an answer :
as.vector(unlist(df[1,]))

Nael

On Wed, Aug 27, 2008 at 7:53 AM, Ronnen Levinson <RML27 at cornell.edu>
wrote:

>
>   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.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list