nafill new type: approx

Open
#4,066 11 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start with the existing nafill entry point and its current types, then review how the examples use approx() and zoo::na.approx() for comparison. Define the expected behavior for the proposed approx type, including the missing-value runs shown in the issue, and validate the result against the supplied examples and performance benchmark.

Written by the indexing model from the issue text.

Description

feature request top request

This is a FR. Also I apologize ahead of time if this is not an appropriate post for here.

From this StackOverflow post:

library(data.table)
dt <- data.table(dist = c(31091.33, NA, 31100.00, 31103.27, NA, NA, NA, NA, 31124.98))
dt[, time := .I]

##       dist time
##1: 31091.33    1
##2:       NA    2
##3: 31100.00    3
##4: 31103.27    4
##5:       NA    5
##6:       NA    6
##7:       NA    7
##8:       NA    8
##9: 31124.98    9

OP wants to do a linear interpolation to fill in the NA values. The most simple solution is to use base approx() or zoo:na.approx() per user dww:

copy(dt)[, dist := zoo::na.approx(dist)][]
copy(dt)[, dist := approx(.I, dist, .I)$y][]

#       dist time
#1: 31091.33    1
#2: 31095.67    2
#3: 31100.00    3
#4: 31103.27    4
#5: 31107.61    5
#6: 31111.95    6
#7: 31116.30    7
#8: 31120.64    8
#9: 31124.98    9

The current nafill types are: c("const", "locf", "nocb") - an "approx" type would be helpful. I wrote an Rcpp implementation to show that there is room for improvement as it's ~10 times faster than the base method which is already a C based method:

#data per @chinsoon12
nr <- 1e7
nNA <- nr/2
DT <- data.table(time=1:nr, dist=replace(rnorm(nr), sample(1:nr, nNA), NA_real_))

bench::mark(
  rcpp_appr = rcpp_approx(DT[['dist']]),
  base_approx = DT[, approx(.I, dist, .I)$y],
  zoo_approx = zoo::na.approx(DT[['dist']]),
  time_unit = 's'
)

# A tibble: 3 x 13 (in seconds)
  expression    min median `itr/sec` mem_alloc
  <bch:expr>  <dbl>  <dbl>     <dbl> <bch:byt>
1 rcpp_appr   0.126  0.132     7.08       76MB
2 base_approx 1.34   1.34      0.745     617MB
3 zoo_approx  2.22   2.22      0.450    1630MB ##manually changed from GB to MB

# this is the rcpp code
Rcpp::sourceCpp(code = '
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector rcpp_approx(NumericVector y) {
  double start = 0, slope = 0;
  int count = 0;

  NumericVector y1 = clone(y); //added to not update-by-reference

  for(int i = 0; i < y1.size(); ++i){
    if (NumericVector::is_na(y1[i])){
      count++;
    } else {
      if (count != 0) {
        start = y1[i - (count+1)];
        slope = (y1[i] - start) / (count + 1);
        for (int j = 0; j < count; j++){
          y1[i-(count-j)] = start + slope * (j + 1);
        }
      count = 0;
      } 
    }
  }
return(y1);
}
')

Thank you for your consideration.

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.