[R] Casting "character" to "Date" using the "as" function

2010-02-23 Thread david.schruth
Hello,

I'm trying to write a function which, among other things, attempts to
convert variables of type 'character' into various other classes like
'integer', 'numeric', 'character' & "Date", depending on the class of
a second parameter to this same function.   Converting from character
to Date is no problem with the "as.Date" function:

> date.var.char <- as.character("2009-10-21")
> as.Date(date.var.char)
 [1]  "2010-02-23"

But when I try to do the equivalent using just "as" I get an error

> date.var.Date <- as.Date("2009-10-21")
> as(date.var.char,   class(date.var.Date))
 Error in as("2009-10-21", "Date"): no method or default for coercing
"character" to "Date"

The same is true for POSIXt, POSIXlt, & POSIXct

Is there something that I can do without writing those extra 2 lines
of code to check and see if 'date.var.Date' is actually of the 'Date'
class? Or is this a bug?

Thanks,

Dave

__
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] producing a master table from list of tables

2009-12-31 Thread david.schruth
Hello,

I'm trying to construct an overall summary table from a list of
tables.

my.list <- list(x=c('a','b','a','b','c'), y=c
('a','d','c','a','b','d'),z=c('d','d','c'))
my.table.list <- lapply(my.list, table)

normally this might be really easy:

master.table <- table(unlist(my.list))

But as it turns out I'm writing a function which allows the passing of
the table list in the form of 'my.table.list' above and I don't have
access to the 'my.list' form.   Does anybody know of an elegant way to
make 'master.table' without too  much code or direct reconstruction of
'my.list'  (e.g. the following line)

my.list <- lapply(my.table.list, function(x) rep(names(x),x))

Thanks in advance,

Dave

__
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] producing a master table from list of tables

2009-12-31 Thread david.schruth
No, that's basically the solution I had already come up with (see the
last line of code from my original post)

but, Yes, I'm thinking this might be the simplest way (even though it
might not scale well to millions of elements )

On Dec 31, 1:17 pm, Dennis Murphy  wrote:
> Hi,
>
> Is this what you wanted?
>
> untable <- function(tab) rep(names(tab), as.vector(tab))
> lapply(my.table.list, untable)
> $x
> [1] "a" "a" "b" "b" "c"
>
> $y
> [1] "a" "a" "b" "c" "d" "d"
>
> $z
> [1] "c" "d" "d"
>
> HTH,
> Dennis
>
>
>
> On Thu, Dec 31, 2009 at 12:37 PM, david.schruth  wrote:
> > Hello,
>
> > I'm trying to construct an overall summary table from a list of
> > tables.
>
> > my.list <- list(x=c('a','b','a','b','c'), y=c
> > ('a','d','c','a','b','d'),z=c('d','d','c'))
> > my.table.list <- lapply(my.list, table)
>
> > normally this might be really easy:
>
> > master.table <- table(unlist(my.list))
>
> > But as it turns out I'm writing a function which allows the passing of
> > the table list in the form of 'my.table.list' above and I don't have
> > access to the 'my.list' form.   Does anybody know of an elegant way to
> > make 'master.table' without too  much code or direct reconstruction of
> > 'my.list'  (e.g. the following line)
>
> > my.list <- lapply(my.table.list, function(x) rep(names(x),x))
>
> > Thanks in advance,
>
> > Dave
>
> > __
> > r-h...@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.
>
>         [[alternative HTML version deleted]]
>
> __
> r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
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] contour( ..., method='edge') incompatible with xlim & ylim

2009-02-11 Thread david.schruth
Hello,

A colleague of mine in our oceanography lab has pointed out a slightly
annoying imperfection with the contour plotting function.   It appears
that the 'edge' option for the 'method' parameter doesn't work very
well (or at all?) when xlim and ylim are also set.

The following code should recreate and demonstrate the problem:
---
library(marelac)
data(Bathyometry)

B <- Bathymetry

xrange <- c(-150, -120)
yrange <- c(40,60)

par(mfrow=c(1,2))

### using xlim and ylim 
contour(B, xlim=xrange, ylim=yrange, method='edge', main='x/y
limited')

 using subsetting 
x <- !is.na(cut(B$x,breaks=xrange))
y <- !is.na(cut(B$y,breaks=yrange))
contour(B$x[x],B$y[y],B$z[x,y], method='edge', main="x/y subset")

Am I missing something or is this really a bug?  If so, I'd imagine
it's a relatively easy one to fix.

Thanks,

David Schruth
dschr...@u.washington.edu
Bioinformatics Research Consultant
The Center for Environmental Genomics
Department of Oceanography
University of Washington

__
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.