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.
You can view and edit a dataframe using with the fix() function: # Open the mtcars dataframe for editing: fix(mtcars) # Edit and close.
transform() function in R Language is used to modify data. It converts the first argument to the data frame. This function is used to transform/modify the data frame in a quick and easy way.
Data Frame is a two-dimensional structured entity consisting of rows and columns. It consists equal length vectors as rows. The data is stored in cells which are accessed by specifying the corresponding [row, col] set of values of the data frame.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With