Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-22 Thread Duncan Murdoch

On 12-08-18 12:33 PM, Martin Maechler wrote:

Joshua Ulrich josh.m.ulr...@gmail.com
 on Sat, 18 Aug 2012 10:16:09 -0500 writes:


  I don't know if this is better, but it's the most obvious/shortest I
  could come up with.  Transpose the data.frame column to a 'row' vector
  and drop the dimensions.

 R identical(nv, drop(t(df)))
  [1] TRUE

Yes, that's definitely shorter,
congratulations!

One gotta is that I'd want a solution that also works when the
df has more columns than just one...

Your idea to use  t(.) is nice and perfect insofar as it
coerces the data frame to a matrix, and that's really the clue:

Where as  df[,1]  is losing the names,
the matrix indexing is not.
So your solution can be changed into

  t(df)[1,]

which is even shorter...
and slightly less efficient, at least conceptually, than mine, which has
been

as.matrix(df)[,1]

Now, the remaining question is:  Shouldn't there be something
more natural to achieve that?
(There is not, currently, AFAIK).


I've been offline, so I'm a bit late to this game, but the examples 
above fail when df contains a character column as well as the desired 
one, because everything gets coerced to a character matrix.  You need to 
select the column first, then convert to a matrix, e.g.


drop(t(df[,1,drop=FALSE]))

Duncan Murdoch



Martin


  Best,
  --
  Joshua Ulrich  |  about.me/joshuaulrich
  FOSS Trading  |  www.fosstrading.com


  On Sat, Aug 18, 2012 at 10:03 AM, Martin Maechler
  maech...@stat.math.ethz.ch wrote:
  Today, I was looking for an elegant (and efficient) way to get a named
  (atomic) vector by selecting one column of a data frame.  Of course,
  the vector names must be the rownames of the data frame.
 
  Ok, here is the quiz, I know one quite cute/slick answer, but was
  wondering if there are obvious better ones, and also if this should
  not become more idiomatic (hence R-devel):
 
  Consider this toy example, where the dataframe already has only one
  column :
 
  nv - c(a=1, d=17, e=101); nv
  a   d   e
  1  17 101
 
  df - as.data.frame(cbind(VAR = nv)); df
  VAR
  a   1
  d  17
  e 101
 
  Now how, can I get 'nv' back from 'df' ?   I.e., how to get
 
  identical(nv, ...)
  [1] TRUE
 
  where .. only uses 'df' (and no non-standard R packages)?
 
  As said, I know a simple solution (*), but I'm sure it is not
  obvious to most R users and probably not even to the majority of
  R-devel readers... OTOH, people like Bill Dunlap will not take
  long to provide it or a better one.
 
  (*) In my solution, the above '...' consists of 17 letters.
  I'll post it later today (CEST time) ... or confirm
  that someone else has done so.
 
  Martin
 
  __
  R-devel@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-22 Thread Joshua Ulrich
On Tue, Aug 21, 2012 at 2:34 PM, Duncan Murdoch
murdoch.dun...@gmail.com wrote:
 On 12-08-18 12:33 PM, Martin Maechler wrote:

 Joshua Ulrich josh.m.ulr...@gmail.com
  on Sat, 18 Aug 2012 10:16:09 -0500 writes:


   I don't know if this is better, but it's the most obvious/shortest
 I
   could come up with.  Transpose the data.frame column to a 'row'
 vector
   and drop the dimensions.

  R identical(nv, drop(t(df)))
   [1] TRUE

 Yes, that's definitely shorter,
 congratulations!

 One gotta is that I'd want a solution that also works when the
 df has more columns than just one...

 Your idea to use  t(.) is nice and perfect insofar as it
 coerces the data frame to a matrix, and that's really the clue:

 Where as  df[,1]  is losing the names,
 the matrix indexing is not.
 So your solution can be changed into

   t(df)[1,]

 which is even shorter...
 and slightly less efficient, at least conceptually, than mine, which has
 been

 as.matrix(df)[,1]

 Now, the remaining question is:  Shouldn't there be something
 more natural to achieve that?
 (There is not, currently, AFAIK).


 I've been offline, so I'm a bit late to this game, but the examples above
 fail when df contains a character column as well as the desired one, because
 everything gets coerced to a character matrix.  You need to select the
 column first, then convert to a matrix, e.g.

 drop(t(df[,1,drop=FALSE]))


