Skip to content

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
#
On Fri, 12 Nov 2004, Giacomo De Giorgi wrote:
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