Monday, 12 August 2013

update a data frame and environment in R

update a data frame and environment in R

I am not quite sure why the data frame object does not update
d <- data.frame(titi=c(0))
(function(dataset) {
dataset[["toto"]] <- 1;
print(names(dataset)) #has "toto" and "titi"
})(d)
print(names(d)) # no has "toto", only "titi"
What is going on here ?
PS : I have a workaround as in my code I also capture the variable and
update the captured with <<-, but I'd like to know the mechanism.
PPS : I am aware of the danger of mutation in general etc.. I just dont
understand the mechanism at play here.

No comments:

Post a Comment