:= making changes on the original dataframe, not on the copied one.

Hi everyone!

I'm trying to do some transformation on the variable TP_STATUS_REDACAO from the dataset called DFtest2. In order to preserve the original data, I have made a copy called DFtest.means.

Working on the Rnotebook, I've ran the following commands:

DFtest.means <- DFtest2
DFtest.means[, TP_STATUS_REDACAO := NA]

I've expected that only the column DFtest.means$TP_STATUS_REDACAO would have been changed, however the column DFtest2$TP_STATUS_REDACAO has also became NA.

I have no clue about what's going on and I couldn't find an answer on the internet.

Could anyone shed some light on it for me? :slight_smile:

You need to copy the object when using data.table:

DFtest.means <- copy(DFtest2)
1 Like

I'm a bit confused by the above code because I'm not sure what := means. Do you mean '!=' or maybe '<-'. If the latter, this could cause 'DFtest2$TP_STATUS_REACA0' to become NA, especially if 'DFtest2' was attached.

Also, I always use 'is.na()' when testing for NAs, as 'X != NA' returns NA.

The := syntax is from the data.table library:

Excerpting from their reference above:

# add/update/delete by reference (see ?assign)
print(DT[, z:=42L])                         # add new column by reference
print(DT[, z:=NULL])                        # remove column by reference
print(DT["a", v:=42L, on="x"])              # subassign to column
print(DT["b", v2:=84L, on="x"])             # subassign to new column (NA padded)

DT[, m:=mean(v), by=x][]                    # add new column by reference by group
1 Like

I really appreciate!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.