Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Mutate a DataFrame?

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!

like image 413
Ecognium Avatar asked Dec 24 '14 21:12

Ecognium


People also ask

How to mutate a variable in a data frame?

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

How do I mutate a data frame in S3?

# 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.

What does the mutate () function do?

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?

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...


1 Answers

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.

like image 118
Mr Alpha Avatar answered Sep 29 '22 06:09

Mr Alpha