0

I'm trying to add a column to a dataframe within a function and when I run it I don't get any errors but the column is not in the dataframe in the global environment. This is my code:

scale.it<-function(df,var,newvar){
  varn<-as.numeric(df[[var]])
  last<-max(varn)
  #df[[newvar]]<-varn/last
  return(df[[newvar]]<-varn/last)
}
scale.it(go.cubs.go,"PAge","IPAge")

So essentially, I want my current dataframe go.cubs.go to have all of the current columns plus IPAge.

I know there is a scale function that already exists but I need to adapt this code later so I need to be able to add columns to dataframes in functions.

Any help is greatly appreciated!

Elizabeth
  • 13
  • 1
  • 4

2 Answers2

1

As mentioned by others in the comments:

  1. Variables declared in a function are local to that function (see: Global and local variables in R)
  2. You need to assign the output of the function to a variable

Example:

# Function
scale.it<-function(df,var,newvar){
  varn<-as.numeric(df[[var]])
  last<-max(varn)
  df[[newvar]]<-varn/last
  return(df) # Return dataframe
}

# Create data frame
go.cubs.go <- data.frame('a' = sample(1:10,5, replace = TRUE),
                         'b'= sample(1:10,5, replace = TRUE),
                         'PAge'= sample(1:10,5, replace = TRUE))

# Replace original data frame (or create new one)
go.cubs.go <- scale.it(go.cubs.go, "PAge", "IPAge")
Jonathan
  • 11
  • 2
0

Calling by reference/pointers is not really a thing in R though you can use the R.oo packages. One way to do it in Base R is to evaluate within the parent frame. In that case, the change will happen to the object 'pointed' to. This is mostly the workings of data.table with the function :=. Here is a simple example:

scale.it<-function(df,var,newvar){
  varn<-as.numeric(df[[var]])
  last<-max(varn)
  eval.parent(substitute(df[[newvar]]<-varn/last))
}

m = head(iris)
scale.it(m,"Sepal.Width","sss")
m
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species       sss
1          5.1         3.5          1.4         0.2  setosa 0.8974359
2          4.9         3.0          1.4         0.2  setosa 0.7692308
3          4.7         3.2          1.3         0.2  setosa 0.8205128
4          4.6         3.1          1.5         0.2  setosa 0.7948718
5          5.0         3.6          1.4         0.2  setosa 0.9230769
6          5.4         3.9          1.7         0.4  setosa 1.0000000

You will notice the addition of the column sss in the original m dataframe

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 1
    I just joined the site like 4 hours ago and don't have enough 'reputation' to upvote. I'm sorry but very grateful for the help. FYI I looked for the answer to this for hours and everyone seemed to answer the same as the user below (Jonathon) which says to call it outside the function but you're the first to show me the how to do it INSIDE the function, so thank you! – Elizabeth Jul 23 '18 at 02:34