I have a dataframe (called train) that contains a YOB (year of birth) column. I'd like to compute the Age in a separate column, like so:
train$Age = 2016 - train$YOB
This works fine.
The problem is that I would also like to do this operation (along with other preprocessing operations) to a number of other dataframes. So, I was thinking to extract the common parts in a function and pass the dataframes to be processed as parameters to the function:
preprocess = function(d) {
d$Age = 2016 - d$YOB
# other transformations...
}
After defining the function above, I expected that calling preprocess(train) would perform the aforementioned transformations on my dataframe. But it doesn't. For example, train$Age is NULL after the call.
Why doesn't the preprocess function transform the dataframe as expected? Is there a way to fix this?
preprocess = function(d) d$Age <<- 2016 - d$YOBorpreprocess = function(d) 2016 - d$YOB; d$age <- preprocess(d). Object that made in function is not outside of function except for<<-.