I have a list called cant with some amounts of samples to take from the list community as follows:
cant = {5, 1, 2, 3, 4};
community = {{1, 2, 3, 4, 5, 6}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3, 4, 5, 6}};
I'm trying to find a better way to do this task:
where = {};
Table[AppendTo[where,
Table[Flatten[{RandomSample[community[[i]], 1], i}], {cant[[
i]]}]];, {i, Length[community]}];
The result is:
where
{{{3, 1}, {2, 1}, {3, 1}, {4, 1}, {4, 1}}, {{1, 2}}, {{3, 3}, {3, 3}}, {{1, 4}, {3, 4}, {1, 4}}, {{4, 5}, {1, 5}, {3, 5}, {1, 5}}}
Because with
where[[1]]
{{3, 1}, {2, 1}, {3, 1}, {4, 1}, {4, 1}}
The 5 samples in the first item (3, 2, 3, 4, and 4) have the index of community 1. I read about Reap and Sow, but really I couldn't understand how to implement this. Any idea of how to make this faster because I'm working with large communities and sampling thousands?
I appreciate your help,
RandomSampleshould beRandomChoice. – C. E. Aug 07 '14 at 22:07RandomChoice(though he correctly usesRandomSamplein his code, in a different way). – C. E. Aug 07 '14 at 22:13Table[RandomSample[community[[1]],1],{4}]several times helped me see what's going on. I fixed my answer based on your recommendation. Thanks so much! – seismatica Aug 07 '14 at 22:17