The class dfidx (from the package dfidx) exists. Here the idea is to glue and idx data.frame to another data.frame, and when working with the desired data.frame, the idx object always is glued to it. This is for example used in the mlogit() package. Conversion of this type to data.table fails.
library(mlogit)
data("TravelMode",package="AER")
Mtravel <- dfidx(data = TravelMode,
shape = "long",
choice = "choice",
chid.var = "individual",
alt.var = "mode")
DTtravel <- as.data.table(Mtravel)
Gives the following error message:
Error in .subset2(x, i, exact = exact) :
attempt to select less than one element in get1index
The mlogit package requires such an object to carry out the desired regression. dfidx objects also come with their own '['.
More on this here:
https://cran.microsoft.com/web/packages/dfidx/vignettes/dfidx.html#more_advanced_use_of_dfidx
One could always use dcast() and melt() to change data.tables from wide to long etc. and just use dfidx right before the estimation. Another alternative would be to write a function like the following (which of course is terribly inefficient)
my.as.dt <- function(x){
wide = ncol(x) - 1
long = nrow(x)
DTprep = list()
for(i in 1:wide){
DTprep[[i]] = x[1:long, i][1:long]
}
DT = as.data.table(DTprep)
colnames(DT) = colnames(x)[1:wide]
return(DT)
}
Another approach would be to use setDT, which also delivers a warning:
DT = setDT(Mtravel)
Warning message:
In setDT(Mtravel) :
Some columns are a multi-column type (such as a matrix column): [8]. setDT will retain these columns as-is but subsequent operations like grouping and joining may fail. Please consider as.data.table() instead which will create a new column for each embedded column.
And the following call of that object will deliver an error:
DT
Error in `[.data.table`(x, i, , ) :
Column 8 ['idx'] is a data.frame or data.table; malformed data.table.
Hopefully you can just give as.data.table() and setDT() the power to overwrite this and drop the idx data.frame (potentially with a warning that some commands which require it in the future may fail).
The class
dfidx(from the packagedfidx) exists. Here the idea is to glue and idxdata.frameto anotherdata.frame, and when working with the desireddata.frame, the idx object always is glued to it. This is for example used in themlogit()package. Conversion of this type todata.tablefails.Gives the following error message:
The
mlogitpackage requires such an object to carry out the desired regression. dfidx objects also come with their own '['.More on this here:
https://cran.microsoft.com/web/packages/dfidx/vignettes/dfidx.html#more_advanced_use_of_dfidx
One could always use
dcast()andmelt()to changedata.tables from wide to long etc. and just use dfidx right before the estimation. Another alternative would be to write a function like the following (which of course is terribly inefficient)Another approach would be to use setDT, which also delivers a warning:
And the following call of that object will deliver an error:
Hopefully you can just give
as.data.table()andsetDT()the power to overwrite this and drop the idxdata.frame(potentially with a warning that some commands which require it in the future may fail).