Strange behavior dropping column from data.table in R

I have been playing with data.tables and noticed some strange behavior and not sure if I am doing something wrong or not.

If I reassign data.table to a different name and then delete the column from the new table, it will also delete it from the first table. For example:

a <- data.table(x=rnorm(10,3),y=rnorm(10,3),z=rnorm(10,3))
> dim(a)
[1] 10  3

b <- a
b[,z:=NULL]
> dim(a)
[1] 10  2
> dim(b)
[1] 10  2

      

However, if I use the data.frame approach it has no effect. For example:

> b$z <- NULL
> dim(a)
[1] 10  3
> dim(b)
[1] 10  2

      

Am I doing something wrong with data.tables or is this just a quirk?

+3
r data.table


source to share


No one has answered this question yet

See similar questions:

155
Understanding when a data table is a reference to (versus a copy of) another data.table

or similar:

1251
How to sort a data frame by multiple columns
773
Dropping columns in a data frame by name
629
data.table vs dplyr: can something do good and other do bad or bad?
181
How to remove column by name in data.table?
fourteen
data.table: why isn't it always possible to pass column names directly?
ten
Why does selecting column (s) from data.table result in copying?
7
Idiom for conditionally selecting columns from a data table.
6
Transpose datasheet with reshape2: dcast
five
Does the speed of a subset of data.table depend on specific key values ​​in a weird way?
five
Find rows with a specified difference between values ​​in a column



All Articles
Loading...
X
Show
Funny
Dev
Pics