Hi, Didn't bother to run the code because someone else said it might do what you intended, and also your problem description was complete unto itself.
The issue is that R copies on change. You are thinking like you have a reference, which you do not. That is not very R like in style, but it certainly can be accomplished if you want via change of input class (See new.env()). A typical R style would be to make the modifications to the input argument, return it, and then assign it back to the input object. e.g. test = myFunction(test) If you really have some reason to want to change the data.frame in a function without re-assigning it then check out data.table, which has that as a side effect of how it operates. Thanks, On Sun, Nov 13, 2016 at 2:09 PM, Bernardo Doré <berd...@gmail.com> wrote: > Hello list, > > my first post but I've been using this list as a help source for a while. > Couldn't live without it. > > I am writing a function that takes a dataframe as an argument and in the > end I intend to assign the result of some computation back to the > dataframe. This is what I have so far: > > myFunction <- function(x){ > y <- x[1,1] > z <- strsplit(as.character(y), split = " ") > if(length(z[[1]] > 1)){ > predictedWord <- z[[1]][length(z[[1]])] > z <- z[[1]][-c(length(z[[1]]))] > z <- paste(z, collapse = " ") > } > x[1,1] <- z > } > > And lets say I create my dataframe like this: > test <- data.frame(var1=c("a","b","c"),var2=c("d","e","f")) > > and then call > myFunction(test) > > The problem is when I assign x[1,1] to y in the first operation inside the > function, x becomes a dataframe inside the function scope and loses the > reference to the dataframe "test" passed as argument. In the end when I > assign z to what should be row 1 and column 1 of the "test" dataframe, it > assigns to x inside the function scope and no modification is made on > "test". > > I hope the problem statement is clear. > > Thank you, > > Bernardo Doré > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.