Transpose a data table using reshape2: dcast

I reshape2:::dcast data table and decided to use reshape2:::dcast , however, I am concerned about the strange handling of data.table ... here is a toy data set that replicates the behavior:

 > library(data.table) > library(reshape2) > DT <- structure(list(STORE = c(32123L, 32469L, 33177L, 33484L, 34772L, 34875L), VOLUME = c(343.87205, 599.78335, 1665.90895, 712.0343, 465.6489, 622.5189)), .Names = c("STORE", "VOLUME"), sorted = "STORE", class = c("data.table", "data.frame"), row.names = c(NA, -6L)) > (R1 <- dcast(DT, . ~ STORE, value.var = "VOLUME")) . 32123 32469 33177 33484 34772 34875 1 . 343.8721 599.7834 1665.909 712.0343 465.6489 622.5189 

Absolutely, exactly what I want it to look like (dropping a column . Is not big) - but it is no longer classified as data.table:

 > class(R1) [1] "data.frame" 

I tried to explicitly call the data.tables method in the data.table package, but R now doesn't like the formula:

 > data.table:::dcast.data.table(DT, . ~ STORE, value.var = "VOLUME") Error in data.table:::dcast.data.table(DT, . ~ STORE, value.var = "VOLUME") : LHS of formula evaluates to 'character(0)', invalid formula. 2: stop("LHS of formula evaluates to 'character(0)', invalid formula.") 1: data.table:::dcast.data.table(DT, . ~ STORE, value.var = "VOLUME") 

This can be easily circumvented by creating a column column for the dummy, which must be deleted after transposition:

 > DT[, "dummy" := NA] > (R2 <- data.table:::dcast.data.table(DT, dummy ~ STORE, value.var = "VOLUME")) dummy 32123 32469 33177 33484 34772 34875 1: NA 343.8721 599.7834 1665.909 712.0343 465.6489 622.5189 > class(R2) [1] "data.table" "data.frame" 

However, the fact that data.table:::dcast.data.table had no processing for the NULL LHS casting formula makes me believe that I am doing all this wrong - is there a way to "data.table" for this?

+6
source share
1 answer

In data.table, you do not need dcast to make the change you need. Just use

  DT[, as.list(setNames(VOLUME, STORE))] 

Explanation:

In [.data.table , if the expression j (ie, DT[i, j] ) is list , the list is treated as output columns.
List names are taken as column names (missing names filled with V* )

eg:

  DT[, j= list(ABC=123, greetings="Hello World", 1, alpha = 0.9995)] # ABC greetings V3 alpha # 1: 123 Hello World 1 0.9995 

Thus, in the solution:

  • setnNames ( base R function. Not to be confused with setnames from the data.table package) uses one vector as the name of another.
  • converting the vector to list tells [.data.table treat them as columns.

(alternatively, you can just save it as a named vector if that is what you need for your purposes)


problem with dcast

As for the difference between reshape2::dcast and dcast.data.table , as @David Arenburg points out in the comments above, this was covered in the development version of data.table on github and will be on CRAN with V1.9.6

 library (devtools) install_github("Rdatatable/data.table") 
+6
source

Source: https://habr.com/ru/post/987422/


All Articles