R Tip: Use Slices

April 16, 2018
By

(This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers)

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.framed“, 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).

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)