<div dir="ltr">First, I guess you should not create my.dates that way, rather<div><br></div><div>my.dates = as.Date(c('2014-02-21','2014-04-03','2014-10-21','2015-01-20'))</div><div><br></div><div>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).</div><div><br></div><div>For the column, I'd do:</div><div><br></div><div>dt[, b := findInterval(a, sort(my.dates))]</div><div><br></div><div>though this approach is not data.table-specific.</div><div><br></div><div>By the way, the >> at the start of each line makes it annoying to copy-paste your code.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 4, 2015 at 10:05 PM, tjonesd289 <span dir="ltr"><<a href="mailto:t.jonesd289@gmail.com" target="_blank">t.jonesd289@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
Say I have data.table:<br>
>> dt <- data.table(a = c(as.Date('2014-01-01'),<br>
>> as.Date('2014-06-01'),as.Date('2014-12-31')))<br>
and a vector of dates:<br>
>> my.dates <-<br>
>> sapply(c('2014-02-21','2014-04-03','2014-10-21','2015-01-20'), as.Date)<br>
<br>
In dt, I want to create a column (b) with the number of items in my.dates<br>
that are on or before the date given in dt$a. In the above example, the<br>
result would be:<br>
>> print(dt$b)<br>
0 2 3<br>
<br>
The following two methods work outside a data.table:<br>
>> d1 = as.Date('2014-06-01')<br>
>> length(which(my.dates <= d1))<br>
2<br>
<br>
>> d2 = as.Date('2014-12-31')<br>
>> sum(my.dates <= d2)<br>
3<br>
<br>
However, I cannot get either of these to work inside of data.table.<br>
When I do:<br>
>> dt[, b:=sum((my.dates)<=a)] # or dt[, b:=length(which((my.dates)<=a))]<br>
Warning message:<br>
In `<.default`((my.dates), a) :<br>
  longer object length is not a multiple of shorter object length<br>
>> print(dt$b)<br>
2 2 2<br>
<br>
Can anyone advise an easy way to do this?<br>
<br>
I was thinking I might first have to move the vector into it's own column in<br>
the data.table, but am not sure how I would even do that.<br>
<br>
Thanks!<br>
-TJ<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://r.789695.n4.nabble.com/For-each-row-aggregate-an-external-vector-based-on-a-column-value-tp4714376.html" rel="noreferrer" target="_blank">http://r.789695.n4.nabble.com/For-each-row-aggregate-an-external-vector-based-on-a-column-value-tp4714376.html</a><br>
Sent from the datatable-help mailing list archive at Nabble.com.<br>
_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
</blockquote></div><br></div>