Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subset a list in R by selecting all elements in a list except for one value?

So basically I have a list called "parameters" with values (x1, x2, ... , xj). I want to, through a for loop, subset this list, but each time leave out one element. So for example I want the first subset (through the first iteration of the for loop) to be (x2, x3, ..., xj), and the next to be (x1, x3, ..., xj) and so on, until the last subset which would be (x1, x2, ... , xj-1). How do I do this?

like image 565
user2560984 Avatar asked Jul 08 '13 13:07

user2560984


2 Answers

this could be useful

> Vector <- paste("x", 1:6, sep="")
> lapply(1:length(Vector), function(i) Vector[-i])
like image 113
Jilber Urbina Avatar answered Oct 03 '22 20:10

Jilber Urbina


I'm assuming by "list" you mean vector. If so:

parameters <- rnorm(100)
y <- matrix(nrow=length(parameters)-1,ncol=length(parameters))
for(i in 1:length(parameters))
    y[,i] <- parameters[-i]

If by "list", you actually mean a list, the code is basically the same, but just do parameters <- unlist(parameters) first.

like image 42
Thomas Avatar answered Oct 03 '22 19:10

Thomas