[R] Hidden information in an object
Rui Barradas
ruipbarradas at sapo.pt
Fri Mar 1 12:24:35 CET 2013
Hello,
Inline.
Em 28-02-2013 12:50, Rasmus Hedegaard escreveu:
>
>
>
>
> 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?
Because Sex == "M" is a logical condition returning TRUE/FALSE. If you
use a logical vector as an index, only the TRUE values will be selected.
Read An Introduction to R, file R-intro.pdf in the doc directory of your
installation of R, section 2.7 - Index Vectors.
Hope this helps,
Rui Barradas
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