<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div style="" class="markdown-here-wrapper"
data-md-url="Thunderbird">
<p style="margin: 1.2em 0px ! important;">Frank,</p>
<p style="margin: 1.2em 0px ! important;">My understanding is <code
style="font-size: 0.85em; font-family:
Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em;
padding: 0px 0.3em; white-space: nowrap; border: 1px solid
rgb(234, 234, 234); background-color: rgb(248, 248, 248);
border-radius: 3px; display: inline;">unique()</code> will
remove all duplicated records in a keyed data.table, keeping
only the first occurrence. So if you sort your data by
decreasing <code style="font-size: 0.85em; font-family:
Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em;
padding: 0px 0.3em; white-space: nowrap; border: 1px solid
rgb(234, 234, 234); background-color: rgb(248, 248, 248);
border-radius: 3px; display: inline;">marker</code>, you
should achieve what you’re looking for. Note that <code
style="font-size: 0.85em; font-family:
Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em;
padding: 0px 0.3em; white-space: nowrap; border: 1px solid
rgb(234, 234, 234); background-color: rgb(248, 248, 248);
border-radius: 3px; display: inline;">unique()</code> also
comes with a <code style="font-size: 0.85em; font-family:
Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em;
padding: 0px 0.3em; white-space: nowrap; border: 1px solid
rgb(234, 234, 234); background-color: rgb(248, 248, 248);
border-radius: 3px; display: inline;">fromLast=FALSE</code>
argument to keep unique records starting from the last
occurrence instead.</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em; overflow: auto;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: nowrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em;display: block; padding: 0.5em; color: rgb(51, 51, 51); background: none repeat scroll 0% 0% rgb(248, 248, 255);" class="hljs language-R">dt <- data.table(
id = rep(c(<span class="hljs-number">2</span>,<span class="hljs-number">5</span>),c(<span class="hljs-number">3</span>,<span class="hljs-number">2</span>)),
datep = as.Date(c(<span class="hljs-string">'1995-04-20'</span>,<span class="hljs-string">'1995-04-20'</span>, <span class="hljs-string">'1997-02-19'</span>, <span class="hljs-string">'1998-01-15'</span>,<span class="hljs-string">'1998-01-15'</span>)),
marker = c(<span class="hljs-number">2</span>,<span class="hljs-number">8</span>,<span class="hljs-number">5</span>,<span class="hljs-number">7</span>,<span class="hljs-number">5</span>),
group=rep(c(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>),c(<span class="hljs-number">3</span>,<span class="hljs-number">2</span>))
)
setkey(dt, id)
setorder(dt, -marker)
dt <- unique(dt)
</code></pre>
<p style="margin: 1.2em 0px ! important;">—Mel.</p>
<p style="margin: 1.2em 0px ! important;">On 10/22/2014 1:19 PM,
Frank S. wrote:</p>
<p style="margin: 1.2em 0px ! important;"></p>
<div class="markdown-here-exclude">
<p></p>
<blockquote
cite="mid:BAY168-W219E8CD5CA923B238B39A1BA950@phx.gbl"
type="cite">
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>
<div dir="ltr">Dear all,<br>
I'm working with a large database in wich I have some rows
which have identical id and datep variables. Of these<br>
duplicated rows, I only want to keep those row associated to
the maximum value in marker variable. As an example:<br>
DT <- data.table(<br>
id = rep(c(2,5),c(3,2)),<br>
datep = as.Date(c('1995-04-20','1995-04-20',
'1997-02-19', '1998-01-15','1998-01-15')),<br>
marker = c(2,8,5,7,5),<br>
group=rep(c("A","B"),c(3,2))<br>
)<br>
First, I sort by key variables: id, marker<br>
DT[order(id,marker)]<br>
<br>
But afterwards I've tried different things and I'm not able
to what I want:<br>
DT[!duplicated(DT[c('id', 'datep')])]<br>
DT[ !(duplicated %chin% c('id','datep'))]<br>
DT[ !(duplicated %in% c('id','datep'))]<br>
DT[,!(duplicated(DT[c("id","datep")])), by=list(id,datep)]<br>
unique(DT[c('id','datep')])<br>
Please, does anyone know how to do it?<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
datatable-help mailing list
<a class="moz-txt-link-abbreviated" href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></pre>
</blockquote>
<p></p>
</div>
<p style="margin: 1.2em 0px ! important;"></p>
<div
title="MDH:RnJhbmssPGJyPjxicj5NeSB1bmRlcnN0YW5kaW5nIGlzIGB1bmlxdWUoKWAgd2lsbCByZW1vdmUgYWxsIGR1cGxpY2F0ZWQgcmVjb3JkcyBpbiBhIGtleWVkIGRhdGEudGFibGUsIGtlZXBpbmcgb25s
eSB0aGUgZmlyc3Qgb2NjdXJyZW5jZS4gU28gaWYgeW91IHNvcnQgeW91ciBkYXRhIGJ5IGRlY3Jl
YXNpbmcgYG1hcmtlcmAsIHlvdSBzaG91bGQgYWNoaWV2ZSB3aGF0IHlvdSdyZSBsb29raW5nIGZv
ci4gTm90ZSB0aGF0IGB1bmlxdWUoKWAgYWxzbyBjb21lcyB3aXRoIGEgYGZyb21MYXN0PUZBTFNF
YCBhcmd1bWVudCB0byBrZWVwIHVuaXF1ZSByZWNvcmRzIHN0YXJ0aW5nIGZyb20gdGhlIGxhc3Qg
b2NjdXJyZW5jZSBpbnN0ZWFkLjxicj48YnI+YGBgUjxicj5kdCAmbHQ7LSBkYXRhLnRhYmxlKDxi
cj7CoGlkID0gcmVwKGMoMiw1KSxjKDMsMikpLDxicj7CoGRhdGVwID0gYXMuRGF0ZShjKCcxOTk1
LTA0LTIwJywnMTk5NS0wNC0yMC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nIGxpc3QKZGF0YXRhYmxlLWhlbHBAbGlzdHMuci1mb3JnZS5yLXByb2plY3Qub3JnCmh0
dHBzOi8vbGlzdHMuci1mb3JnZS5yLXByb2plY3Qub3JnL2NnaS1iaW4vbWFpbG1hbi9saXN0aW5m
by9kYXRhdGFibGUtaGVscDwvcHJlPgoKPC9ibG9ja3F1b3RlPjxicj4="
style="height:0;font-size:0em;padding:0;margin:0;"></div>
</div>
<pre class="moz-signature markdown-here-signature" cols="72">--
Melanie BACOU
International Food Policy Research Institute
Snr. Program Manager, HarvestChoice
Work +1(202)862-5699
E-mail <a class="moz-txt-link-abbreviated" href="mailto:m.bacou@cgiar.org">m.bacou@cgiar.org</a>
Visit <a class="moz-txt-link-abbreviated" href="http://www.harvestchoice.org">www.harvestchoice.org</a> </pre>
</body>
</html>