Generating sub tables in R

Dair

I need to translate some Python code to R. What I need to do is sample random rows from a larger table multiple times so I can use it for later. Here is an illustration:

library(data.table)
library(dplyr)

test_table <- data.table(replicate(10, sample(0:1, 10, rep=TRUE)))
test_table

Gives a 10 x 10 table populated with (on some particular run):

Full table

So for instance one can get a sample:

sample <- sample_n(test_table, 2)
sample

Which might look like:

A particular sample

However, I don't understand the result when taking multiple samples:

kSampleSize <- 2
kNumSamples <- 3

samples <- replicate(kNumSamples, sample_n(test_table, kSampleSize))
samples

may give:

Samples

But it doesn't really look like a "list of sample". I expected samples[1] to give a result similar to sample but instead I get a weird result (varies per run):

1. 1 0

Am I doing something wrong? Am I misinterpreting the output? Is expecting a "list of sample" something to expect in Python but not in R?

Mako212

There is a simplify argument within replicate that determines whether R attempts to simplify the returned object to a less complicated data structure.

simplify defaults to TRUE, and in this case it collapses the returned list of data frames down into a single object of type list. Specifying simplify = FALSE turns off this behavior.

kSampleSize <- 2
kNumSamples <- 3

replicate(kNumSamples, sample_n(test_table, kSampleSize), simplify = FALSE)

Returns a list of three data frames, preserving the original data structure:

[[1]]
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1:  1  0  0  0  1  0  0  1  0   1
2:  1  1  1  0  0  1  0  0  1   1

[[2]]
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1:  1  1  0  1  0  1  0  1  0   0
2:  1  1  1  1  1  0  0  1  0   1

[[3]]
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1:  0  0  1  0  1  1  0  0  1   1
2:  1  1  1  1  0  0  1  0  0   0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related