Re: [R] Converting 1-D array to vector

2008-08-27 Thread N. Lapidus
You were very close to an answer :
as.vector(unlist(df[1,]))

Nael

On Wed, Aug 27, 2008 at 7:53 AM, Ronnen Levinson [EMAIL PROTECTED] 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
   day22
   svn rev45424
   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@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@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.


Re: [R] Converting 1-D array to vector

2008-08-27 Thread Peter Dalgaard
N. Lapidus wrote:
 You were very close to an answer :
 as.vector(unlist(df[1,]))

   
I'd use as.character() there, for clarity. It's not easy to remember
what as.vector on a factor variable does.

Also notice that the original post has serious confusion about what the
data structures are:

df is data frame, not a matrix
df[1,] is a one-row data frame, not a 1-d vector
the components of df are factors, not character vectors

A data frame is a kind of list, and lists are a kind of vector, hence
as.vector does nothing to df[,1]. The unlist() function applied to
factors will take the union of their level sets and concatenate the values.

An alternative way is to make sure that you have a character matrix to
begin with:

 as.matrix(df)
 x   y
[1,] a d
[2,] b e
[3,] c f
 as.matrix(df)[1,]
  x   y
a d

(but beware the rather surprising

 as.character(df)
[1] 1:3 1:3

!!!)

 Nael

 On Wed, Aug 27, 2008 at 7:53 AM, Ronnen Levinson [EMAIL PROTECTED] 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
 


-- 
   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
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@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.


Re: [R] Converting 1-D array to vector

2008-08-27 Thread Gasper Cankar
 
 c(as.matrix(df[1,]))

will also work in your case.

Gasper Cankar

-Original Message-
From: N. Lapidus [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 27, 2008 9:06 AM
To: r-help@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 [EMAIL PROTECTED]
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
   day22
   svn rev45424
   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@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@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.

__
R-help@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.


[R] Converting 1-D array to vector

2008-08-26 Thread Ronnen Levinson

   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
   day22
   svn rev45424
   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@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.