Home > Software engineering >  How to get an R function to have a global effect on a dataframe?
How to get an R function to have a global effect on a dataframe?

Time:03-26

I have been trying to create a function which will permanently change a value in specific cells of my data frame. I insert the data frame name, row index I wish to change, and the new name as a string. However, the function seems to change the value name within the local environment but not global.

The function is as follows:

#change name function
name_change <- function(df, row, name) {
  df[row, 1] = name
  return(df[row, 1])
}

E.g. if data frame was:

Name Column B
Mark 2
Beth 4

The function name_change(df, 2, 'Jess') would change Beth to Jess.

When inserted as raw code it does permanently change the value. But then does not work when used as a function.

df[2, 1] = 'Jess'

Thanks in advance for your time

CodePudding user response:

You are returning the value of the cell, not the mutated df. R passes by arguments by value so you can imagine the function modifying a copy of df passed in. The solution is to return the mutated df and reassign it.

Can you pass-by-reference in R?

CodePudding user response:

An alternative to the solutions already provided is to use the superassignment operator <<-. The ordinary assignment <- (or '=' you used) operate in your function's environment only. The superassignment reaches beyond your function's closure and can thus modify the dataframe residing in the global environment. Note, though, this is a quick'n'dirty fix only.

That said, the code would read like this:

#change name function
dirty_name_change <- function(df, row, name) {
  df[row, 1] <<- name ## note the double arrow
}

CodePudding user response:

If you change your function like this:

name_change <- function(df, row, name) {
  df[row, 1] = name
  return(df)
}

and then assign the result of the function back to the original df, you will get the change you are looking for:

df = name_change(df,2,'Jess')
  • Related