从 R 中的两列重新采样行
Re-sampling rows from two columns in R
我有一个包含 2 列的数据集(比如 X 和 Y)。我需要生成两列的 100 个样本并进行替换,其中每个样本有 10 行。
X 和 Y 值应该是相关的。这意味着基本上我需要重新采样行。
到目前为止,这是我生成 1 个样本的工作,
set.seed(326581)
X1=rnorm(10,0,1)
Y1=rnorm(10,0,2)
data=data.frame(X1,Y1)
library("dplyr", lib.loc="~/R/win-library/3.5")
sample_n(data,10,replace = T)
因为我需要 100 个样本,所以我尝试了 R 中的复制功能。但它没有用。所以我尝试使用循环。
rex=c()
rey=c()
for(i in 1:100)
{
rex[i]=sample_n(data,10,replace=T)[,1]
rey[i]=sample_n(data,10,replace=T)[,2]
}
但它没有给出所需的输出。
谁能帮我找出错误?
或者是否有任何功能(如从 1 列采样的复制功能)更容易做到这一点?
你的意思是这样的吗?
使用基数 R sample
# Sample 100 rows from data
set.seed(2017)
idx <- sample(nrow(data), 100, replace = T)
# Store samples in a 100x2 data.frame
df.smpl <- data[idx, ]
使用dplyr::sample_n
set.seed(2017)
df.smpl <- data %>%
sample_n(100, replace = T)
这两种方法都将从 data
的行中抽取(带替换)的 100 个样本存储在新的 data.frame
维度
中
dim(data.smpl)
#[1] 100 2
或者如果您希望样本位于 list
个向量中
lapply(idx, function(i) as.numeric(data[i, ]))
更新
要从 data
重复绘制 10 行 100 次,您可以使用 replicate
set.seed(2017);
lst <- replicate(
100,
df.smpl <- data %>% sample_n(10, replace = T),
simplify = FALSE)
这会生成一个 list
和 100 data.frame
s
length(lst)
[1] 100
每个维度 10x2
sapply(lst, dim)
#[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#[,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,99] [,100]
#[1,] 10 10
#[2,] 2 2
我有一个包含 2 列的数据集(比如 X 和 Y)。我需要生成两列的 100 个样本并进行替换,其中每个样本有 10 行。
X 和 Y 值应该是相关的。这意味着基本上我需要重新采样行。
到目前为止,这是我生成 1 个样本的工作,
set.seed(326581)
X1=rnorm(10,0,1)
Y1=rnorm(10,0,2)
data=data.frame(X1,Y1)
library("dplyr", lib.loc="~/R/win-library/3.5")
sample_n(data,10,replace = T)
因为我需要 100 个样本,所以我尝试了 R 中的复制功能。但它没有用。所以我尝试使用循环。
rex=c()
rey=c()
for(i in 1:100)
{
rex[i]=sample_n(data,10,replace=T)[,1]
rey[i]=sample_n(data,10,replace=T)[,2]
}
但它没有给出所需的输出。
谁能帮我找出错误? 或者是否有任何功能(如从 1 列采样的复制功能)更容易做到这一点?
你的意思是这样的吗?
使用基数 R sample
# Sample 100 rows from data
set.seed(2017)
idx <- sample(nrow(data), 100, replace = T)
# Store samples in a 100x2 data.frame
df.smpl <- data[idx, ]
使用dplyr::sample_n
set.seed(2017)
df.smpl <- data %>%
sample_n(100, replace = T)
这两种方法都将从 data
的行中抽取(带替换)的 100 个样本存储在新的 data.frame
维度
dim(data.smpl)
#[1] 100 2
或者如果您希望样本位于 list
个向量中
lapply(idx, function(i) as.numeric(data[i, ]))
更新
要从 data
重复绘制 10 行 100 次,您可以使用 replicate
set.seed(2017);
lst <- replicate(
100,
df.smpl <- data %>% sample_n(10, replace = T),
simplify = FALSE)
这会生成一个 list
和 100 data.frame
s
length(lst)
[1] 100
每个维度 10x2
sapply(lst, dim)
#[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#[,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]
#[1,] 10 10 10 10 10 10 10 10 10 10 10 10
#[2,] 2 2 2 2 2 2 2 2 2 2 2 2
#[,99] [,100]
#[1,] 10 10
#[2,] 2 2