I'm trying to write a function in R that drops columns from a data frame and returns the new data with a name specified as an argument of the function:
drop <- function(my.data,col,new.data) { new.data <<- my.data[,-col] return(new.data) } So in the above example, I want a new data frame to exist after the function is called that is named whatever the user inputs as the third argument.
When I call the function the correct data frame is returned, but then if I then try to use the new data frame in the global environment I get object not found. I thought by using the <<- operator I was defining new.data globally.
Can someone help me understand what's going on and if there is a way to accomplish this?
I found this and this that seemed related, but neither quite answered my question.
assign(new.data, mydata[,-col], envir = .GlobalEnv)although I would recommend against this whole idea<<-from within a function is terrible practice.