[datatable-help] For each row, aggregate an external vector based on a column value
tjonesd289
t.jonesd289 at gmail.com
Thu Nov 5 04:05:07 CET 2015
Hello,
Say I have data.table:
>> dt <- data.table(a = c(as.Date('2014-01-01'),
>> as.Date('2014-06-01'),as.Date('2014-12-31')))
and a vector of dates:
>> my.dates <-
>> sapply(c('2014-02-21','2014-04-03','2014-10-21','2015-01-20'), as.Date)
In dt, I want to create a column (b) with the number of items in my.dates
that are on or before the date given in dt$a. In the above example, the
result would be:
>> print(dt$b)
0 2 3
The following two methods work outside a data.table:
>> d1 = as.Date('2014-06-01')
>> length(which(my.dates <= d1))
2
>> d2 = as.Date('2014-12-31')
>> sum(my.dates <= d2)
3
However, I cannot get either of these to work inside of data.table.
When I do:
>> dt[, b:=sum((my.dates)<=a)] # or dt[, b:=length(which((my.dates)<=a))]
Warning message:
In `<.default`((my.dates), a) :
longer object length is not a multiple of shorter object length
>> print(dt$b)
2 2 2
Can anyone advise an easy way to do this?
I was thinking I might first have to move the vector into it's own column in
the data.table, but am not sure how I would even do that.
Thanks!
-TJ
--
View this message in context: http://r.789695.n4.nabble.com/For-each-row-aggregate-an-external-vector-based-on-a-column-value-tp4714376.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list