MYaseen208
MYaseen208

Reputation: 23898

Random samples from each column of a data.frame

I want to draw random sample from each row of a data.frame independently from other rows. Here is an example. This code selects the same column for each row but I require independent selection of columns for each row.

library(plyr)
set.seed(12345)
df1 <- mdply(data.frame(mean=c(10, 15)), rnorm, n = 5, sd = 1)
df1
  mean       V1       V2        V3        V4       V5
1   10 10.58553 10.70947  9.890697  9.546503 10.60589
2   15 13.18204 15.63010 14.723816 14.715840 14.08068
> df1[ , -1]
        V1       V2        V3        V4       V5
1 10.58553 10.70947  9.890697  9.546503 10.60589
2 13.18204 15.63010 14.723816 14.715840 14.08068
> sample(df1[, -1], replace = TRUE)
         V3       V2       V5        V4      V4.1
1  9.890697 10.70947 10.60589  9.546503  9.546503
2 14.723816 15.63010 14.08068 14.715840 14.715840
> t(apply(df1[, -1], 1, sample))
         [,1]      [,2]     [,3]     [,4]      [,5]
[1,] 10.70947  9.890697 10.60589 10.58553  9.546503
[2,] 14.71584 13.182044 14.08068 15.63010 14.723816

Edited

df1[ , -1]
            V1       V2        V3        V4       V5
    1 10.58553 10.70947  9.890697  9.546503 10.60589
    2 13.18204 15.63010 14.723816 14.715840 14.08068

sample(df1[, -1], replace = TRUE)
             V3       V2       V5        V4      V4.1
    1  9.890697 10.70947 10.60589  9.546503  9.546503
    2 14.723816 15.63010 14.08068 14.715840 14.715840

sample(df1[, -1], replace = TRUE) selects the columns V3, V2, V5, V4, and V4 for both rows. But I require that it could select columns V3, V2, V5, V4, and V4 for first row and/or any combinations of five columns for second row.

Upvotes: 3

Views: 1954

Answers (2)

BenBarnes
BenBarnes

Reputation: 19454

You could sample the column indices all at once and then use matrix subsetting to avoid having to use apply:

## Determine how many indices are required (nrow x (ncol - 1))
nsamp <- prod(dim(df1[, -1]))

## Sample from the number of desired columns, here 5 = ncol(df1[, -1])
mySamp <- sample.int(5, nsamp, replace = TRUE)

## Create a matrix of row and column indices
## Have to add 1 to mySamp to ignore first column of df1
myIdx <- cbind(rep(seq_len(nrow(df1)), ncol(df1) - 1), mySamp + 1)

## Return the corresponding values
matrix(df1[myIdx], nrow = nrow(df1))

#           [,1]     [,2]      [,3]      [,4]     [,5]
# [1,]  9.890697 10.60589  9.546503  9.546503 10.70947
# [2,] 15.630099 14.71584 15.630099 14.723816 14.72382

Upvotes: 2

akrun
akrun

Reputation: 886938

You could use apply with replace=TRUE for the sample

 t(apply(df1[,-1], 1, sample, replace=TRUE))

Upvotes: 3

Related Questions