I need to apply transformations to all numeric variables of a large dataframe. The dataframe has variables of other types as well. My initial idea was to iterate over all the columns, check if they are numerical and then divide them by 1000.
I've got stuck in my code for a function, would appreciate some pointers here:
transformDivideThousand <- function(data_frame){
for(i in ncol(data_frame)){
if (is.numeric(data_frame[i])) {
data_frame[i]/1000
}
}
return(data_frame)
}
The execution of the function:
test <- transformDivideThousand(mypatients)
- test is a dataframe, but the transformations are not happening. Where did I err?
- As an extra, I would also like transformDivideThousand to have an optional argument where I could pass a list with the names for the variables to use, if empty, than iterate over all of them.