Re: [R] Frequency vector

2008-08-12 Thread David Carslaw

how about:
a <- c(1,1,1,1,2,3,4,5,5)
b <- as.data.frame(table(a))
b
  a Freq
1 14
2 21
3 31
4 41
5 52

which you can then select the bits you want from.

David





dennis11 wrote:
> 
> I want to create a vecor with frequencies. 
> 
> I have tried this:
> 
> a <- c(1,1,1,1,2,3,4,5,5)
> b <- table(a)
> print (b[1])
> 
> which results in:
>> print (b[1])
> 1 
> 4 
> 
> The only thing I want is the 4.
> 
> So this seems obvious:
> print (b[1,2])
> 
> but it does not work:
> Error in b[1, 2] : incorrect number of dimensions
> 
> How do I get a vector or how do I refer to the "4" without getting the "1"
> label as well?
> 


-
Institute for Transport Studies
University of Leeds
-- 
View this message in context: 
http://www.nabble.com/Frequency-vector-tp18939882p18940830.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] subset grouped data with quantile and NA's

2008-08-22 Thread David Carslaw

I can't quite seem to solve a problem subsetting a data frame.  Here's a
reproducible example. 

Given a data frame:

dat <- data.frame(fac = rep(c("a", "b"), each = 100),
  value = c(rnorm(130), rep(NA, 70)),
  other = rnorm(200))

What I want is a new data frame (with the same columns as dat) excluding the
top 5% of "value" separately by "a" and "b". For example, this produces the
results I'm after in an array:

sub <- tapply(dat$value, dat$fac, function(x) x[x < quantile(x, probs =
0.95, na.rm = TRUE)]) 

My difficulty is putting them into a data frame along with the other columns
"fac" and "other". Note that quantile will return different length vectors
due to different numbers of NAs for a and b.

There's something I'm just not seeing - can you help?

Many thanks.

David Carslaw

-
Institute for Transport Studies
University of Leeds
-- 
View this message in context: 
http://www.nabble.com/subset-grouped-data-with-quantile-and-NA%27s-tp19102795p19102795.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] text processing for plots

2008-09-05 Thread David Carslaw

Hi R people,

I want to write some functions to automate the plotting of some expressions
that use some of the plotmath capabilities.

An example is a string supplied to a plot call such as:

plot(1, 1, ylab = "pm10 (ug/m3)")

This should actually appear like:

plot(1, 1, ylab = expression("PM"[10] * " (" * mu * "g m" ^-3 * ")"))

 i.e. pollutant name, units

What I would like to do is write a function that will automatically detect
certain strings, reformat them, and supply it as an argument to plot.  I
could just search for the whole string, but I would like something more
flexible because there will be many combinations of pollutant (pm10, nox,
no2...) and units (ug/m3, mg/m3, ng/m3...).

My question is this - is there a way to separately process the pollutant and
units and supply the correct overall expression to plot?

I'm not sure of the best strategy for this having read through previous
posts etc., and would appreciate your help!

Many thanks.

David Carslaw

-
Institute for Transport Studies
University of Leeds
-- 
View this message in context: 
http://www.nabble.com/text-processing-for-plots-tp19331219p19331219.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] how to split a data framed with sequences

2008-09-09 Thread David Carslaw

Hi all,

Given a data frame:

my.df <- data.frame(a = c(1:5, 1:10, 1:20), b = runif(35))

I want to split it by "a" such that I end up with a list containing 3
components i.e. the first containing a = 1 to 5, the second a = 1 to 10 etc.
In other words, sets of sequences of a.

I can't seem to find the right form using the split function - can you help? 

Much appreciated.

David



-
Institute for Transport Studies
University of Leeds
-- 
View this message in context: 
http://www.nabble.com/how-to-split-a-data-framed-with-sequences-tp19388964p19388964.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Creating smooth color regions with panel.contourplot()

2008-09-17 Thread David Carslaw

I think this is a very useful function that I imagine has wide appeal -
thanks.  Using the code below produces the plot OK but when I try and
copy/save it (as a metafile) I receive the following error and an hourglass:

Error: invalid graphics state

[using XP, 2.72, lattice 0.17.13]

Regards,
David



Deepayan Sarkar wrote:
> 
> On 9/15/08, Waichler, Scott R <[EMAIL PROTECTED]> wrote:
>> When I use panel.contourplot() with filled color regions, the coloring
>>  follows the stair-step edge of the underlying grid instead the smooth
>>  contour lines themselves.  How can I get the latter behavior?  I would
>>  guess there is a much simpler way than manually creating polygons with
>>  contourLines(), especially since a contour interval/region can have
>>  holes inside it (different contour intervals).
> 
> Manually creating polygons with contourLines will not really help
> because (1) as you noted, there will be holes, and (2) contours that
> cross edges will be open. The only real solution is to color each
> rectangle individually, and that would be very inefficient in R code
> (grid does not have a C-level interface).
> 
> The good news is that filled.contour() already does this efficiently,
> and you can use that through the gridBase package:
> 
> 
> panel.filledcontour <-
> function(x, y, z, subscripts,
>  at,
>  col.regions = cm.colors,
>  col = col.regions(length(at) - 1),
>  ...)
> {
> stopifnot(require("gridBase"))
> z <- matrix(z[subscripts],
> nrow = length(unique(x[subscripts])),
> ncol = length(unique(y[subscripts])))
> if (!is.double(z)) storage.mode(z) <- "double"
> opar <- par(no.readonly = TRUE)
> on.exit(par(opar))
> if (panel.number() > 1) par(new = TRUE)
> par(fig = gridFIG(), omi = c(0, 0, 0, 0), mai = c(0, 0, 0, 0))
> cpl <- current.panel.limits()
> plot.window(xlim = cpl$xlim, ylim = cpl$ylim,
> log = "", xaxs = "i", yaxs = "i")
> .Internal(filledcontour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)),
> as.double(do.breaks(cpl$ylim, ncol(z) - 1)),
> z, as.double(at), col = col))
> }
> 
> plot.new()
> 
> levelplot(volcano, panel = panel.filledcontour,
>   col.regions = terrain.colors,
>   cuts = 25)
> 
> -Deepayan
> 
> __
> R-help@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.
> 
> 


-
Institute for Transport Studies
University of Leeds
-- 
View this message in context: 
http://www.nabble.com/Creating-smooth-color-regions-with-panel.contourplot%28%29-tp19501169p19540180.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] xyplot axis scaling with dates/times

2009-07-23 Thread David Carslaw

Dear R users,

I'm trying to get a good x-scale and labels on a plot like the one below.

library(lattice)
## make almost a year of hourly data:

mydat <- data.frame(dates = Sys.time() + 3600 * (1:7000), y = runif(7000))

## plot it

xyplot(y ~ dates, data = mydat, type = "l")

Only one x-label is given (May).  I would prefer several because it makes
the plot very difficult to interpret.
 I then thought I could provide additional tick marks:

xyplot(y ~ dates, data = mydat, type = "l", scales = list(x =
list(tick.number = 10)))

But this has no effect.  While I can set ticks at specific points (and
labels) this is difficult because a wide range of time intervals are used.

Any suggestions?

Thanks.

David

-
Institute for Transport Studies
University of Leeds
Leeds 
LS2 9JT
Tel: 0113 343 7522
d.c.cars...@its.leeds.ac.uk


-- 
View this message in context: 
http://www.nabble.com/xyplot-axis-scaling-with-dates-times-tp24620903p24620903.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.