<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13.333333969116211px">Hm... Eddi does seem to have a point here. While I agree with Frank that once you're used to it, it is rather straightforward to deal with, I can see why one would have the expectation of a vector. ie, that the last of the following `identical` statements should evaluate to `TRUE`</span><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><div> df <- as.data.frame(dt)</div><div><br></div><div> > identical(df[, "a"], dt[, get("a")])</div><div> [1] TRUE</div>
<div> > identical(df[, "a"], dt[["a"]])</div><div> [1] TRUE</div><div> > identical(df[, "a"], dt[, "a", with=FALSE])</div><div> [1] FALSE</div><div><br></div><div>
rm(df)</div></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
-Rick</div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div class="gmail_extra"><br clear="all"><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<div style="font-size:13px">Ricardo Saporta</div><div style="font-size:13px">Graduate Student, Data Analytics</div><div style="font-size:13px"><span style="font-size:13px">Rutgers University, New Jersey</span></div><div style="font-size:13px">
<span style="font-size:13px">e: </span><a href="mailto:saporta@rutgers.edu" style="color:rgb(17,85,204);font-size:13px" target="_blank">saporta@rutgers.edu</a></div><div><br></div></div></div>
<br><br><div class="gmail_quote">On Fri, May 17, 2013 at 4:26 PM, Eduard Antonyan <span dir="ltr"><<a href="mailto:eduard.antonyan@gmail.com" target="_blank">eduard.antonyan@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">Well, looking at the documentation:<div><br></div><div>j: A single column name, single expresson of column names, <code>list()</code> of expressions of column names, an expression or function call that evaluates to <code>list</code> (including <code>data.frame</code> and <code>data.table</code> which are <code>list</code>s, too), or <b>(when <code>with=FALSE</code>) same as <code>j</code> in <code>[.data.frame</code>.</b><br>
</div><div>...</div><div>with:<b> </b>By default <code>with=TRUE</code> and <code>j</code> is evaluated within the frame of <code>x</code>. The column names can be used as variables. <b>When <code>with=FALSE</code>, <code>j</code> works as it does in <code>[.data.frame</code>.</b></div>
<br><div><b><br></b></div><div>The bolded out part of the documentation doesn't match the actual behavior.</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">
On Fri, May 17, 2013 at 2:44 PM, Frank Erickson <span dir="ltr"><<a href="mailto:FErickson@psu.edu" target="_blank">FErickson@psu.edu</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">@Arun and eddi: This question has come up before.<div><a href="http://r.789695.n4.nabble.com/Better-hacks-getting-a-vector-AND-using-with-inserting-chunks-of-rows-tt4666592.html" target="_blank">http://r.789695.n4.nabble.com/Better-hacks-getting-a-vector-AND-using-with-inserting-chunks-of-rows-tt4666592.html</a><br>
</div><div>(And I'm sure there are other times, too.) I can't say I've heard anyone arguing about it, though. :)</div><div><br></div><div>I guess it works that way because</div><div>...in dt[ ,a], j is an expression which evaluates to a vector</div>
<div>...in dt[,"a",with=FALSE] the option turns on the "you must want one or more columns" mode, translating j from "a" to list(a)</div><div><br></div><div>It's unintuitive if you're expecting data frame behavior (you know, drop=TRUE, as Arun mentioned), but if you've already seen dt[,list(a)], it shouldn't be much of a surprise. Adding the drop option, and maybe defaulting it to TRUE when with=FALSE might satisfy eddi's concern...?</div>
</div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, May 17, 2013 at 10:22 AM, Eduard Antonyan <span dir="ltr"><<a href="mailto:eduard.antonyan@gmail.com" target="_blank">eduard.antonyan@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">I don't remember discussing this issue...? What is the conceptual difference between dt[, a] and dt[, "a", with = F] and what does 'drop' have to do with this??</div>
<div><div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Fri, May 17, 2013 at 10:02 AM, Arunkumar Srinivasan <span dir="ltr"><<a href="mailto:aragorn168b@gmail.com" target="_blank">aragorn168b@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>
Eduard, are we discussing the same thing again :)? Wasn't this somehow your question as well.. the discrepancy between:
</div><div><br></div><div>dt[, a] and dt[, "a", with=FALSE]. </div><div><br></div><div>There should be a drop=TRUE/FALSE option (as in the case of data.frame) that should be used when you use `with=FALSE`. Until then, the default option seems to be drop=FALSE, which results in a data.table.</div>
<div><br></div><div>Alexandre, as of now, it could be done as Eduard points out.</div>
<div><div><br></div><div>Arun</div><div><br></div></div><div><div>
<p style="color:#a0a0a8">On Friday, May 17, 2013 at 4:59 PM, Eduard Antonyan wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div><div dir="ltr">Use dt[[colname]], but this seems like a bug to me - I would've thought that dt[, a] and dt[, "a", with = F] should return the exact same thing.</div><div>
<br><br><div>
On Fri, May 17, 2013 at 9:42 AM, Alexandre Sieira <span dir="ltr"><<a href="mailto:alexandre.sieira@gmail.com" target="_blank">alexandre.sieira@gmail.com</a>></span> wrote:<br><blockquote type="cite"><div>
<div style="word-wrap:break-word"><p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto">Sorry if this is a basic question.</p><p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto">
<br></p><p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto"><span style="font-family:sans-serif;line-height:normal">I'm using R 3.0.0 and data.table 1.8.8.</span><span style="font-family:sans-serif;line-height:normal"> </span>The documentation for 'j' states that "<span style="font-family:sans-serif;line-height:normal">A single column or single expression returns that type, usually a vector."</span></p>
<p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto"><span style="font-family:sans-serif;line-height:normal"><br></span></p><p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto">
<span style="font-family:sans-serif;line-height:normal">I am able to obtain this behavior if I know the column name in advance:</span></p><p style="font-family:Helvetica,Arial;font-size:13px;margin:0px;line-height:auto">
<span style="font-family:sans-serif;line-height:normal"><br>
</span></p><p style="margin:0px"></p><p style="margin:0px"><font face="Courier New">> dt = data.table(a=c(1, 2, 3), b=c(4, 5, 6))</font></p><p style="margin:0px"><font face="Courier New">> dt</font></p><p style="margin:0px">
<font face="Courier New"> a b</font></p><p style="margin:0px"><font face="Courier New">1: 1 4</font></p><p style="margin:0px"><font face="Courier New">2: 2 5</font></p><p style="margin:0px"><font face="Courier New">3: 3 6</font></p>
<p style="margin:0px"><font face="Courier New">> str(dt[,a])</font></p><p style="margin:0px"><font face="Courier New"> num [1:3] 1 2 3</font></p><p></p><div><div><div> </div></div></div><div><br></div><div>However, if I don't, no such luck:</div>
<div><br></div><div><div><font face="Courier New">> colname="a"</font></div><div><font face="Courier New">> str(dt[,colname,with=F])</font></div><div><font face="Courier New">Classes ‘data.table’ and 'data.frame':<span style="white-space:pre-wrap"> </span>3 obs. of 1 variable:</font></div>
<div><font face="Courier New"> $ a: num 1 2 3</font></div><div><font face="Courier New"> - attr(*, ".internal.selfref")=<externalptr> </font></div></div><div><br></div><div>If there a way to extract an entire column as a vector if I have the column name as a character scalar?</div>
<div><br></div><div>Thank you!</div><span><font color="#888888"><div><br></div><div><span style="font-family:helvetica,arial;font-size:13px"></span><div style="font-family:Helvetica;line-height:normal">-- </div>
<div style="font-family:Helvetica;line-height:normal"><span style="font-family:arial;font-size:small">Alexandre Sieira</span><br style="font-family:arial;font-size:small"><span style="font-family:arial;font-size:small">CISA, CISSP, ISO 27001 Lead Auditor</span><br style="font-family:arial;font-size:small">
<br style="font-family:arial;font-size:small"><span style="font-family:arial;font-size:small">"The truth is rarely pure and never simple."</span><br style="font-family:arial;font-size:small"><span style="font-family:arial;font-size:small">Oscar Wilde, The Importance of Being Earnest, 1895, Act I</span></div>
</div></font></span></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">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br></div></blockquote></div><br></div>
</div><div><div>_______________________________________________</div><div>datatable-help mailing list</div><div><a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a></div>
<div><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></div></div></div></span>
</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" 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">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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br></blockquote></div><br></div></div>