[R] Mean Help
arun
smartpink111 at yahoo.com
Wed Oct 31 18:13:15 CET 2012
Sure.
set.seed(1)
dat1<-data.frame(Height=sample(150:180,12,replace=TRUE),EyeColor=rep(c("Green","Blue","Brown"),each=4))
dat1[,2]=="Blue"
# [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE # Returns a logical vector which checks whether each row of 2nd column of dat1 is "Blue" or not.
dat1[,1][dat1[,2]=="Blue"] #gives me values of 1st column where the index (logical vector) is TRUE.
#[1] 156 177 179 170
If I use,
dat1[dat1[,2]=="Blue",] #returns the subset of data with all the columns of dat1
# Height EyeColor
#5 156 Blue
#6 177 Blue
#7 179 Blue
#8 170 Blue
#Finally,
mean(dat1[,1][dat1[,2]=="Blue"]) gives the mean of those values I mentioned above.
#[1] 170.5
----- Original Message -----
From: Hard Core <gioxc at hotmail.it>
To: r-help at r-project.org
Cc:
Sent: Wednesday, October 31, 2012 11:19 AM
Subject: Re: [R] Mean Help
Thank you ... can you explain what you've done?
--
View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000p4648013.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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