I'm trying to iron out a formatting bug. My formatC()
function applied against a data frame, using lapply()
, is adding a decimal point to NULL values in the data frame output column header. I do want to keep the NULL values appearing in my output column header, I just don't want a "." added to the end of each NULL output in the column header. That combination of lapply()
and formatC()
is important for formatting the numeric values in the data frame (though in the below reproducible code they are not produced, for the sake of brevity). Please see image at the bottom where you can see the issue.
I've tried following the advice from
CodePudding user response:
Sorry for my comment, I didn't understand the issue. In fact this has nothing to do with formatC
, which anyway is not applied to the column names. The problem is with the usage of "NULL"
as the name of a list component, and as.data.frame
"corrects" this name by default.
> m <- matrix(1, nrow=1, ncol=1, dimnames=list("NULL", "NULL"))
> m # ok
NULL
NULL 1
> data.table(m) # ok
NULL
1: 1
> as.data.frame(data.table(m)) # ok
NULL
1 1
> as.data.frame(lapply(data.table(m), formatC)) # not ok
NULL.
1 1
> data.frame("NULL" = 4) # the problem is here: the "NULL" string is reserved
NULL.
1 4
> lapply(data.table(m), formatC) # look, the name is the NULL object, not the "NULL" string
$`NULL`
[1] "1"
> # you can solve the problem as follows:
> as.data.frame(lapply(data.table(m), formatC), check.names = FALSE)
NULL
1 1