问题描述
我想从 data.frame
的每一行中独立于其他行抽取随机样本.这是一个例子.此代码为每一行选择相同的列,但我需要为每一行独立选择列.
I want to draw random sample from each row of a data.frame
independently from other rows. Here is an example. This code selects the same column for each row but I require independent selection of columns for each row.
library(plyr)
set.seed(12345)
df1 <- mdply(data.frame(mean=c(10, 15)), rnorm, n = 5, sd = 1)
df1
mean V1 V2 V3 V4 V5
1 10 10.58553 10.70947 9.890697 9.546503 10.60589
2 15 13.18204 15.63010 14.723816 14.715840 14.08068
> df1[ , -1]
V1 V2 V3 V4 V5
1 10.58553 10.70947 9.890697 9.546503 10.60589
2 13.18204 15.63010 14.723816 14.715840 14.08068
> sample(df1[, -1], replace = TRUE)
V3 V2 V5 V4 V4.1
1 9.890697 10.70947 10.60589 9.546503 9.546503
2 14.723816 15.63010 14.08068 14.715840 14.715840
> t(apply(df1[, -1], 1, sample))
[,1] [,2] [,3] [,4] [,5]
[1,] 10.70947 9.890697 10.60589 10.58553 9.546503
[2,] 14.71584 13.182044 14.08068 15.63010 14.723816
已编辑
df1[ , -1]
V1 V2 V3 V4 V5
1 10.58553 10.70947 9.890697 9.546503 10.60589
2 13.18204 15.63010 14.723816 14.715840 14.08068
sample(df1[, -1], replace = TRUE)
V3 V2 V5 V4 V4.1
1 9.890697 10.70947 10.60589 9.546503 9.546503
2 14.723816 15.63010 14.08068 14.715840 14.715840
sample(df1[, -1], replace = TRUE)
选择列 V3
, V2
, V5
、V4
和 V4
用于两行.但我要求它可以选择列 V3
、V2
、V5
、V4
和 V4<
第一行
的/code>和/或第二行
的五列的任意组合.
sample(df1[, -1], replace = TRUE)
selects the columns V3
, V2
, V5
, V4
, and V4
for both rows. But I require that it could select columns V3
, V2
, V5
, V4
, and V4
for first row
and/or any combinations of five columns for second row
.
推荐答案
您可以将
apply
与 replace=TRUE
一起用于 sample
You could use
apply
with replace=TRUE
for the sample
t(apply(df1[,-1], 1, sample, replace=TRUE))
这篇关于来自 data.frame 的每一列的随机样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!