3

Example

require(data.table)
d <- data.table(a=sample(1:10,100,replace=T))
dcast(d[,.N,a],~a,value.var="N")

The above dcast returns the following message

Error in check_formula(formula, names(data), valnames) : Invalid formula. Cast formula should be of the form LHS ~ RHS, for e.g., a + b ~ c.

The formula has to be written as .~a or ""~a. Then the output looks like this

   .  1  2  3 4  5 6  7 8 9 10
1: . 11 13 12 9 13 9 10 6 9  8

Is there a way of writing the formula so that the . is not written out? I realize I can filter that out using [,-c(1),with=F].

Sotos
  • 51,121
  • 6
  • 32
  • 66
ironv
  • 978
  • 10
  • 25
  • 1
    What is the use case for this? Why is long format not sufficient? – Roland Aug 26 '16 at 14:05
  • 2
    I agree with Roland that there's not an obvious compelling reason for extending dcast to support this, but you have other options if you really want your table across cols for some reason: `d[, as.list(table(a))]` – Frank Aug 26 '16 at 14:07
  • I am trying to solve the same problem. Why does `dcast` include the extra `.` as a column when no grouping column is specified? `data.table::dcast(data.table::data.table(a = letters[1:2], b = c(1,2)), .~ a, value.var = 'b')` – Alex Nov 16 '16 at 03:34

0 Answers0