I am working with a data frame that has mixed data types (numeric and character) and also has a character key as the primary identifier. I\'d like to scale and center the n
This code below does not need any external library:
# Scale all numeric columns in a data frame.
# df is your data frame
performScaling <- TRUE # Turn it on/off for experimentation.
if (performScaling) {
# Loop over each column.
for (colName in names(df)) {
# Check if the column contains numeric data.
if(class(df[,colName]) == 'integer' | class(df[,colName]) == 'numeric') {
# Scale this column (scale() function applies z-scaling).
df[,colName] <- scale(df[,colName])
}
}
}