<div dir="ltr">Hi Frank,<div><br></div><div>Not sure about a one-liner, but this seems to do what you want:</div><div><br></div><div>> DT <- data.table(obs=1:7, id=c(1,1,1,4,4,4,4), time=c(3,4,7,5,8,10,15))</div><div>
> DT[,value:=0]</div><div>> DT[!duplicated(id),value:=2]</div><div>> DT[!duplicated(id,fromLast=T),value:=1]</div><div>> DT</div><div><br></div><div><div> obs id time value</div><div>1: 1 1 3 2</div>
<div>2: 2 1 4 0</div><div>3: 3 1 7 1</div><div>4: 4 4 5 2</div><div>5: 5 4 8 0</div><div>6: 6 4 10 0</div><div>7: 7 4 15 1</div></div><div><br></div><div>Seems too straightforward, though, so maybe I am missing something about your problem. Please elaborate if so.</div>
<div><br></div><div><br></div><div>Regards,</div><div>Matt</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 25, 2014 at 8:29 AM, Frank S. <span dir="ltr"><<a href="mailto:f_j_rod@hotmail.com" target="_blank">f_j_rod@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div dir="ltr"><p>Hi all, I'm very novice in data.table management, and I have the following doubt about this data:</p><p> </p><p>> DT <- data.table(obs=1:7, id=c(1,1,1,4,4,4,4), time=c(3,4,7,5,8,10,15))<br>
> DT<br><font face="Courier New"> obs id time<br>1: 1 1 3<br>2: 2 1 4<br>3: 3 1 7<br>4: 4 4 5<br>5: 5 4 8<br>6: 6 4 10<br>7: 7 4 15</font></p><p> </p><p>In general, I know that I can select respectively the first and the last observation within "id" group with: </p>
<p> </p><p><font face="Courier New"><font face="Calibri">First observation:</font> <em>DT[!duplicated(id)] </em></font></p><p><font face="Courier New"><font face="Calibri">Last observation:</font><em><font face="Calibri"> </font>DT[!duplicated(id, fromLast=T)] </em></font></p>
<p> </p><p>But, how can I add a new column, called "value", which contains all zeros except:</p><p>1) The first observation within each "id" group, which is equal to 2</p><p>2) The last observation within each "id" group, which is equal to 1 ?</p>
<p> </p><p><font face="Courier New"> obs id time value<br>1: 1 1 3 2<br>2: 2 1 4 0<br>3: 3 1 7 1<br>4: 4 4 5 2<br>5: 5 4 8 0<br>6: 6 4 10 0<br>7: 7 4 15 1</font></p>
<p><font face="Courier New"></font> </p><p>I've tried with conditionslas, ifelse, etc, but I get an error message. Please, can you help me?</p><p> </p><p>Thanks in advance!!</p><p><font face="Courier New"></font> </p>
<p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p>
<p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p><p><font face="Courier New"></font> </p> </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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br></blockquote></div><br></div>