<div dir="ltr">Hi Arun,<div><br></div><div>Thanks. That looks like a bug in format.data.table to me. I think it should be function(col) not function(col,...).</div><div><br></div><div style>Your last line does not work verbatim on my example, but that looks like the way I should go about it. One additional wrinkle: with print.data.frame, it also performs formatting recursively (fixing my list columns), which would call for rapply with the "replace" option, I guess. I tried it, but couldn't get it to work.</div>
<div style><br></div><div style>FYI, it looks like you missed the reply to all, but I'm sending this back to data.table-help, so it's all good.</div><div style><br></div><div style>Thanks again,</div><div style><br>
</div><div style>Frank</div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 17, 2013 at 5:04 PM, 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>
The issue seems to come from `data.table:::format.data.table`, specifically the lines:
</div><div><br></div><div><div> do.call("cbind", lapply(x, function(col, ...) {</div><div> if (is.list(col)) </div><div> col = sapply(col, format.item)</div><div> format(col, justify = justify, ...)</div>
<div> }))</div><div><br></div></div><div>Here, it seems that passing `…` inside `lapply` as `function(col, …)` somehow loses the information about "digits". That is, if you just do:</div><div><br></div><div>
That is, consider: </div>
<div>dt <- data.table(x=1:5, y=rnorm(5))</div><div><br></div><div>If you've </div><div>ff <- function(x, …) {</div><div> do.call("cbind", lapply(x, format, …))</div><div>}</div><div>ff(dt, digits=2)</div>
<div><br></div><div>seems to work.</div><div><br></div><div>However, if you do:</div><div><br></div><div>ff <- function(x, …) {</div><div> do.call("cbind", lapply(x, function(y, …) {</div><div> format(y, …)</div>
<div> }))<span style="white-space:pre-wrap"> </span></div><div>}</div><div><div>ff(dt, digits=2)</div><div><br></div><div>won't work!</div><div><br></div><div>That said, for now, you can do something like:</div><div>
<br></div><div>as.data.table(do.call("cbind", lapply(dt, function(x) as.numeric(format(x, digits=2)))))</div><div><br></div><div>until this is resolved..</div><div><br></div><div>Arun</div><div><br></div></div>
<div>
<div class="h5">
<p style="color:#a0a0a8">On Monday, June 17, 2013 at 11:12 PM, Frank Erickson wrote:</p>
</div></div><blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div><div class="h5"><div><div dir="ltr">Hi,<div><br></div><div>I have a data.table with list and float columns. I want to print it with the floats rounded to display only a few significant digits. Using getAnywhere(print.data.table), I see that digits is an option. However, when I use it, it seems to have no effect. Is there a special way to pass arguments to hidden functions like this (on the methods(print) list it shows up as print.data.table*)?</div>
<div><br></div><div>Anyway, here are the first two lines of my dt:</div><div><br></div><div><div><font face="courier new, monospace">DT <- structure(list(fisyr = 1995:1996, er = list(c(1, 3), c(1, 3)), </font></div>
<div><font face="courier new, monospace"> eg = c(0.0197315833926059, 0.0197315833926059), esal = list(</font></div><div><font face="courier new, monospace"> c(2329.89763779528, 2423.6811023622), c(2263.07456978967, </font></div>
<div><font face="courier new, monospace"> 2354.16826003824)), fr = list(c(4, 4), c(4, 4)), fg = c(0.039310363070415, </font></div><div><font face="courier new, monospace"> 0.039310363070415), fsal = list(c(2520.85433070866, 2520.85433070866</font></div>
<div><font face="courier new, monospace"> ), c(2448.55449330784, 2448.55449330784)), mr = list(c(5, </font></div><div><font face="courier new, monospace"> 30), c(5, 30)), mg = c(0.0197779376457164, 0.0197779376457164</font></div>
<div><font face="courier new, monospace"> ), msal = list(c(2571.70078740157, 4215.73622047244), c(2497.94263862333, </font></div><div><font face="courier new, monospace"> 4094.82600382409))), .Names = c("fisyr", "er", "eg", "esal", </font></div>
<div><font face="courier new, monospace">"fr", "fg", "fsal", "mr", "mg", "msal"), class = c("data.table", </font></div><div><font face="courier new, monospace">"data.frame"), row.names = c(NA, -2L))</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">print(DT,digits=4)</font></div><div><font face="courier new, monospace"># just DT</font></div><div><font face="courier new, monospace">print.data.frame(DT,digits=4)</font></div>
<font face="courier new, monospace"># fisyr er eg esal fr fg fsal mr mg msal<br># 1 1995 1, 3 0.01973 2330, 2424 4, 4 0.03931 2521, 2521 5, 30 0.01978 2572, 4216<br># 2 1996 1, 3 0.01973 2263, 2354 4, 4 0.03931 2449, 2449 5, 30 0.01978 2498, 4095</font><div>
<br></div><div>Printing as a data.frame does the rounding/shortening, but the list columns look ugly (thanks to that extra space), so I'd rather see a data.table output.</div><div><br></div><div>It's a tiny data.table, so if anyone knows a fancy lapply trick for this, that'd be cool. I'm going to try to find one myself now.</div>
<div><br></div><div>Thanks,</div><div><br></div><div>Frank</div></div></div>
</div></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>
</blockquote></div><br></div>