It appears that one can add/delete a column to a data.table
in-place, i.e., without copying all the other columns over to a new table.
似乎可以向數據添加/刪除列。表就地,即。,而無需將所有其他列復制到新表中。
Is it possible to do that with a vanilla data.frame
?
有可能用一個普通的數據。
PS. I know how to add/delete columns "functionally", i.e., creating a new frame without modifying the original one.
我知道如何“功能性地”添加/刪除列。,創建一個新的框架而不修改原來的框架。
33
You can delete or modify an existing column from a data.frame
by reference with data.table::set
. I doubt you can add a column without making a copy. The reason that you can add a column to a data.table
without making a copy is that data.table
over allocates memory. See ?alloc.col
for more.
可以使用data.table::set從data.frame中刪除或修改現有列。我懷疑你不復制就能添加一列。您可以向數據添加列的原因。沒有復制的表就是數據。表分配內存。看到了什么? alloc。坳。
R> library(data.table)
R> data(mtcars)
R> tracemem(mtcars)
[1] "<0x59fef68>"
R> set(mtcars, j="mpg", value=NULL) # remove a column
R> set(mtcars, j="cyl", value=rep(42, 32)) # modify a column
R> untracemem(mtcars)
R> str(mtcars)
'data.frame': 32 obs. of 10 variables:
$ cyl : num 42 42 42 42 42 42 42 42 42 42 ...
$ 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 ...
Compare that with normal data.frame operations
與普通的data.frame操作進行比較
R> data(mtcars)
R> tracemem(mtcars)
[1] "<0x6b3ec30>"
R> mtcars[, "mpg"] <- NULL
tracemem[0x6b3ec30 -> 0x84de0c8]:
tracemem[0x84de0c8 -> 0x84de410]: [<-.data.frame [<-
tracemem[0x84de410 -> 0x84de6b0]: [<-.data.frame [<-
R> tracemem(mtcars)
[1] "<0x84dca30>"
R> mtcars[, "cyl"] <- rep(42, 32)
tracemem[0x84dca30 -> 0x84dcc28]:
tracemem[0x84dcc28 -> 0x84dd018]: [<-.data.frame [<-
tracemem[0x84dd018 -> 0x84dff70]: [<-.data.frame [<-
R> untracemem(mtcars)
R> data(mtcars)
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/07/26/725c82ad766cdc453c57167606f6c57d.html。