Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

An alternative to `accumulate` that allows cumulative filtering?

$
0
0

I'm looking for an approach to do something like this

# this doesnt work
# accumulate(1:8, ~filter(mtcars, carb >= .x))

So that I can examine some summary statistics at different cutoff values. I could simply do

# this works but redundant filtering is done
map2(list(mtcars), 1:8, ~filter(.x, carb >= .y))

But since my data is rather large, it doesn't make sense to filter out values that were already filtered out in the step just before. In essence, this just duplicates the original dataframe a number of times and then filters each one separately. I was looking at accumulate from the purrr package, but that function doesn't seem fit to this problem (I'm hoping that I'm wrong on this). The base-R solution could be

# something like this works, but is ugly
output <- vector("list", length(1:8) + 1)
output[[1]] <- mtcars
for (i in 1:8) {
  output[[i + 1]] <- filter(output[[i]], carb >= i)
}
output[[1]] <- NULL

but that's not particularly elegant. How can I accomplish this better?

# the above code assumes
library(tidyverse)
mtcars <- as_tibble(mtcars)

This is an example of something the output could be used for:


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>