【发布时间】:2012-01-06 13:58:24
【问题描述】:
我正在努力寻找合适的函数,该函数将返回随机选取的指定行数,而无需从 R 语言的数据框中进行替换?谁能帮帮我?
【问题讨论】:
我正在努力寻找合适的函数,该函数将返回随机选取的指定行数,而无需从 R 语言的数据框中进行替换?谁能帮帮我?
【问题讨论】:
先做一些数据:
> df = data.frame(matrix(rnorm(20), nrow=10))
> df
X1 X2
1 0.7091409 -1.4061361
2 -1.1334614 -0.1973846
3 2.3343391 -0.4385071
4 -0.9040278 -0.6593677
5 0.4180331 -1.2592415
6 0.7572246 -0.5463655
7 -0.8996483 0.4231117
8 -1.0356774 -0.1640883
9 -0.3983045 0.7157506
10 -0.9060305 2.3234110
然后随机选择一些行:
> df[sample(nrow(df), 3), ]
X1 X2
9 -0.3983045 0.7157506
2 -1.1334614 -0.1973846
10 -0.9060305 2.3234110
【讨论】:
set.seed(42) )。
sample.int 我相信会稍微快一点:library(microbenchmark);microbenchmark( sample( 10000, 100 ), sample.int( 10000, 100 ), times = 10000 )
John Colby 给出的答案是正确的答案。但是,如果您是dplyr 用户,那么还有sample_n 的答案:
sample_n(df, 10)
从数据框中随机抽取 10 行。它调用sample.int,所以实际上是相同的答案,输入更少(并简化了在 magrittr 上下文中的使用,因为数据帧是第一个参数)。
【讨论】:
data.table 包提供函数DT[sample(.N, M)],从数据表DT 中随机抽取M 行。
library(data.table)
set.seed(10)
mtcars <- data.table(mtcars)
mtcars[sample(.N, 6)]
mpg cyl disp hp drat wt qsec vs am gear carb
1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
6: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
【讨论】:
写一个!包装JC的答案给了我:
randomRows = function(df,n){
return(df[sample(nrow(df),n),])
}
现在通过首先检查是否 n
【讨论】:
为了完整起见:
dplyr 还提供通过
提取样本的比例或分数df %>% sample_frac(0.33)
这非常方便,例如在机器学习中,当你必须做一个特定的分割比例,比如 80%:20%
【讨论】:
编辑:此答案现已过时,请参阅the updated version。
在my R package 中,我增强了sample,现在它对于数据帧的行为也符合预期:
library(devtools); install_github('kimisc', 'krlmlr')
library(kimisc)
example(sample.data.frame)
smpl..> set.seed(42)
smpl..> sample(data.frame(a=c(1,2,3), b=c(4,5,6),
row.names=c('a', 'b', 'c')), 10, replace=TRUE)
a b
c 3 6
c.1 3 6
a 1 4
c.2 3 6
b 2 5
b.1 2 5
c.3 3 6
a.1 1 4
b.2 2 5
c.4 3 6
这是achieved,通过使sample 成为S3 通用方法并在函数中提供必要的(微不足道的)功能。拨打setMethod 可以解决所有问题。仍然可以通过base::sample 访问原始实现。
【讨论】:
df 调用sample.default(df, ...) 时,它从数据框的列 中采样,因为数据框被实现为相同长度的向量列表。
install_github('kimisc', 'krlmlr'),得到了Error: Does not appear to be an R package (no DESCRIPTION)。有什么办法吗?
[ 运算符就是一个反例。另外,请告诉我:您是否曾经使用过sample 对数据框中的列进行采样?
iris[2] 像列表一样工作,iris[[2]] 也是如此。或者iris$Species, lapply(iris, mean), ... 数据框是列表。所以我希望他们表现得像他们一样。是的,我实际上使用了 sample(myDataframe)。在每个变量都包含单个基因的表达数据的数据集上。你的具体方法帮助了新手用户,也有效地改变了sample()behaves的方式。注意我从程序员的角度使用“如预期”。这与一般的直觉不同。 R中有很多与一般直觉不兼容的东西......;)
过时的答案。请改用
dplyr::sample_frac()或dplyr::sample_n()。
在my R package 中有一个函数sample.rows 就是为了这个目的:
install.packages('kimisc')
library(kimisc)
example(sample.rows)
smpl..> set.seed(42)
smpl..> sample.rows(data.frame(a=c(1,2,3), b=c(4,5,6),
row.names=c('a', 'b', 'c')), 10, replace=TRUE)
a b
c 3 6
c.1 3 6
a 1 4
c.2 3 6
b 2 5
b.1 2 5
c.3 3 6
a.1 1 4
b.2 2 5
c.4 3 6
根据 Joris Meys 对previous answer 的 cmets 的说法,通过将 sample 设为通用 S3 函数来增强它是一个坏主意。
【讨论】:
你可以这样做:
library(dplyr)
cols <- paste0("a", 1:10)
tab <- matrix(1:1000, nrow = 100) %>% as.tibble() %>% set_names(cols)
tab
# A tibble: 100 x 10
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 101 201 301 401 501 601 701 801 901
2 2 102 202 302 402 502 602 702 802 902
3 3 103 203 303 403 503 603 703 803 903
4 4 104 204 304 404 504 604 704 804 904
5 5 105 205 305 405 505 605 705 805 905
6 6 106 206 306 406 506 606 706 806 906
7 7 107 207 307 407 507 607 707 807 907
8 8 108 208 308 408 508 608 708 808 908
9 9 109 209 309 409 509 609 709 809 909
10 10 110 210 310 410 510 610 710 810 910
# ... with 90 more rows
上面我刚刚做了一个 10 列 100 行的数据框,好吗?
现在您可以使用sample_n 对其进行采样:
sample_n(tab, size = 800, replace = T)
# A tibble: 800 x 10
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 53 153 253 353 453 553 653 753 853 953
2 14 114 214 314 414 514 614 714 814 914
3 10 110 210 310 410 510 610 710 810 910
4 70 170 270 370 470 570 670 770 870 970
5 36 136 236 336 436 536 636 736 836 936
6 77 177 277 377 477 577 677 777 877 977
7 13 113 213 313 413 513 613 713 813 913
8 58 158 258 358 458 558 658 758 858 958
9 29 129 229 329 429 529 629 729 829 929
10 3 103 203 303 403 503 603 703 803 903
# ... with 790 more rows
【讨论】:
从 R 中的 tibble 类型中选择一个随机样本:
library("tibble")
a <- your_tibble[sample(1:nrow(your_tibble), 150),]
nrow 接受一个小标题并返回行数。传递给sample 的第一个参数是从 1 到小标题结尾的范围。传递给 sample 的第二个参数 150 是您想要的随机采样数。方括号切片指定返回的索引的行。变量“a”获取随机抽样的值。
【讨论】:
你可以这样做:
sample_data = data[sample(nrow(data), sample_size, replace = FALSE), ]
【讨论】:
我是 R 新手,但我使用的这种简单方法对我有用:
sample_of_diamonds <- diamonds[sample(nrow(diamonds),100),]
PS:如果它有一些我没有想到的缺点,请随时指出。
【讨论】:
(rest_of_diamonds <- diamonds[which(!diamonds %in% sample_of_diamonds)])
diamonds 或main dataset 相同的输出。
sample_of_diamonds 中的元素,你能确认sample_of_diamonds 不是空的吗?这可以解释你的问题。
sample_of_diamonds <- diamonds[sample(nrow(diamonds),10),] 我得到10 rows randomly 和rest_of_diamonds <- diamonds[which(!diamonds %in% sample_of_diamonds)] 我得到20 rows (main dataset)
2021 年在 tidyverse 中这样做的方式是:
library(tidyverse)
df = data.frame(
A = letters[1:10],
B = 1:10
)
df
#> A B
#> 1 a 1
#> 2 b 2
#> 3 c 3
#> 4 d 4
#> 5 e 5
#> 6 f 6
#> 7 g 7
#> 8 h 8
#> 9 i 9
#> 10 j 10
df %>% sample_n(5)
#> A B
#> 1 e 5
#> 2 g 7
#> 3 h 8
#> 4 b 2
#> 5 j 10
df %>% sample_frac(0.5)
#> A B
#> 1 i 9
#> 2 g 7
#> 3 j 10
#> 4 c 3
#> 5 b 2
由reprex package (v2.0.0.9000) 于 2021-10-05 创建
【讨论】:
正如@matt_b 所指出的,sample_n() 和sample_frac() 已被软弃用,取而代之的是slice_sample()。请参阅dplyr docs。
来自文档字符串的示例:
# slice_sample() allows you to random select with or without replacement
mtcars %>% slice_sample(n = 5)
mtcars %>% slice_sample(n = 5, replace = TRUE)
【讨论】: