<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Thanks Frank for the your ingenious solution!<BR> <BR><b class="b4"> </b><br> <BR><div><hr id="stopSpelling">From: f_j_rod@hotmail.com<br>To: caneff@gmail.com; eduard.antonyan@gmail.com<br>Date: Tue, 25 Aug 2015 18:37:45 +0200<br>CC: datatable-help@lists.r-forge.r-project.org<br>Subject: Re: [datatable-help] Conditional replacement using a single line<br><br>

<style><!--
.ExternalClass .ecxhmmessage P {
padding:0px;
}

.ExternalClass body.ecxhmmessage {
font-size:12pt;
font-family:Calibri;
}

--></style>
<div dir="ltr">Thank you Eduard and Chris for your very rapid answers!!!<br> <br>Frank S.<br> <br><div><hr id="ecxstopSpelling">From: caneff@gmail.com<br>Date: Tue, 25 Aug 2015 16:27:39 +0000<br>Subject: Re: [datatable-help] Conditional replacement using a single line<br>To: eduard.antonyan@gmail.com; f_j_rod@hotmail.com<br>CC: datatable-help@lists.r-forge.r-project.org<br><br><div dir="ltr"><div>You almost had it right with number 2:</div><div><br></div><div> DT[,value:=ifelse(!duplicated(id),0,2)]</div><div><br></div></div><br><div class="ecxgmail_quote"><div dir="ltr">On Tue, Aug 25, 2015 at 12:26 PM Eduard Antonyan <<a href="mailto:eduard.antonyan@gmail.com">eduard.antonyan@gmail.com</a>> wrote:<br></div><blockquote class="ecxgmail_quote" style="padding-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;"><div dir="ltr">DT[, value := c(2, rep(0, .N-1)), by = id]<br></div><div class="ecxgmail_extra"><br><div class="ecxgmail_quote">On Tue, Aug 25, 2015 at 11:17 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="ecxgmail_quote" style="padding-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;">


<div><div dir="ltr">Hello everyone,<br>I ask a question directly linked with other question which I was answered in this list a year ago. Let the data table be:<br>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> <br>Now I add a new column, called "value", which contains all zeros except the first observation within each "id" group, which is equal to 2. <br>One possible solution I was given is the following two code lines:<br>> DT[ , value:=0]<br>> DT[!duplicated(id), value:=2]<br> <br>But I wonder if it is possible to do the same ON A SINGLE CODE LINE. I have tried different options but they doesn't work:<br>1) DT[ , value:= c(0, 2), by=id]<br>2) DT[ , value:= ifelse( DT[!duplicated(id)]==T, 2, 0), by=id]<br> <br>Many thanks!<br>                                        </div></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" target="_blank" rel="noreferrer">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br></blockquote></div><br></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" target="_blank" rel="noreferrer">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></blockquote></div></div>                                       </div>
<br>_______________________________________________
datatable-help mailing list
datatable-help@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</div>                                           </div></body>
</html>