I am trying to remove some columns from my data frame and would prefer not to return the modified data frame and reassign it to the old. Instead, I would like the function to just modify the data frame. This is what I tried but it does not seem to be doing what I except. I was under the impression arguments as passed as reference and not by value?
function remove_cols! (df::DataFrame, cols)
df = df[setdiff(names(df), cols)];
end
df = DataFrame(x = [1:10], y = [11:20]);
remove_cols!(df, [:y]); # this does not modify the original data frame
Of course the below works but I would prefer if remove_cols!
just changed the df in place
df = remove_cols!(df, [:y]);
How can I change the df in place inside my function?
Thanks!
The basic synax for mutate() is as follows: data <- mutate(new_variable = existing_variable/3) data:the new data frame to assign the new variables to new_variable:the name of the new variable existing_variable:the existing variable in the data frame that you wish to perform some operation on to create the new variable
# S3 method for data.frame mutate( .data , ... , .keep = c ("all", "used", "unused", "none") , .before = NULL , .after = NULL ) transmute(.data, ...) A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.
The mutate () function is a function for creating new variables. Essentially, that’s all it does. Like all of the dplyr functions, it is designed to do one thing.
How to Use Mutate to Create New Variables in R 1 Adding New Variables in R 2 mutate () The mutate () function adds new variables to a data frame while preserving any existing variables. 3 transmute () The transmute () function adds new variables to a data frame and drops existing variables. More items...
As I understand Julia it uses what is called pass by sharing, meaning that the reference is passed by value. So when you pass the DataFrame to the function a new reference to the DataFrame is created which is local to the function. When you reassign the local df
variable with its own reference to the DataFrame it has no effect on the separate global variable and its separate reference to the DataFrame.
There is a function in DataFrames.jl for deleting columns in DataFrames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With