[R] Hidden information in an object
David L Carlson
dcarlson at tamu.edu
Thu Feb 28 22:28:38 CET 2013
Below is what happens when you let hotmail format your message using html.
Always use plain text emails.
The command "attach(cats)" told R to put the data.frame in the search path
so that the variables in cats would be visible without specifying the name
of the data frame:
detach(cats)
data(cats)
ratio <- Hwt/Bwt
Error: object 'Hwt' not found
attach(cats)
ratio <- Hwt/Bwt
R can find the variables in cats, but it does not automatically put new
variables in cats so your command creates "ratio" as a separate variable.
You can still use "sex" to subset "ratio" even though they are not in the
same data.frame.
> str(cats)
'data.frame': 144 obs. of 3 variables:
$ Sex: Factor w/ 2 levels "F","M": 1 1 1 1 1 1 1 1 1 1 ...
$ Bwt: num 2 2 2 2.1 2.1 2.1 2.1 2.1 2.1 2.1 ...
$ Hwt: num 7 7.4 9.5 7.2 7.3 7.6 8.1 8.2 8.3 8.5 ...
> str(ratio)
num [1:144] 3.5 3.7 4.75 3.43 3.48 ...
If you want to add ratio to the cats data frame, you need to tell R to put
it there:
> cats$ratio <- Hwt/Bwt
> str(cats)
'data.frame': 144 obs. of 4 variables:
$ Sex : Factor w/ 2 levels "F","M": 1 1 1 1 1 1 1 1 1 1 ...
$ Bwt : num 2 2 2 2.1 2.1 2.1 2.1 2.1 2.1 2.1 ...
$ Hwt : num 7 7.4 9.5 7.2 7.3 7.6 8.1 8.2 8.3 8.5 ...
$ ratio: num 3.5 3.7 4.75 3.43 3.48 ...
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rasmus Hedegaard
> Sent: Thursday, February 28, 2013 6:51 AM
> To: r-help at r-project.org
> Subject: [R] Hidden information in an object
>
> Hello, The dataset "cats" contain information about the heart weight
> ("Hwt"), body weight ("Bwt") and gender ("Sex") of a group of 144 cats.
> I write the following piece of code: library(MASS)attach(cats)ratio <-
> Hwt/Bwtmale <- ratio[Sex == "M"]female <- ratio[Sex == "F"] My question
> is, when I look at the object "ratio", it is just a list of 144 numbers
> with no information about the gender of the cat that the ratio comes
> from, and yet the command "ratio[Sex == "M"]" is able to pick out those
> numbers of "ratio" for which the corresponding cat is male. Why is
> this? If I write the code like library(MASS)cats$ratio <-
> cats$Hwt/cats$Bwtmale <- cats$ratio[cats$Sex == "M"]... it also works,
> which I suppose is because there is a correspondence between the "Sex"
> variable and the "ratio" variable in the cats dataset. Regards,Rasmus
> Hedegaard.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list