Skip to content
Prev 178931 / 398506 Next

How do I sample "cases" within a matrix?

Hi again Silvia (last time... ?),
now : do I understand : you want 
1) to randomly select some intersections between rows and columns
2) randomly select a number of cases for each intersection (being <= the
number of initial cases ?

if yes, here is my solution, using your example :

# select intersections, put them in new matrix mm
mm<-m[sample(seq(1,5,by=1),2),sample(seq(1,5,by=1),3)]

# make a function that samples a number of cases
foo<-function(x) sample(seq(1,x,by=1),1)

# use mapply
mmm<-matrix(mapply(mm,FUN=foo),dim(mm)[1],dim(mm)[2])

On my computer it seems to work... hope this really help, this time !
Regards. Olivier
Silvia Lomascolo wrote: