[datatable-help] print.data.table's digits argument

Frank Erickson FErickson at psu.edu
Tue Jun 18 00:49:48 CEST 2013


Hi Arun,

Thanks. That looks like a bug in format.data.table to me. I think it should
be function(col) not function(col,...).

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.

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.

Thanks again,

Frank



On Mon, Jun 17, 2013 at 5:04 PM, Arunkumar Srinivasan <aragorn168b at gmail.com
> wrote:

>  The issue seems to come from `data.table:::format.data.table`,
> specifically the lines:
>
>     do.call("cbind", lapply(x, function(col, ...) {
>         if (is.list(col))
>             col = sapply(col, format.item)
>         format(col, justify = justify, ...)
>     }))
>
> Here, it seems that passing `…` inside `lapply` as `function(col, …)`
> somehow loses the information about "digits". That is, if you just do:
>
> That is, consider:
> dt <- data.table(x=1:5, y=rnorm(5))
>
> If you've
> ff <- function(x, …) {
>     do.call("cbind", lapply(x, format, …))
> }
> ff(dt, digits=2)
>
> seems to work.
>
> However, if you do:
>
> ff <- function(x, …) {
>     do.call("cbind", lapply(x, function(y, …) {
>         format(y, …)
>     }))
> }
> ff(dt, digits=2)
>
> won't work!
>
> That said, for now, you can do something like:
>
> as.data.table(do.call("cbind", lapply(dt, function(x) as.numeric(format(x,
> digits=2)))))
>
> until this is resolved..
>
> Arun
>
>  On Monday, June 17, 2013 at 11:12 PM, Frank Erickson wrote:
>
> Hi,
>
> 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*)?
>
> Anyway, here are the first two lines of my dt:
>
> DT <- structure(list(fisyr = 1995:1996, er = list(c(1, 3), c(1, 3)),
>     eg = c(0.0197315833926059, 0.0197315833926059), esal = list(
>         c(2329.89763779528, 2423.6811023622), c(2263.07456978967,
>         2354.16826003824)), fr = list(c(4, 4), c(4, 4)), fg =
> c(0.039310363070415,
>     0.039310363070415), fsal = list(c(2520.85433070866, 2520.85433070866
>     ), c(2448.55449330784, 2448.55449330784)), mr = list(c(5,
>     30), c(5, 30)), mg = c(0.0197779376457164, 0.0197779376457164
>     ), msal = list(c(2571.70078740157, 4215.73622047244),
> c(2497.94263862333,
>     4094.82600382409))), .Names = c("fisyr", "er", "eg", "esal",
> "fr", "fg", "fsal", "mr", "mg", "msal"), class = c("data.table",
> "data.frame"), row.names = c(NA, -2L))
>
> print(DT,digits=4)
> # just DT
> print.data.frame(DT,digits=4)
> #   fisyr   er      eg       esal   fr      fg       fsal    mr      mg
>     msal
> # 1  1995 1, 3 0.01973 2330, 2424 4, 4 0.03931 2521, 2521 5, 30 0.01978
> 2572, 4216
> # 2  1996 1, 3 0.01973 2263, 2354 4, 4 0.03931 2449, 2449 5, 30 0.01978
> 2498, 4095
>
> 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.
>
> 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.
>
> Thanks,
>
> Frank
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130617/df239050/attachment.html>


More information about the datatable-help mailing list