Reputation: 53
I have a sample like this:
V echo
A 2
D 4
N 12
P 6
D 4
P 2
N 3
A 1
I want to get three data frame samples (n=20,n=50, n=100), based on this sample with replacement and then I want to calculate the mean and SD for each sample.
Upvotes: 1
Views: 40
Reputation: 101054
Do you mean something like below?
n <- c(20, 50, 100)
lapply(
n,
function(k) {
df[sample(nrow(df), k, replace = TRUE), ]
}
)
Or, we can use split
(probably faster than my previous option)
split(
df[sample(nrow(df), sum(n), replace = TRUE), ],
rep(seq_along(n), n)
)
Upvotes: 2
Reputation: 886938
Loop over those numbers
sapply(c(20, 50, 100), function(x) {
x1 <- sample(df1$echo, x, replace = TRUE)
c(Mean = mean(x1), SD = sd(x1))})
If we want to return 3 dataset samples
lst1 <- lapply(c(20, 50, 100), function(x) {
i1 <- sample(seq_len(nrow(df1)), x, replace = TRUE)
df1[i1, , drop = FALSE]
})
Upvotes: 2