[datatable-help] For each row, aggregate an external vector based on a column value
Frank Erickson
fperickson at wisc.edu
Thu Nov 5 05:50:05 CET 2015
First, I guess you should not create my.dates that way, rather
my.dates = as.Date(c('2014-02-21','2014-04-03','2014-10-21','2015-01-20'))
If you compare str(my.dates) this way and your way, you'll see that sapply
refuses to return a Date-class object (as documented in the "Value" section
of ?sapply).
For the column, I'd do:
dt[, b := findInterval(a, sort(my.dates))]
though this approach is not data.table-specific.
By the way, the >> at the start of each line makes it annoying to
copy-paste your code.
On Wed, Nov 4, 2015 at 10:05 PM, tjonesd289 <t.jonesd289 at gmail.com> wrote:
> 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.
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20151104/6a19f153/attachment-0001.html>
More information about the datatable-help
mailing list