<html><head><style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
padding:1em;
margin:auto;
background:#fefefe;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
}
h1 {
color: #000000;
font-size: 28pt;
}
h2 {
border-bottom: 1px solid #CCCCCC;
color: #000000;
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777777;
background-color: inherit;
font-size: 14px;
}
hr {
height: 0.2em;
border: 0;
color: #CCCCCC;
background-color: #CCCCCC;
}
p, blockquote, ul, ol, dl, li, table, pre {
margin: 15px 0;
}
a, a:visited {
color: #4183C4;
background-color: inherit;
text-decoration: none;
}
#message {
border-radius: 6px;
border: 1px solid #ccc;
display:block;
width:100%;
height:60px;
margin:6px 0px;
}
button, #ws {
font-size: 12 pt;
padding: 4px 6px;
border-radius: 5px;
border: 1px solid #bbb;
background-color: #eee;
}
code, pre, #ws, #message {
font-family: Monaco;
font-size: 10pt;
border-radius: 3px;
background-color: #F8F8F8;
color: inherit;
}
code {
border: 1px solid #EAEAEA;
margin: 0 2px;
padding: 0 5px;
}
pre {
border: 1px solid #CCCCCC;
overflow: auto;
padding: 4px 8px;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
}
#ws { background-color: #f8f8f8; }
.bloop_markdown table {
border-collapse: collapse;
font-family: Helvetica, arial, freesans, clean, sans-serif;
color: rgb(51, 51, 51);
font-size: 15px; line-height: 25px;
padding: 0; }
.bloop_markdown table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0; }
.bloop_markdown table tr:nth-child(2n) {
background-color: #f8f8f8; }
.bloop_markdown table tr th {
font-weight: bold;
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
.bloop_markdown table tr td {
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
.bloop_markdown table tr th :first-child, table tr td :first-child {
margin-top: 0; }
.bloop_markdown table tr th :last-child, table tr td :last-child {
margin-bottom: 0; }
.bloop_markdown blockquote{
border-left: 4px solid #dddddd;
padding: 0 15px;
color: #777777; }
blockquote > :first-child {
margin-top: 0; }
blockquote > :last-child {
margin-bottom: 0; }
.send { color:#77bb77; }
.server { color:#7799bb; }
.error { color:#AA0000; }</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div class="bloop_markdown"><blockquote>
<p>DTNEW <- DT[ , {<br>
if (all(start <= opening)){<br>
result <- list(start, end, t.dif= unclass(round(difftime(end, start)/365.25,1)), value)<br>
} else {<br>
result <- list(start, end, t.dif= 20, value)<br>
}<br>
result}, by=ID] </p>
<p>Why can I not keep the column names?</p>
</blockquote>
<p>When we do:</p>
<pre><code>DT[, list(x, y), by=z]
</code></pre>
<p>the <code>j-expression</code> returns an <em>unnamed</em> list. But we understand it as a straightforward <code>list()</code> scenario and extract the symbols and assign them as names in output/result.</p>
<p>But what happens within <code>{ ... }</code> is more complicated and therefore is hard to extract the names to set names in result.</p>
<blockquote>
<p>DTNEW <- DT[ , {<br>
if (all(start <= opening)){<br>
result <- list(start, end, t.dif= unclass(round(difftime(end, start)/365.25,1)), value)<br>
} else {<br>
result <- list(start, end, t.dif= 20, value)<br>
}<br>
result[!(t.dif == value)]}, by=ID] </p>
<p>But R does not find the variable t.dif !!</p>
</blockquote>
<p>But <code>result</code> is a <code>list</code>. You should be doing <code>result[!result$t.dif == value]</code>. That is:</p>
<pre><code>DTNEW <- DT[ , {
if (all(start <= opening)){
result <- list(start, end, t.dif= unclass(round(difftime(end, start)/365.25,1)), value)
} else {
result <- list(start, end, t.dif= 20, value)
}
result[!result$t.dif == value]}, by=ID]
</code></pre>
<p></p></div><div class="bloop_original_html"><style>body{font-family:Helvetica,Arial;font-size:13px}</style><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><div><div class="hmmessage"><div dir="ltr"><p abp="4859"><font face="Calibri" abp="4933"><br></font></p></div></div></div></div> <div id="bloop_sign_1411384821003868160" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">Arun</div></div> <div style="color:black"><br>From: <span style="color:black">Frank S.</span> <a href="mailto:f_j_rod@hotmail.com"><f_j_rod@hotmail.com></a><br>Reply: <span style="color:black">Frank S.</span> <a href="mailto:f_j_rod@hotmail.com"><f_j_rod@hotmail.com>></a><br>Date: <span style="color:black">September 22, 2014 at 1:16:48 PM</span><br>To: <span style="color:black">datatable-help@lists.r-forge.r-project.org</span> <a href="mailto:datatable-help@lists.r-forge.r-project.org"><datatable-help@lists.r-forge.r-project.org>></a><br>Subject: <span style="color:black"> [datatable-help] Two short questions about the operation of data table <br></span></div><br> <blockquote type="cite" class="clean_bq"><span><div class="hmmessage"><div></div><div>
<title></title>
<div dir="ltr">
<p abp="4849">Hello to everyone,</p>
<p abp="4850"> </p>
<p abp="4851">Let's consider, just by way of example, the following
date and data table:</p>
<p abp="4852"> </p>
<p abp="4853"><font face="Courier New,sans-serif" size="2" style="font-size: 10pt;" abp="4921">opening <-
as.Date("1990-01-01")<br abp="4922">
DT <- data.table(ID=c(1,2,3),<br abp="4923">
start=c("1985-01-01","1993-07-15","1993-05-17"),<br abp="4924">
end=c("1992-05-01","1997-02-25","2002-01-01"),<br abp="4925">
value=c(7.8, 3.2, 20.0))</font></p>
<p abp="4858"> </p>
<p abp="4859">FIRST QUESTION:</p>
<p abp="4859"> </p>
<p abp="4859">If I execute:</p>
<p abp="4859"> </p>
<p abp="4859"><font face="Courier New,sans-serif" size="2" style="font-size: 10pt;" abp="4926">DTNEW <- DT[ , { <br abp="4927">
if (all(start <= opening)){ <br abp="4928">
result <- list(start,
end, t.dif= unclass(round(difftime(end, start)/365.25,1)),
value) <br abp="4929">
} else { <br abp="4930">
result <- list(start,
end, t.dif= 20, value)<br abp="4931">
} <br abp="4932">
result}, by=ID]</font></p>
<p abp="4859"> </p>
<p abp="4859"><font face="Calibri" abp="4933">Why can I not keep
the column names?</font></p>
<p abp="4859"><font face="Courier New,sans-serif" abp="4934">
ID
t.dif <br abp="4935">
1: 1 1985-01-01 1992-05-01 7.3 7.8<br abp="4936">
2: 2 1993-07-15 1997-02-25 20.0 3.2<br abp="4937">
3: 3 1993-05-17 2002-01-01 20.0 20.0</font><br abp="4938"></p>
<p abp="4859">SECOND QUESTION:</p>
<p abp="4859"> </p>
<p abp="4859">I would want to remove rows where <font face="Courier New,sans-serif">t.dif=value</font> in the final result.
Then, I tried:</p>
<p abp="4859"> </p>
<p abp="4859"><font face="Courier New" size="2" abp="4939">DTNEW
<- DT[ , { <br abp="4940"></font><font face="Courier New" size="2" abp="4941"> if (all(start <=
opening)){ <br abp="4942">
result <- list(start, end,
t.dif= unclass(round(difftime(end, start)/365.25,1)),
value) <br abp="4943">
} else { <br abp="4944">
result <- list(start, end,
t.dif= 20, value)<br abp="4945">
} <br abp="4946">
result[!(t.dif == value)]}, by=ID]</font></p>
<p abp="4859"> </p>
<p abp="4859">But R does not find the variable<font face="Courier New,sans-serif" abp="4947"> t.dif</font> !!</p>
<p abp="4859"> </p>
<p abp="4859">Thank you for your time to all of the members of
the list!!</p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
<p abp="4859"> </p>
</div>
_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</div></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>