Dear All: I imported a table into R. But I want to read a variable (y) from this table conditional on another variable (x=1) in the table. y<-data[,7] x<-data[,3] I want to know the mean of y if corresponding x=1. I tried if (x==1) y3<-y It does not work. Anyone can give a help? Thanks! Xin [[alternative HTML version deleted]]
Xin <jasonshi510 <at> hotmail.com> writes:> > y<-data[,7] > x<-data[,3] > > I want to know the mean of y if corresponding x=1. >Try this: mean(data[data[,3]==1,data[,7]) You can also look at ?subset.
Xin wrote:> Dear All: > > I imported a table into R. But I want to read a variable (y) from this table conditional on another variable (x=1) in the table. > > y<-data[,7] > x<-data[,3] > > I want to know the mean of y if corresponding x=1. > > I tried > > if (x==1) y3<-y > > It does not work. > > Anyone can give a help?Is this homework? Please do read the posting guide and An Introduction to R! Uwe Ligges > Thanks!> > > Xin > [[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.
Possibly Parallel Threads
- how to plot histogram plot and fitted distributions on the same graph
- how to specify the location of tick mark on x axies
- how to read data from the different worksheets in the same Excel file
- How to set read.table variables to vectors?
- read.xls -> rotate data.frame