Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is data.table not making a data.table from a table although data.frame does?

I have following data and code:

mydf
  grp categ condition value
1   A     X         P     2
2   B     X         P     5
3   A     Y         P     9
4   B     Y         P     6
5   A     X         Q     4
6   B     X         Q     5
7   A     Y         Q     8
8   B     Y         Q     2
> 
> 
mydf = structure(list(grp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L), .Label = c("A", "B"), class = "factor"), categ = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("X", "Y"), class = "factor"), 
    condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("P", 
    "Q"), class = "factor"), value = c(2, 5, 9, 6, 4, 5, 8, 2
    )), .Names = c("grp", "categ", "condition", "value"), out.attrs = structure(list(
    dim = structure(c(2L, 2L, 2L), .Names = c("grp", "categ", 
    "condition")), dimnames = structure(list(grp = c("grp=A", 
    "grp=B"), categ = c("categ=X", "categ=Y"), condition = c("condition=P", 
    "condition=Q")), .Names = c("grp", "categ", "condition"))), .Names = c("dim", 
"dimnames")), row.names = c(NA, -8L), class = "data.frame")

However, following works for data.frame but not for data.table:

> data.frame(with(mydf, table(grp, categ, condition)))
  grp categ condition Freq
1   A     X         P    1
2   B     X         P    1
3   A     Y         P    1
4   B     Y         P    1
5   A     X         Q    1
6   B     X         Q    1
7   A     Y         Q    1
8   B     Y         Q    1
> 
> data.table(with(mydf, table(grp, categ, condition)))
   V1
1:  1
2:  1
3:  1
4:  1
5:  1
6:  1
7:  1
8:  1
> 

Am I making some mistake here or do I need to correct the data.table command to get other variables? It is highly unlikely that there is a bug here. With 2 variables it works all right:

> data.table(with(mydf, table(grp, categ)))
   categ grp N
1:     A   X 2
2:     B   X 2
3:     A   Y 2
4:     B   Y 2
> 
> 
> data.frame(with(mydf, table(grp, categ)))
  grp categ Freq
1   A     X    2
2   B     X    2
3   A     Y    2
4   B     Y    2

Thanks for your help.

like image 555
rnso Avatar asked Feb 13 '15 12:02

rnso


People also ask

How do I convert a Dataframe to a data table in R?

Method 1 : Using setDT() method table package, which needs to be installed in the working space. The setDT() method can be used to coerce the dataframe or the lists into data. table, where the conversion is made to the original dataframe. The modification is made by reference to the original data structure.

How do you convert a table into a data frame?

data. frame() function converts a table to a data frame in a format that you need for regression analysis on count data. If you need to summarize the counts first, you use table() to create the desired table. Now you get a data frame with three variables.

Is data table DT == true?

data. table(DT) is TRUE. To better description, I put parts of my original code here. So you may understand where goes wrong.


Video Answer


1 Answers

Fixed in commit #1760 of data.table v1.9.5. Closes #1043.

like image 103
Arun Avatar answered Nov 09 '22 22:11

Arun