That's true, but I was assuming a one-column data.frame, which can be
achieved via:
df - data.frame(VAR=nv,CHAR=letters[1:3],stringsAsFactors=FALSE)
drop(t(df[1]))

That said, I prefer the setNames() solution for its efficiency.

Best,
Josh

 Duncan Murdoch



 Martin


   Best,
   --
   Joshua Ulrich  |  about.me/joshuaulrich
   FOSS Trading  |  www.fosstrading.com


   On Sat, Aug 18, 2012 at 10:03 AM, Martin Maechler
   maech...@stat.math.ethz.ch wrote:
   Today, I was looking for an elegant (and efficient) way to get a
 named
   (atomic) vector by selecting one column of a data frame.  Of
 course,
   the vector names must be the rownames of the data frame.
  
   Ok, here is the quiz, I know one quite cute/slick answer, but
 was
   wondering if there are obvious better ones, and also if this
 should
   not become more idiomatic (hence R-devel):
  
   Consider this toy example, where the dataframe already has only
 one
   column :
  
   nv - c(a=1, d=17, e=101); nv
   a   d   e
   1  17 101
  
   df - as.data.frame(cbind(VAR = nv)); df
   VAR
   a   1
   d  17
   e 101
  
   Now how, can I get 'nv' back from 'df' ?   I.e., how to get
  
   identical(nv, ...)
   [1] TRUE
  
   where .. only uses 'df' (and no non-standard R packages)?
  
   As said, I know a simple solution (*), but I'm sure it is not
   obvious to most R users and probably not even to the majority of
   R-devel readers... OTOH, people like Bill Dunlap will not take
   long to provide it or a better one.
  
   (*) In my solution, the above '...' consists of 17 letters.
   I'll post it later today (CEST time) ... or confirm
   that someone else has done so.
  
   Martin
  
   __
   R-devel@r-project.org mailing list
   https://stat.ethz.ch/mailman/listinfo/r-devel

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Suggestion for Writing R Extensions

2012-08-22 Thread Joshua Ulrich
Hi,

Early in Chapter 1, the manual states:
A computing environment including a number of tools is assumed; the
R Installation and Administration manual describes what is needed.
Under a Unix-alike most of the tools are likely to be present by
default, but Microsoft Windows may require careful setup.

Would it make sense to add links and/or mention the relevant
appendices (A and D) of R Installation and Administration?

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Suggestion for Writing R Extensions

2012-08-22 Thread Oliver Bandel

Hello,


Zitat von Joshua Ulrich josh.m.ulr...@gmail.com (Wed, 22 Aug 2012  
12:35:51 -0500)


[...]

Would it make sense to add links and/or mention the relevant
appendices (A and D) of R Installation and Administration?

[...]

I do not understad, what your question is all about.

Maybe you should be more verbose.

Do you want to make the document better,
or do you have questions on how to understand
what it talks about?


Ciao,
   Oliver

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Suggestion for Writing R Extensions

2012-08-22 Thread Joshua Ulrich
On Wed, Aug 22, 2012 at 2:58 PM, Oliver Bandel
oli...@first.in-berlin.de wrote:
 Hello,


 Zitat von Joshua Ulrich josh.m.ulr...@gmail.com (Wed, 22 Aug 2012
 12:35:51 -0500)

 [...]

 Would it make sense to add links and/or mention the relevant
 appendices (A and D) of R Installation and Administration?

 [...]

 I do not understad, what your question is all about.

 Maybe you should be more verbose.

 Do you want to make the document better,
 or do you have questions on how to understand
 what it talks about?

The sentences I quoted, which you removed from the thread, point the
reader to R Installation and Administration.  Rather than point the
reader to the entire manual, I suggest pointing them to the relevant
appendices (A and D) of R Installation and Administration.


 Ciao,
Oliver

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel