Search code examples
rfor-loopsample

sample rows from dataframe using for loop in R


There is a df given with nrow=600 and ncol=18

Now I need to sample 10000 of each of this columns with replacement.

According to the specifications first I need to create an empty matrix:

df1 <- as.data.frame(matrix(NA,nrow = 10000,ncol=18))

now I want to use for loop to do all the samples(for each column) at once:

for (i in 1:18){

df1[1:10000, i) <- sample(df[,i], 10 000, replace=true)

when I run this code, my df1 is still empty.

Can anyone help?

Many thanks in advance


Solution

  • There are syntax issues in your code. Try the following :

    df1 <- as.data.frame(matrix(NA,nrow = 10000,ncol=18))
    
    for (i in 1:18) {
      df1[, i] <- sample(df[, i], 10000, replace = TRUE)
    }
    

    Without an explicit for loop you can also use sapply/lapply :

    #With `sapply`
    df1 <- as.data.frame(sapply(df, sample, 1000, replace = TRUE))
    
    #Using `lapply`
    
    df1 <- do.call(cbind.data.frame, lapply(df, sample, 1000, replace = TRUE))
    

    It works for the data shared in comments.

    df <- data.frame(V1, V2, V3)
    df1 <- as.data.frame(matrix(NA,nrow = 10000,ncol=3))
    
    for (i in 1:3) {
      df1[, i] <- sample(df[, i], 10000, replace = TRUE)
    }
    
    dim(df1)
    #[1] 10000     3
    
    head(df1)
    #           V1           V2           V3
    #1  0.02527926  0.039423826  0.097738594
    #2  0.03391239  0.039423826  0.036153091
    #3  0.03919354 -0.004922473  0.097738594
    #4 -0.06703827  0.039423826  0.097738594
    #5  0.02168909  0.048176052  0.036153091
    #6  0.02527926  0.074435079 -0.009444024