Frank S.
De: Bert Gunter
Enviat el: dilluns, 26 de setembre de 2016 23:18:52
Per a: Ista Zahn
A/c: Frank S.; r-help@r-project.org
Tema: Re: [R] Using lapply in R data table
... and just for fun, here's an alternative in which mapply() is used
to vectorize switch(); again, whet
... and just for fun, here's an alternative in which mapply() is used
to vectorize switch(); again, whether you like it may be just a matter
of taste, although I suspect it might be less efficient than ifelse(),
which is already vectorized:
DT <- within(DT,
exposure <- {
Ista:
Aha -- now I see the point. My bad. You are right. I was careless.
However, cut() with ifelse() might simplify the code a bit and/or make
it more readable. To be clear, this is just a matter of taste; e.g.
using your data and a data frame instead of a data table:
> DT <- within(DT,
On Mon, Sep 26, 2016 at 2:48 PM, Bert Gunter wrote:
> I thought that that was a typo from the OP, as it disagrees with his
> example. But the labels are arbitrary, so in fact cut() will do it
> whichever way he meant.
I don't see how cut will do it, at least not conveniently. Consider
this slight
I thought that that was a typo from the OP, as it disagrees with his
example. But the labels are arbitrary, so in fact cut() will do it
whichever way he meant.
-- Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Ber
On Mon, Sep 26, 2016 at 1:59 PM, Bert Gunter wrote:
> This seems like a job for cut() .
I thought that at first two, but the middle group shouldn't be .87 but rather
exposure" = "2007-01-01" - "fini"
so, I think cut alone won't do it.
Best,
Ista
>
> (I made DT a data frame to avoid loading the
This seems like a job for cut() .
(I made DT a data frame to avoid loading the data table package. But I
assume it would work with a data table too, Check this, though!)
> DT <- within(DT, exposure <-
> cut(fini,as.Date(c("2000-01-01","2006-01-01","2006-06-30","2006-12-21")),
> labels= c(1,.87,
Hi Frank,
lapply(DT) iterates over each column. That doesn't seem to be what you want.
There are probably better ways, but here is one approach.
DT[, exposure := vector(mode = "numeric", length = .N)]
DT[fini < as.Date("2006-01-01"), exposure := 1]
DT[fini >= as.Date("2006-01-01") & fini <= as.D
Dear all,
I have a R data table like this:
DT <- data.table(
id = rep(c(2, 5, 7), c(3, 2, 2)),
fini = rep(as.Date(c('2005-04-20', '2006-02-19', '2006-10-08')), c(3, 2, 2)),
group = rep(c("A", "B", "A"), c(3, 2, 2)) )
I want to construct a new variable "exposure" defined as follows:
1) I
9 matches
Mail list logo