Re: [R] extracting rows of a dataframe

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Fri 02 Dec 2005 - 22:11:12 EST

The key things here are:

  1. a data frame is a list with some attributes (unlike a matrix which is (generally) an atomic vector with attributes).
  2. 'as.vector' removes attributes.

Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Robin Hankin wrote:

>Hi
>
>look at the following session, in which I have a dataframe,
>and I want to extract the second row, without the first column.
>
>Everything works as expected until the last line, where I set
>the names of x to NULL, and get a non-desired object (I
>want c(4,3).).
>
>
>Three questions:
>
>(1) why is as.vector(a[2,-1]) not a vector?
>
>(2) How come setting names to NULL gives me bad weirdness?
>
>(3) Can I structure my dataframe in a better way, so that
>this problem does not occur?
>
>
>
>
> > a <- data.frame(male=c(T,T,F),mass=c(1,4,3),height=c(4,3,2))
> > a
> male mass height
>1 TRUE 1 4
>2 TRUE 4 3
>3 FALSE 3 2
> > x <- as.vector(a[2,-1])
> > x
> mass height
>2 4 3
> > names(x) <- NULL
> > x
> structure("4", class = "AsIs") structure("3", class = "AsIs")
>2 4 3
> >
>
>--
>Robin Hankin
>Uncertainty Analyst
>National Oceanography Centre, Southampton
>European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
>______________________________________________
>R-help@stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Dec 02 22:27:29 2005

This archive was generated by hypermail 2.1.8 : Fri 02 Dec 2005 - 23:50:12 EST