Resampling with replacement permutation analysis My question might be easy for most of you to answer. I am starting to learn statistics and coding in R so my questions are on the basic level.
I have a dataset of (two groups, replicates). My data is split based on the groups so I have 24 samples in group 1 and 20 samples in group 2. My data has replicates. So each set has 4 replicates, hence I have 6 sets in group 1 and 5 sets in group 2. Hence I have assigned indices to them to make it easier during permutation (indices from 1-11). What I want to do now is a routine permutation analysis to obtain the test statistic. I am using non paramteric method with resampling with replacement. 
My doubt\problem in R coding is that I have to pool the data together and then resample the groups. When I try to do this, I have to make sure I maintain the sample size for respective groups (that is after resampling the group lables, my new dataset should still contain 6 sets (24 samples) in group 1 and 5 sets (20 samples) in group 2. I am unable to achieve the latter. 
I tried something like this in my code:
n_resamples=100

permdat=function(dat){
for (i in 6:nc)
{
 for (j in 1:n_resamples)
   {
        inds=unique(dat$ind)
     cc=dat$grp[dat$ind==inds]
    pcc=sample(cc,replace=T) ##for resampling with replacement
    #m<-list()
    pdat=dat

for(i in 1:length(inds))
{
pdat$grp[pdat$ind==inds[i]]=pcc[i]
#browser()
}

This does not produce the desired result. Kindly help with R codes. 
Thanking you all for your help. :)
Regards
Ap
 A: With such small numbers of "sets" within each group, it is possible to generate the full permutation distribution.  The following account focuses on the statistical ideas and techniques, using R to illustrate with simulated data like those described in the question.

The number of samples of size $k$ (such as $k=5$ or $k=6$) with replacement from a set of $n$ items (such as $n=11$) equals $\binom{n+k-1}{k}$.  It is useful to know why that is, because that knowledge helps with the coding.  It is based on a one-to-one correspondence between $k$-subsets of $n+k-1$ things and $k$-multisubsets of $n$ things.  (A "multisubset" can have repeated elements.)  By indexing these things $1, 2, \ldots, n+k-1$, we can represent a subset by an ordered list of $k$ distinct indexes 
$$1 \le i_1 \lt i_2 \lt \cdots \lt i_k \le n+k-1.$$
Now subtract $0$ from the first index, $1$ from the second, etc., producing the sequence 
$$1 \le j_1 = i_1-0 \le j_2 = i_2-1 \le \cdots \le j_k = i_k - (k-1) \le n.$$
Because the $i_s$ can be recovered as $i_s = j_s + s - 1$, this is a one-to-one correspondence.  But the second set of indexes, which range only from $1$ to $n$, allows replicates and therefore enumerates all samples of $n$ things with replacement, QED.
From this we can compute that there are $\binom{11+6-1}{6} = 8008$ ways to simulate the first group and $\binom{11+5-1}{5} = 3003$ ways to simulate the second group.
Depending on the test statistic, there are two ways to proceed.  The most efficient is where the statistic depends separately on the values in the first group and the values in the second.  A simple example would be the difference between the group means.  In such cases we can obtain the full permutation (actually, bootstrap) distributions $F_1$ and $F_2$ of means for groups $1$ and $2$, respectively, by doing just $N=3003+8008$ calculations.  By computing the convolution of $F_1(x)$ and $F_2(-x)$--which can be done in just $O(N)$ operations--we can obtain directly the permutation distribution of the difference of group means.  So, for a computational cost equivalent to a few tens of thousands of random samples, we can obtain the distribution exactly with no sampling error at all.
Less efficient is when the statistic depends intimately on the data in both groups. Then we might have to perform $3003 \times 8008$ calculations.  However, because these can be very fast, the prospect is still not daunting.  The following R code illustrates this method.
To illustrate, let's begin by simulating data like those in the question.  To make it interesting, I will draw medians in each "set" from a standard normal distribution, so that in reality there is no intrinsic difference between the two groups in this regard.  However, I will make the four replicates within each set lognormally distributed and will make the variability greater in the first group.  (This actually makes the expected values in the first group greater than those in the second.)  These conditions of highly skewed distributions (within sets) and heteroscedasticity call into question the validity of conventional parametric analyses, such as ANOVA: that's why a permutation test can be useful.
# Simulation #
n1 <- 6; n2 <- 5; n <- n1+n2 # Group sizes
m <- 4                       # Replicates per set
col1 = rgb(.2, .2, .8, .2)   # Color coding for group 1
col2 = rgb(.8, .2, .2, .2)   # Color coding for group 2

set.seed(17)                                    # Use reproducible numbers
means <- c(rnorm(n))                            # Groups have common means
sds <- c(rep(1,n1), rep(1/4,n2))                # Group 2 has smaller SDs
x <- matrix(exp(rnorm(m*n, means, sds)), nrow=n)# Sets go into rows of `m` values each
par(mfrow=c(1,1))
boxplot(log(t(x)), col=c(rep(col1, n1), rep(col2, n2)), 
        main="Simulated data", xlab="Set", ylab="Log value")

The data x are arranged by rows, one row per set.  The first n1 rows contain data for group 1 and the next n2 rows contain data for group 2.

Note that these boxplots are on a logarithmic scale.  The groups are distinguished by color: blue for group 1, red for group 2.  The heteroscedasticity is evident in the consistently shorter red boxes.  The red boxes tend to be higher than the blue ones, but this is an accident due to chance alone.
To proceed, we need to choose a test statistic.  A simple one that should work well is a robust non-parametric analog of group grand means: obtain the median within each set, then return the median of those medians to characterize the general tendency of a group.  To compare the two groups, we will subtract the group 2 median from the group 1 median.  If that is close to $0$, the groups look similar.
statistic <- function(x, i, ...) {
  median(apply(x[i,], 1, median, ...))
}
group1.stat <- statistic(x, 1:n1)
group2.stat <- statistic(x, -(1:n1))
test.stat <- group1.stat - group2.stat

(Note that this is inefficient: in this particular example, we would be better off computing all $11$ set medians once and for all and applying a permutation test to them.  In order to illustrate the general procedure, I don't do that.)
For these data, the test statistic turns out to be $-1.76$, reflecting the evident lowering of the blue boxes compared to the red ones.  We will subject this to a significance test.  Here is the code:
permute <- function(x, stat, n1) {
  # `x` is an array of rows.
  # `stat` is a statistic that can be computed for any group of the rows of `x`.
  # Returns the full resampling (with replacement) distribution of `stat` for
  # `n1` rows at a time.
  multisubsets <- function(k, n) combn(n+k-1, k) - 0:(k-1)
  apply(multisubsets(n1, dim(x)[1]), 2, function(i) stat(x, i))
}

This could be written more compactly without multisubsets, but it might be useful to see the code for obtaining all $k$-multisubsets of $n$ things, so I set it apart.  As previously explained, it obtains all $k$-subsets of $n+k-1$ things (via combn) and then adjusts the indexes.  The result is an array whose columns contain the indexes of rows of x.  The apply line then iterates over those columns, using each in turn to extract a sample of the rows of x and compute its test statistic (median of medians).
Because apply and the sub-indexing that go on are not inherently fast, this is a fairly slow calculation: the next two lines take four seconds on my machine to simulate the full permutation distributions (having $8008$ and $3003$ elements, respectively).
system.time(group1.sim <- permute(x, statistic, n1))
system.time(group2.sim <- permute(x, statistic, n2))

Before we proceed, let's look at these results separately.  I use histograms to show the two distributions.  Remember, these are exact: they are computed without error.
par(mfrow=c(1,2))
hist(group1.sim, probability=TRUE, breaks=25, col=col1)
abline(v = group1.stat, lwd=2)
hist(group2.sim, probability=TRUE, breaks=25, col=col2)
abline(v = group2.stat, lwd=2)


The vertical black lines mark the actual (observed) statistics: that of group 1 is fairly low and that of group 2 is fairly high.  We proceed to compute the exact distribution of the bootstrap statistic--the difference between the group medians--using the brute force approach rather than the efficient $O(N)$ method.  This is very efficiently carried out using a generalized outer product:
par(mfrow=c(1,1))
system.time(simulation.all <- outer(group1.sim, group2.sim, "-"))
hist(simulation.all, probability=TRUE)
abline(v = test.stat, lwd=2, col="Red")

This goes quickly, taking only a quarter second.

The vertical red line shows the observed test statistic of $-1.76$ previously noted.  It looks a little low.  Let's compute its p-value by averaging the upper and lower p-values (this accounts for ties in the data):
p = sum(simulation.all <= test.stat) / length(simulation.all)
q = sum(simulation.all >= test.stat) / length(simulation.all)
(p + 1 - q)/2

The value is $0.0452$: mild evidence of a "significant" difference between these groups.  (It should be doubled, though, for a two-sided alternative hypothesis.)  Because we simulated these data, we know there is no underlying difference in medians.
