I have some data tables with the same structure and I want to make a few data transformations on them (create new variables, assign missing values etc)
This is what I've tried, without success. This codes runs ok but it does not make changes to the data tables. Any ideas?
For a reproducible example, run this snippet of code first
data("mtcars") # load data
setDT(mtcars) # convert to data table
mtcars[gear==5, gear :=NA] # create NA values for the purpose of my application
mtcars2 <- mtcars # create second DT
My code
# Create function
computeWidth <- function(dataset){
dataset$gear[is.na(dataset$gear)] <- 0 # Convert NA to 0
dataset[ ,width := hp + gear] # create new variable
}
# Apply function
lapply(list(mtcars, mtcars2), computeWidth)
As you can see, the function works fin, but it didn't modify the data tables. ny thoughts on this ?