Short examples for moving average and cumulative average usig R. Moving average Some alternatives to do moving average, sometimes called running mean or rolling mean. You can use base R, data.table or zoo package. Use function frollmean from data.table package or rollmean from zoo package.
Read MoreSubset DT
Subsetting data.table can be done using base R subset with S3 Class for data.table object. Alternatively is to use the recode approach as mentioned in StackOverflow. lookup <- list(v1 = 1:3, v2 = letters[5:7]) DT[lookup, on = names(lookup), nomatch = NULL] This will return all columns in DT that match lookup list.
Read Morenames attribute
Sometime an object has attribute names for identification of the value with names instead of index. There are time you need to extract the names of a vector or object. The easiest way is to do: library(data.table) dt <- cars[1,] setDT(dt) str(dt) dt[, dist := as.
Read Moredata.table tips
Updated: 2021-09-17 Some tips for data.table that I came across while googling which might be useful. Print To print more rows that default can be done with either: options(datatable.print.topn = 70) print(DT, topn = 70) Using options will implement the changes globally.
Read MoreCase when
It’s often easier to use ifelse to implement conditioning. The fast implementation in data.table of ifelse is fifelse. Other approach to do multiple conditioning is to use data.table::fcase or dplyr::case_when. fcase fcase can be used directly x <- 1:6 data.table::fcase(x < 3, 1, x >= 4, 2) To implement in a data.
Read More