[datatable-help] data.table question: ave like behavior
Ozan Bakis
obakis at gsu-giam.net
Mon May 24 16:11:57 CEST 2010
Hi,
I have just noticed and installed your package and am trying to understand
how it works.
The commented part in the following example from the reference manual (p.
20) gives an error.
dt <- data.table(a=rep(1:5, 1), b=1:10)
transform(dt, c = a^2)
within(dt, {
b <- rev(b)
c <- a^2
rm(a)
})
dt[, transform, c = max(b), by="a"] # like "ave
It is the last line that gives the following error:
Error in `[.data.table`(dt, , transform, c = max(b), by = "a") :
unused argument(s) (c = max(b))
Actually, I have a large data set where I use ave function. Unfortunately,
it is time consuming. This is why I want to try data.table. I have the
following
data frame:
set.seed(1)
v1 <- c(rep(10, 3), rep(11, 2),rep(12, 2))
v2 <- sample(5:10, 7, replace = T)
v3 <- c(0, 1, 2, 0, 2,2,1)
df <- data.frame(v1, v2, v3)
df
v1 v2 v3
1 10 6 0
2 10 7 1
3 10 8 2
4 11 10 0
5 11 6 2
6 12 10 2
7 12 10 1
I want to add a new column v4 such that its values are equal to the value
of v2 if v3=0 and NA if v3=0 does not in each subgroup of v1. In the above
example,
the final result should be like
v1 v2 v3 v4
1 10 6 0 6
2 10 7 1 6
3 10 8 2 6
4 11 10 0 7
5 11 6 2 7
6 12 10 2 NA
7 12 10 1 NA
Thank you very much,
ozan bakis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20100524/55c6ed4b/attachment.htm>
More information about the datatable-help
mailing list