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
>>>>> "RH" == Robin Hankin <r.hankin at 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)) > amale 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> xstructure("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 ========================== Full address ===========================Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Mathematics and Statistics +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin
Hi or maybe x <- as.numeric(a[2,-1]) can suit to your needs. Petr On 2 Dec 2005 at 17:14, Berwin A Turlach wrote: From: Berwin A Turlach <berwin at maths.uwa.edu.au> Date sent: Fri, 2 Dec 2005 17:14:42 +0800 To: Robin Hankin <r.hankin at noc.soton.ac.uk> Copies to: RHelp <r-help at stat.math.ethz.ch> Subject: Re: [R] extracting rows of a dataframe Send reply to: berwin at maths.uwa.edu.au <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> >>>>> "RH" == Robin Hankin <r.hankin at 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 > > ========================== Full address ===========================> Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) > School of Mathematics and Statistics +61 (8) 6488 3383 (self) > The University of Western Australia FAX : +61 (8) 6488 1028 35 > Stirling Highway Crawley WA 6009 > e-mail: berwin at maths.uwa.edu.au Australia > http://www.maths.uwa.edu.au/~berwin > > ______________________________________________ > R-help at 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.htmlPetr Pikal petr.pikal at precheza.cz
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 at 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 at 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 > > > > >