Description Usage Arguments Examples
Row names are not preserved.
1 |
x |
object from which to extract elements |
size |
a positive number, the number of items to choose. |
replace |
Should sampling be with replacement? |
prob |
A vector of probability weights for obtaining the elements of the vector being sampled. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #Sampling from a list
l1<-as.list(1:1e6)
b<-sample2(l1,1e5)
#Sampling from a data frame
orders<-data.frame(orderNum=sample(1e5, 1e6, TRUE),
sku=sample(1e3, 1e6, TRUE),
customer=sample(1e4,1e6,TRUE),stringsAsFactors=FALSE)
a<-sample2(orders,250000)
#With oversampling sample2 can be much faster than the alternatives,
#with the caveat that it does not preserve row names.
system.time(a<-sample2(orders,2000000,TRUE))
system.time(b<-orders[sample.int(nrow(orders),2000000,TRUE),])
## Not run:
system.time(c<-dplyr::sample_n(orders,2000000,replace=TRUE))
#Can quickly sample for sparse matrices while preserving sparsity
sm<-rsparsematrix(20000000,10000,density=.0001)
sm2<-sample2(sm,1000000)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.