Reputation: 13
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!
Upvotes: 0
Views: 1471
Reputation: 79208
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
Upvotes: 0
Reputation: 11
As mentioned by others in the comments:
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")
Upvotes: 1