Skip to content

Instantly share code, notes, and snippets.

@njudd
Last active August 19, 2025 13:04
Show Gist options
  • Select an option

  • Save njudd/849f3bb47a1acd5ebe36f8e06d10d63b to your computer and use it in GitHub Desktop.

Select an option

Save njudd/849f3bb47a1acd5ebe36f8e06d10d63b to your computer and use it in GitHub Desktop.
R data table standardize a set of cols
pacman::p_load(data.table, stringr)
setDT(ct)
col_fence.s <- c("col1", "col2", "col3")
ct[,(str_c(col_fence.s, ".s")) := lapply(.SD, function(x) as.numeric(as.character(scale(x)))), .SDcols=col_fence.s] # making new ones with a .s suffex
# super sketchy do to only as.numeric incase it is a factor!!! updated to as.numeric(as.character
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment