This question about data.table has 2 parts... :
First, the disappearing row.names in data.table. See code below: converting a dataframe to a data.table zaps the row.names. But even after adding the row.names as a field they are zapped during the conversion. What am I doing wrong?
Second, the communicating data.tables. See code below: if I make a new data.table, the old and the new one seems to be communicating. In other words, they are different tables, but updating table 1 also updates table 2. What am I doing wrong?
library(data.table)
library(stringr)
# part 1 - the zapped row.names...
data(mtcars)
dt=mtcars
dt$cars=row.names(dt) # add row.names as field
cars=dt$cars # stores field as vector, as next step will zap it
dt=data.table(mtcars) # zaps field "cars"...
dt=cbind(dt,cars)
# part 2 - the communicating data.tables...
dt1=dt # make a new table
dt1[,cars:=str_replace(cars,"Valiant","Thingy")] # change something in the table
# now *both* tables have changed...
# try with data.frame
df=mtcars
df$cars=row.names(df)
df1=df
df1=transform(df1,cars=str_replace(cars,"Valiant","Thingy")) # works as expected
# now only df1 has changed.