fbpx
R Tip: Use Slices R Tip: Use Slices
R tip: use slices. R has a very powerful array slicing ability that allows for some very slick data processing. Suppose we have a data.frame “d“, and... R Tip: Use Slices

R tip: use slices.

SliceOMatic

R has a very powerful array slicing ability that allows for some very slick data processing.

Suppose we have a data.frame “d“, and for every row where d$n_observations < 5 we wish to “NA-out” some other columns (mark them as not yet reliably available). Using slicing techniques this can be done quite quickly as follows.

library("wrapr")

d[d$n_observations < 5, 
  qc(mean_cost, mean_revenue, mean_duration)] <- NA

(For “qc()” please see R Tip: Use qc() For Fast Legible Quoting.)

The above notation is very convenient, compact, and powerful. We are adding this as operator to our rquery query generator as assign_slice()(and a related method for directly dealing with NA/NULL).

 


 

Original Source

John Mount

John Mount

My specialty is analysis and design of algorithms, with an emphasis on efficient implementation. I work to find applications of state of the art methods in optimization, statistics and machine learning in various application areas. Currently co-authoring "Practical Data Science with R"

1