[R] random sample from list

Rui Barradas rui1174 at sapo.pt
Thu Apr 5 18:42:16 CEST 2012


Hello,

>
> #Here is how I have tried to sample but it is not sampling from the right
> part of the list
> 
> bg<- z_nonna[sample(1:length(z_nonna), 5000, replace=FALSE)]
> 

You are sampling from the length of z_nonna, with no guarantee that they are
indices to unique list elements.

Try this.

# First, create some fake data.
n <- 1000
z <- list()
set.seed(1234)
for(i in 1:n) z[[i]] <- sample(letters, 2)

# Now sample some unique elements from it.
iz <- which(!duplicated(z))
iz <- sample(iz, 100) # sample from the non-duplicate indices.
z[iz]

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/random-sample-from-list-tp4533936p4535397.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list