'Colnames unexpectedly updating variable in R

I'm trying to get a list of column names that have been added after the initial csv load. If I am not updating the variable after column names are added, then how are they being added to the variable?

I would expect that only Name and Age would get printed from my_cols but it is printing IsJon as well

library(data.table)

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.table(Name, Age)

my_cols <- colnames(df)

print(my_cols)

df[,isJon:=ifelse(Name=="John", 1, 0)]

print(my_cols)
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source