0

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    you have some syntax problems here. You have an `[` matched with `)` and there is no closing `}` in the for loop. Is this exactly what you are running? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 30 '20 at 00:51

1 Answers1

0

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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Many thanks for your help. I should use for loop due to the instructions. But if I run the code you provided, my df1 is still empty (with NA's)? :/ – Nenad Knezevic Nov 30 '20 at 11:51
  • When I test it with default `mtcars` dataset it seems to work for me. Can you update your post with `dput(df)` so that I have the data you are working with? Maybe use `dput(head(df))` to provide just first 6 rows. – Ronak Shah Nov 30 '20 at 11:57
  • V1 = c(0.0252792633002786, 0.0137958149491066, 0.0339123877484187, 0.0391935449319028, 0.0216890856734926, -0.0670382728164868 ),V2 = c(0.0394238262879256, 0.0481760517233848, 0.0744350793732872, 0.0617327027592713, -0.046803185985514, -0.00492247301275759), V3 = c(0.0746731701199468, 0.0892577869031498, 0.0977385938604203, -0.00944402382649064, 0.0166598487218359, 0.036153091265947 ), Here I have only 3 columns and 6 rows but it should be enough to get correct solution. Hope it helps – Nenad Knezevic Nov 30 '20 at 12:18
  • @NenadKnezevic It still works for me for the data you shared in the comment. See updated answer. – Ronak Shah Nov 30 '20 at 12:26