【发布时间】:2019-07-13 02:43:13
【问题描述】:
如果能随机选择var.w_X 的子集,我将不胜感激
我的样本数据sampleDT 中包含10 var.w_X 变量中的5,同时保留所有其他不以var.w_ 开头的变量。
以下是样本数据sampleDT,其中包含除其他变量(将完全保留的变量)之外的X 变量,它们的名称以var.w_ 开头(从中抽取随机样本)。
在当前示例中,X=10,因此var.w_ 包括var.w_1 到var.w_10,我想从这些10 中抽取5 的随机样本。但是,在我的实际数据中,X>1,000,000 并且我可能想从这些X>1,000,000 中抽取7,500var.w_ 变量的样本。
因此,在任何给定的解决方案中考虑效率都是最重要的,因为recently 我遇到了mutate_at 的一些性能问题,原因我仍然没有解释。
重要的是,要保留的其他变量(不以 var.w_ 开头的变量)不能保证保持任何预先指定的顺序,因为它们可能位于 @987654343 之前和/或之间和/或之后@变量,例如。因此依赖列顺序的解决方案将不起作用。
#样本数据
sampleDT<-structure(list(n = c(62L, 96L, 17L, 41L, 212L, 143L, 143L, 143L,
73L, 73L), r = c(3L, 1L, 0L, 2L, 170L, 21L, 0L, 33L, 62L, 17L
), p = c(0.0483870967741935, 0.0104166666666667, 0, 0.0487804878048781,
0.80188679245283, 0.146853146853147, 0, 0.230769230769231, 0.849315068493151,
0.232876712328767), var.w_8 = c(1.94254385942857, 1.18801169942857,
3.16131123942857, 3.16131123942857, 1.13482609242857, 1.13042157942857,
2.13042157942857, 1.13042157942857, 1.12335579942857, 1.12335579942857
), var.w_9 = c(1.942365288, 1.187833128, 3.161132668, 3.161132668,
1.134647521, 1.130243008, 2.130243008, 1.130243008, 1.123177228,
1.123177228), var.w_10 = c(1.94222639911111, 1.18769423911111,
3.16099377911111, 3.16099377911111, 1.13450863211111, 1.13010411911111,
2.13010411911111, 1.13010411911111, 1.12303833911111, 1.12303833911111
), group = c(1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L,
0L, 0L), treat = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), c1 = c(1.941115288,
1.186583128, 1.159882668, 1.159882668, 1.133397521, 1.128993008,
1.128993008, 1.128993008, 1.121927228, 1.121927228), var.w_6 = c(1.939115288, 1.184583128,
3.157882668, 3.157882668, 1.131397521, 1.126993008, 2.126993008,
1.126993008, 1.119927228, 1.119927228), var.w_7 = c(1.94278195466667,
1.18824979466667, 3.16154933466667, 3.16154933466667, 1.13506418766667,
1.13065967466667, 2.13065967466667, 1.13065967466667, 1.12359389466667,
1.12359389466667), c2 = c(0.1438,
0.237, 0.2774, 0.2774, 0.2093, 0.1206, 0.1707, 0.0699, 0.1351,
0.1206), var.w_1 = c(1.941115288, 1.186583128, 3.159882668, 3.159882668,
1.133397521, 1.128993008, 2.128993008, 1.128993008, 1.121927228,
1.121927228), var.w_2 = c(1.931115288, 1.176583128, 3.149882668,
3.149882668, 1.123397521, 1.118993008, 2.118993008, 1.118993008,
1.111927228, 1.111927228), var.w_3 = c(1.946115288, 1.191583128,
3.164882668, 3.164882668, 1.138397521, 1.133993008, 2.133993008,
1.133993008, 1.126927228, 1.126927228), var.w_4 = c(1.93778195466667,
1.18324979466667, 3.15654933466667, 3.15654933466667, 1.13006418766667,
1.12565967466667, 2.12565967466667, 1.12565967466667, 1.11859389466667,
1.11859389466667), var.w_5 = c(1.943615288, 1.189083128, 3.162382668,
3.162382668, 1.135897521, 1.131493008, 2.131493008, 1.131493008,
1.124427228, 1.124427228)), class = "data.frame", row.names = c(NA, -10L))
#我的尝试
//based on the comment by @akrun - this does not keep the other variables as specified above
myvars <- sample(grep("var\\.w_", names(sampleDT), value = TRUE), 5)
sampleDT_test <- sampleDT[myvars]
提前感谢您的帮助
【问题讨论】:
-
试试
sample(grep("var\\.w_", names(sampleDT), value = TRUE), 5) -
谢谢,@akrun。执行
sampleDT_test<-sample(grep("var\\.w_", names(sampleDT), value = TRUE), 5)会得到head(sampleDT_test) [1] "var.w_4" "var.w_7" "var.w_6" [4] "var.w_9" "var.w_5",这不是我想要的问题详细说明。 -
是否有理由只做
c(names(sampleDT)[1:7], sampleDT_test)不能对您的数据集进行子集化?即sampleDT[,c(names(sampleDT)[1:7], sampleDT_test)]。如果这不适用于您的数据集,您能否详细说明原因? -
@Andrew,因为正如问题本身所指定的
In the current example, X=10, so that var.w_ includes var.w_1 to var.w_10, and I want to draw a random sample of 5 out of these 10. However, in my actual data, X>1,000,000and I might want to draw a sample of 7,500 var.w_ variables out of these X>1,000,000.。因此,我需要一种更高效且可扩展的方法,并且随机性至关重要。 -
@Krantz,我读了你的问题。我不清楚 akrun 的解决方案如何不适用于解决您的问题。是不是太慢了,它没有返回您想看到的内容,您能否提供更多信息,说明它为什么不起作用/以不同于引用您的问题的方式还需要什么?
标签: r function random data.table tidyr