<div dir="ltr">Yeah, I would use mapply pretty much the same way:<div><br></div><div>dt[, mapply(sub, pattern = shift(col1, type="lead", fill=""), replacement = "", x = col1, USE.NAMES = FALSE)]<br></div><div><br></div><div>I expect it to be faster than a loop, since `dt[]` has some overhead that you would incur for each row of a loop. Once you get used to mapply/Map, it won't seem so hard to read, I think.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 19, 2015 at 11:28 AM, Nicolas Paris <span dir="ltr"><<a href="mailto:niparisco@gmail.com" target="_blank">niparisco@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)">Thanks all,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)">Actualy my use case is a little different</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)"><div class="gmail_default">> dt <- data.table(col1=c("FOOBARBAZ","BARBAZ","BAZ"))</div><div class="gmail_default"> col1</div><div class="gmail_default">1: FOOBARBAZ</div><div class="gmail_default">2: BARBAZ</div><div class="gmail_default">3: BAZ</div><div><br></div></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,51,51)">What I want to get is :</div><div class="gmail_default"><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">> data.table(col1=c("FOO","BAR","BAZ"))</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif"> col1</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">1: FOO</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">2: BAR</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">3: BAZ</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">(I remove next row from actual row)</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">This works :</div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">dt[,col3:=mapply(function(x,y){gsub(x,"",y,fixed=T)},</span></div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">shift(col1,fill=" ",type="lead")</span><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">,</span></div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">col1)]</span><br></div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif"><div class="gmail_default"><div><br></div></div><div class="gmail_default"><br></div><div class="gmail_default">Have you in mind a better solution ? Will it be faster than loop (yet this is less readable)?</div><div class="gmail_default"><br></div><div class="gmail_default">Thanks again</div><div><br></div></font></div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></div><div style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2015-10-19 16:12 GMT+02:00 Frank Erickson <span dir="ltr"><<a href="mailto:fperickson@wisc.edu" target="_blank">fperickson@wisc.edu</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I think `shift` is the best option:<div><br></div><div><span style="font-size:12.8000001907349px">dt <-data.table(col1=c(1,2,3))</span><br></div><div><span style="font-size:12.8000001907349px">dt[, col3 := shift(col1, type="lag") > 2]</span></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 19, 2015 at 10:10 AM, jim holtman <span dir="ltr"><<a href="mailto:jholtman@gmail.com" target="_blank">jholtman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>does this do what you want:</div><div><br></div><div>> dt <-data.table(col1=c(1,2,3))<br>> dt<br> col1<br>1: 1<br>2: 2<br>3: 3<br>> dt[2:nrow(dt), col3:=dt[1:(nrow(dt) - 1), list(col1)]>2]<br>> dt<br> col1 col3<br>1: 1 NA<br>2: 2 FALSE<br>3: 3 FALSE</div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><div><br>Jim Holtman<br>Data Munger Guru<br> <br>What is the problem that you are trying to solve?<br>Tell me what you want to do, not how you want to do it.</div></div>
<br><div class="gmail_quote"><div><div>On Mon, Oct 19, 2015 at 9:59 AM, Nicolas Paris <span dir="ltr"><<a href="mailto:niparisco@gmail.com" target="_blank">niparisco@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">Hello,</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">I wonder if there is a way in data.table (or more generaly in R) to work on previous row without loops</div><div class="gmail_default" style="color:rgb(0,51,51);font-family:tahoma,sans-serif">E.G. something equivalent to :</div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif"><br></font></div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif">dt <-data.table(col1=c(1,2,3))</font><br></div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif">for (i in 2:nrow(dt))</font></div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif">{</font></div><div class="gmail_default"><font color="#003333" face="tahoma, sans-serif">dt[i,col3:=dt[i-1,list(col1)]>2]</font></div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">}</span></div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif"><br></span></div><div class="gmail_default"><span style="color:rgb(0,51,51);font-family:tahoma,sans-serif">Thanks a lot !</span></div></div>
<br></div></div>_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">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>
<br>_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">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>
</div></div></blockquote></div><br></div>
</div></div><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>