我正在努力寻找合适的函数,将返回指定数量的行随机拾取,没有从R语言的数据帧替换?有人能帮帮我吗?


当前回答

2021年在潮流宇宙中这样做的方式是:

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包在2021-10-05创建(v2.0.0.9000)

其他回答

过时的答案。请使用dplyr::sample_frac()或dplyr::sample_n()代替。

在我的R包中有一个函数示例。仅用于此目的的行:

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对之前的回答的评论,通过使它成为通用的S3函数来增强sample是一个坏主意。

2021年在潮流宇宙中这样做的方式是:

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包在2021-10-05创建(v2.0.0.9000)

为了完整起见:

Dplyr还提供绘制样本的比例或分数

df %>% sample_frac(0.33)

这是非常方便的,例如,在机器学习中,当你必须做一个特定的分割比例,如80%:20%

你可以这样做:

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

约翰·科尔比给出的答案是正确的。然而,如果你是一个dplyr用户,也有答案sample_n:

sample_n(df, 10)

从数据框架中随机抽取10行。它调用sample.int,因此实际上是相同的答案,但输入更少(并且简化了在magrittr上下文中的使用,因为dataframe是第一个参数)。