【发布时间】:2016-08-17 02:24:41
【问题描述】:
所以我有一个数据框:
> MLSpredictions
fit se.fit residual.scale upr lwr
1 1.392213 0.1476321 1 1.681572 1.102854
2 1.448370 0.1709856 1 1.783501 1.113238
3 1.392213 0.1476321 1 1.681572 1.102854
4 1.448370 0.1709856 1 1.783501 1.113238
5 1.448370 0.1709856 1 1.783501 1.113238
6 1.448370 0.1709856 1 1.783501 1.113238
7 1.506792 0.1969097 1 1.892734 1.120849
8 1.506792 0.1969097 1 1.892734 1.120849
9 1.567570 0.2253572 1 2.009270 1.125870
10 1.567570 0.2253572 1 2.009270 1.125870
11 1.630800 0.2563338 1 2.133214 1.128386
12 1.448370 0.1709856 1 1.783501 1.113238
13 1.448370 0.1709856 1 1.783501 1.113238
14 1.448370 0.1709856 1 1.783501 1.113238
15 1.506792 0.1969097 1 1.892734 1.120849
16 1.567570 0.2253572 1 2.009270 1.125870
17 1.567570 0.2253572 1 2.009270 1.125870
18 1.567570 0.2253572 1 2.009270 1.125870
19 1.567570 0.2253572 1 2.009270 1.125870
我想对整个数据框进行两次采样,并将这两个样本添加到新的数据框 MLSSeason:
我的尝试是:
MLSSeason[1:19] = sample(MLSpredictions)
MLSSeason[20:38] = sample(MLSpredictions)
但这并没有给我正确的解决方案。理想情况下,MLSSeason 将有 38 行,其中每个 MLSprediction 采样两个。
【问题讨论】:
-
MLSSeason = sample(MLSpredictions)有效,但是当我尝试将 MLSSeason 的赛季翻倍时,我遇到了一些麻烦。 -
使用
MLSpredictions[sample(nrow(MLSpredictions)),] -
df[sample(1:(nrow(df)*2))/2,]应该在 df 为MLSpredictions的情况下工作。它将为您提供行名,指示哪些是重复的,例如,第 4.1 行是第 4 行的重复样本。 -
@sayaa 有效。非常感谢。你能解释一下那个代码是怎么回事吗?我看到我们从 1:38 开始采样,然后将其除以 2。我不太确定这是如何为我们提供解决方案的。
-
我们乘以 2 得到 38 行并除以 2 以便我们索引原始数据框中的正确行。