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

pick elements cumulatively from a vector in R

$
0
0

I'm using the following to cumulatively (i.e., first pick first 2 elements and each time add 1 more; see output below) pick elements from vector mods below. I wonder if there is a faster or simpler approach to doing this in BASE R?

n <- names(mtcars) # BASE R dataset

lapply(seq_along(n)[-1], function(i)n[seq_len(i)]) # any faster or simpler approach here?

[[1]]
[1] "mpg""cyl"

[[2]]
[1] "mpg""cyl""disp"

[[3]]
[1] "mpg""cyl""disp""hp"  

[[4]]
[1] "mpg""cyl""disp""hp""drat"

Viewing all articles
Browse latest Browse all 201839

Trending Articles



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