An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20041112/89082bfc/attachment.pl
Simple operation on a subset of data
3 messages · Giacomo De Giorgi, Dimitris Rizopoulos, Thomas Lumley
Hi Giacomo, "An Introduction to R" is very useful document for all these things! Look at ?subset and try: dat <- data.frame(x=sample(1:2, 10, TRUE), y=sample(c(4,5), 10, TRUE), z=rnorm(10)) ###### summary(dat$z) summary(subset(dat, x==1 & y==4, select=z)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Giacomo De Giorgi " <uctpgde at ucl.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Friday, November 12, 2004 1:04 PM Subject: [R] Simple operation on a subset of data
Sorry for the silly question. I am trying to perform a simple operation on a subsample of my data loaded as data and attached: data<-read.dta(name file) attach(data) Say x=1,2 and y=4,5 I want to summarize z only if x=1 & y=4. I thought that the way to do that would be to write if((x=1) & (y=4)) summary(z) butwhen I do this the result I get is for the whole data (irrespective of the conditions imposed). Can anyone help? Thanks Giacomo [[alternative HTML version deleted]]
______________________________________________ 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
On Fri, 12 Nov 2004, Giacomo De Giorgi wrote:
Say x=1,2 and y=4,5 I want to summarize z only if x=1 & y=4. I thought that the way to do that would be to write if((x=1) & (y=4)) summary(z) butwhen I do this the result I get is for the whole data (irrespective of the conditions imposed). Can anyone help?
There are actually worse problems with
if((x=1) & (y=4)) summary(z)
than that: you have just set x to 1 and y to 4.
You can do
summary(z[(x==1) & (y==4)])
to get the answer you want (or various other things)
You really need to read the Introduction to R, which will tell you, among
other things, what the = operator does.
-thomas