I often get in the situation where I have an index vector and a data object and I need to :
slice data, if it's a data.frame
indexing data, if it's a vector or list
I was wondering, is there already a function doing that in base R or tidyverse ? Because it bothers me to reimplement it in each of my projetcs, and I'm quite sure I'm not the only one who needs it.
If this function is not already available, I think it would be nice to extend dplyr::slice to handle vectors and lists (by indexing them). What do you think about it ?
dplyr::slice arguable has a place because it can potentially improve readability/transparency over the square bracket syntax for accessing data.frame's. for example : mydat[x,y] ; is x or y the row or column etc ? slice(mydat,5) is in that sense 'clearer'
but the square bracket syntax to access a vector is trivially myvec[x]; would slice(myvec,x) therefore be an improvement ? its arguable a step back for readabiility; and for people used to seeing data.frames get sliced by slice, simply seeing an object being sliced might mislead them into expecting it to have been a data.frame whereas its a vector.
The order is [row,col], and slice is preferred only within a tverse based workflow. A user confused by the difference between a data frame and a vector is likely to be more confused with slice, which takes as its argument
## Arguments
.data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See *Methods*, below, for more details.