Re: [R] extracting rows of a dataframe

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Fri 02 Dec 2005 - 20:14:42 EST

>>>>> "RH" == Robin Hankin <r.hankin@noc.soton.ac.uk> writes:

    RH> Three questions:

    RH> (1) why is as.vector(a[2,-1]) not a vector? Did you read the help file of as.vector() ?

> 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])
> mode(x)
[1] "list"
> str(x)

`data.frame': 1 obs. of 2 variables:
 $ mass : num 4
 $ height: num 3

Clearly you want:

> x <- as.vector(a[2,-1], mode="numeric")
> str(x)

 num [1:2] 4 3
> x

[1] 4 3

    RH> (2) How come setting names to NULL gives me bad weirdness?
> names(x) <- NULL
> str(x)
`data.frame': 1 obs. of 2 variables:
 $ : num 4
 $ : num 3
> x

  structure("4", class = "AsIs") structure("3", class = "AsIs")

2                              4                              3

With your "names(x)<-NULL" command you are wiping out the names of the variables in your data frame. Looking at print.data.frame(), the answer to the so-called weirdness can presumably be found in format.data.frame().

    RH> (3) Can I structure my dataframe in a better way, so that     RH> this problem does not occur?
Not really, it's more a question of the appropriate use of as.vector(). :-))

Cheers,

        Berwin


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 20:21:42 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:28 EST