[R] Extracting data subset for plot

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Oct 12 09:10:50 CEST 2010


On Tue, 2010-10-12 at 11:44 +0800, elaine kuo wrote:
> Dear list,

> I want to make a plot based on the following information, using the command
> plot.
> 
> variable A for x axis : temperature (range: -20 degrees to 40 degree)
> 
> variable B for y axis : altitude (range: 50 m to 2500 m )

Use the subset argument of plot():

## dummy data
dat <- data.frame(temperature = seq(-20, 40, by = 1),
                  altitude = seq(50, 2500, length = 61))
## use subset to select out the data we want
plot(altitude ~ temperature, data = dat, subset = temperature >= 0)

To understand what this is doing, consider:

with(dat, temperature >= 0)

So temperature >= 0 is yielding a logical vector indicating which rows
of dat are used to form the plot.

If you need to permanently subset your data, there is a subset function
that works the same way:

subset(dat, subset = temperature >= 0)

but yields a data frame of the rows of dat that matched the temperature
condition.

HTH

G

> The data below 0 degree of X variable wants to be erased tentatively.
> 
> Please kindly advise the command to extract the data ranging from 0 degree
> to 40 degrees.
> 
> Thank you.
> 
> 
> 
> Elaine
> 
> 	[[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.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list