将data.frame转换为data.table列丢失

时间:2022-11-18 21:08:25

Had a case like this. Tried to convert "mtcars" class from data.frame to data.table.

有这样的情况。试图将“mtcars”类从data.frame转换为data.table。

"mtcars" data:

“mtcars”数据:

> mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1

Original class is "data.frame".

原始类是“data.frame”。

> str(mtcars)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Convert to data.table. Found the Column for car brands are gone. Why? How to retain the column of brands?

转换为data.table。发现汽车品牌专栏已经不见了。为什么?如何保留品牌栏目?

> mtcars2 <- data.table(mtcars)
> mtcars2
     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
 1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
 2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
 3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1

Would like to have a data.table with final data format as below -- having a NEW column name "Brands" for the first column of brands. How to code to add the column "Brands" from the original "mtcars" dataset?

想拥有一个data.table,最终数据格式如下 - 第一列品牌的新名称为“Brands”。如何编码从原始“mtcars”数据集添加“品牌”列?

Brands              mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1

1 个解决方案

#1


11  

It's the rownames that are missing, not one of the columns.

这是缺少的rownames,而不是其中一个列。

If you want Brands as a column, the manual approach is:

如果您想将品牌作为专栏,那么手动方法是:

data.table(Brands = rownames(mtcars), mtcars)

Alternately:

交替:

data.table(mtcars, keep.rownames = TRUE)

However, this does not make the resulting data.table have the old rownames, it just makes a column for them, called "rn". This is in the documentation, ?data.table.

但是,这并不会使得结果data.table具有旧的rownames,它只为它们创建一个列,称为“rn”。这是在文档中,?data.table。


Alternately, modify the table in place, for DF = mtcars:

或者,为DF = mtcars修改表格:

setDT(DF, keep.rownames = "Brands")

Minor point: we cannot setDT(mtcars, ...), since mtcars is a built-in table.

小点:我们不能设置DT(mtcars,...),因为mtcars是一个内置表。

#1


11  

It's the rownames that are missing, not one of the columns.

这是缺少的rownames,而不是其中一个列。

If you want Brands as a column, the manual approach is:

如果您想将品牌作为专栏,那么手动方法是:

data.table(Brands = rownames(mtcars), mtcars)

Alternately:

交替:

data.table(mtcars, keep.rownames = TRUE)

However, this does not make the resulting data.table have the old rownames, it just makes a column for them, called "rn". This is in the documentation, ?data.table.

但是,这并不会使得结果data.table具有旧的rownames,它只为它们创建一个列,称为“rn”。这是在文档中,?data.table。


Alternately, modify the table in place, for DF = mtcars:

或者,为DF = mtcars修改表格:

setDT(DF, keep.rownames = "Brands")

Minor point: we cannot setDT(mtcars, ...), since mtcars is a built-in table.

小点:我们不能设置DT(mtcars,...),因为mtcars是一个内置表。