2

As you can read in the title I'm wondering why data.table::setnames acts on multiple dataframes while I'm specifying just one. See attached code below:

library(data.table)

# create dataframe
df <- data.frame(col_1 = c(1:9), col_2 = letters[1:9])

# craeate dataframe copy
df_copy <- df

# check names
names(df)
names(df_copy)

# change dataframe "df" names
data.table::setnames(x = df, old = c("col_1", "col_2"), new = c("col_a", "col_b"))

# check names
names(df)
names(df_copy)

As you can see, I'm specifying just dataframe df for argument x in function setnames but after running code also the names of dataframe df_copy result in changes. It's seems assign operator <- is acting as a dynamic join instead of assigning a copy of df to df_copy. Anyone can help me understanding why?

emaoca
  • 47
  • 3
  • 5
    Use `df_copy <- copy(df)` – akrun Jun 08 '22 at 16:23
  • [This](https://stackoverflow.com/questions/62740267/is-r-data-table-documented-to-pass-by-reference-as-argument) may help explain what's going on. – Limey Jun 08 '22 at 16:40
  • 1
    Ok, thanks. But could you explain briefly the behavior of data.frame function ```setnames```? Why is changing names from objects other than ones I have specified? – emaoca Jun 08 '22 at 16:45
  • see [set attributes by reference](https://rdatatable.gitlab.io/data.table/reference/setattr.html) : "In data.table, all set* functions change their input by reference; that is, no copy is made at all" – Waldi Jun 08 '22 at 19:00

0 Answers0