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?
this could be useful
> Vector <- paste("x", 1:6, sep="")
> lapply(1:length(Vector), function(i) Vector[-i])
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.
You can subset the element from the list by indicating they order in the list.
To select the items 1 and 3 from a list
my.list[c(1,3))]
Try this:
# create a dummy list of data frames
d1 <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4))
d3 <- data.frame(y1=c(3,2,1),y2=c(9,8,7))
my.list <- list(d1, d2, d3)
# get the items 1 and 3
my.list[c(1,3)]
# get the all element except of the first one
my.list[c(-1)]
Just in case you want to use the data for a leave-one-out jackknife, here a quick pointer to the bootstrap
package and a short example
library(bootstrap)
vec_list=list()
jackknife(rnorm(10,0,1),function(x) {vec_list[[length(vec_list)+1]]<<-x;mean(x)})
vec_list[1:10]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.