I have a vector x
containing 5 elements.
x <- (1,2,3,4,5)
I would want to delete one element at each iteration and retain other elements in the vector.(as shown below)
x <- (2,3,4,5) #vector after first iteration
x <- (1,3,4,5) #vector after second iteration
x <- (1,2,4,5) #vector after third iteration
x <- (1,2,3,5) #vector after fourth iteration
and also, is it possible to store these new vectors in a list? is there a way to extend this to multiple vectors?
Here you have a way to get what you want. You only need to change the parameter
n
to make it more generalYou could use
combn
:To get the data as a
list
:To use this on multiple vectors or a matrix, first transform it into a
data.frame
, to make it easier forlapply
to go over the length (columns) of thedata.frame
. Then you can uselapply
withcombn
like so:We can do