Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 21, 2021 17:00
Show Gist options
  • Save simonthompson99/141a631f1f32e54a701d46e27d8f31ae to your computer and use it in GitHub Desktop.
Save simonthompson99/141a631f1f32e54a701d46e27d8f31ae to your computer and use it in GitHub Desktop.
[R one-liners] #r #oneliner
#-- capture folder path, filename, and extension from filepath
<filename> <- sub("(.*\\/)([^.]+)(\\.[[:alnum:]]+$)", "\\2", <path>)
#-- add leading zeros to number
sprintf("%02d", <num>) #2 here pads with zeros so that all characters hace 2 characters (e.g.9 > 09), 4 would make 39 > 0039
#-- timestamp for file etc.
format(now(), "%Y%m%d-%H%M%S")
#-- get unique string for row in dataframe
require(digest)
df$hash <- apply(df, 1, digest)
#-- read certain element from JSON list
sapply(list_object, "[[", "item_name")
#-- read tab-delimited data from Mac clipboard
d <- read.table(pipe("pbpaste"), sep="\t", header=TRUE)
#-- write dataframe to db table, overwrite or append as argument
dbWriteTable(con, DBI::Id(schema = "schema", table = "table",
d, row.names = F, overwrite = T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment