My R cheatsheet


DescriptionR command(Stata command)
Produce freq. table by groupdt[, .(rowcount = .N),
tab group
Compute max of var per groupdt[, group_max := max(var), by=groupby group: egen group_max = max(var)
Create row id within groupdt[, r_id:=seq_len(.N), by=group]by group: g r_id = _n

lubridate (w/ data.table)

DescriptionR command
Create Date from stringdt[, date_var := as.Date(as.character(num_date),
Convert Date to year-quarterdt[, yq := quarter(date_var, with_year=TRUE)
Create Date from components (e.g., year-quarter from year and quarter)yq = quarter(parse_date_time(paste0(year, “.”, quarter),
orders=”Yq”), with_year=TRUE)

Leave a Reply

Your email address will not be published. Required fields are marked *