# r faq - Comment faire un grand exemple reproductible R

Mots clés : rrrr-faqr

### meilleur 3 Réponses r faq - Comment faire un grand exemple reproductible R

97

``d <- as.Date("2020-12-30") ``
``class(d) # [1] "Date" ``
``x <- rnorm(10)  ## random vector normal distributed x <- runif(10)  ## random vector uniformly distributed     x <- sample(1:100, 10)  ## 10 random draws out of 1, 2, ..., 100     x <- sample(LETTERS, 10)  ## 10 random draws out of built-in latin alphabet ``
``m <- matrix(1:12, 3, 4, dimnames=list(LETTERS[1:3], LETTERS[1:4])) m #   A B C  D # A 1 4 7 10 # B 2 5 8 11 # C 3 6 9 12 ``
``set.seed(42)  ## for sake of reproducibility n <- 6 dat <- data.frame(id=1:n,                    date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),                   group=rep(LETTERS[1:2], n/2),                   age=sample(18:30, n, replace=TRUE),                   type=factor(paste("type", 1:n)),                   x=rnorm(n)) dat #   id       date group age   type         x # 1  1 2020-12-26     A  27 type 1 0.0356312 # 2  2 2020-12-27     B  19 type 2 1.3149588 # 3  3 2020-12-28     A  20 type 3 0.9781675 # 4  4 2020-12-29     B  26 type 4 0.8817912 # 5  5 2020-12-30     A  26 type 5 0.4822047 # 6  6 2020-12-31     B  28 type 6 0.9657529 ``
``  id       date group age   type         x 1  1 2020-12-26     A  27 type 1 0.0356312 2  2 2020-12-27     B  19 type 2 1.3149588 3  3 2020-12-28     A  20 type 3 0.9781675 ``
``dput(iris[1:4, ]) # first four rows of the iris data set ``
``structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6), Sepal.Width = c(3.5,  3, 3.2, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5), Petal.Width = c(0.2,  0.2, 0.2, 0.2), Species = structure(c(1L, 1L, 1L, 1L), .Label = c("setosa",  "versicolor", "virginica"), class = "factor")), row.names = c(NA,  4L), class = "data.frame") ``
``set.seed(42) rnorm(3) # [1]  1.3709584 -0.5646982  0.3631284  set.seed(42) rnorm(3) # [1]  1.3709584 -0.5646982  0.3631284 ``

90

``my.df <- data.frame(col1 = sample(c(1,2), 10, replace = TRUE),         col2 = as.factor(sample(10)), col3 = letters[1:10],         col4 = sample(c(TRUE, FALSE), 10, replace = TRUE)) my.list <- list(list1 = my.df, list2 = my.df[3], list3 = letters) ``
``library(vegan) data(varespec) ord <- metaMDS(varespec) ``
``my.df2 <- data.frame(a = sample(10e6), b = sample(letters, 10e6, replace = TRUE)) ``
``library(raster) r1 <- r2 <- r3 <- raster(nrow=10, ncol=10) values(r1) <- runif(ncell(r1)) values(r2) <- runif(ncell(r2)) values(r3) <- runif(ncell(r3)) s <- stack(r1, r2, r3) ``
``library(rgdal) ogrDrivers() dsn <- system.file("vectors", package = "rgdal")[1] ogrListLayers(dsn) ogrInfo(dsn=dsn, layer="cities") cities <- readOGR(dsn=dsn, layer="cities") ``

72

``install.packages("devtools") library(devtools) source_url("https://raw.github.com/rsaporta/pubR/gitbranch/reproduce.R")  reproduce(myData) ``
``# sample data DF <- data.frame(id=rep(LETTERS, each=4)[1:100], replicate(100, sample(1001, 100)), Class=sample(c("Yes", "No"), 100, TRUE)) ``
``reproduce(DF, cols=c("id", "X1", "X73", "Class"))  # I could also specify the column number. ``
``This is what the sample looks like:      id  X1 X73 Class 1    A 266 960   Yes 2    A 373 315    No            Notice the selection split 3    A 573 208    No           (which can be turned off) 4    A 907 850   Yes 5    B 202  46   Yes 6    B 895 969   Yes   <~~~ 70 % of selection is from the top rows 7    B 940 928    No 98   Y 371 171   Yes 99   Y 733 364   Yes   <~~~ 30 % of selection is from the bottom rows. 100  Y 546 641    No       ==X==============================================================X==          Copy+Paste this part. (If on a Mac, it is already copied!)     ==X==============================================================X==   DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L, 25L, 25L), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"), class = "factor"), X1 = c(266L, 373L, 573L, 907L, 202L, 895L, 940L, 371L, 733L, 546L), X73 = c(960L, 315L, 208L, 850L, 46L, 969L, 928L, 171L, 364L, 641L), Class = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label = c("No", "Yes"), class = "factor")), .Names = c("id", "X1", "X73", "Class"), class = "data.frame", row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))      ==X==============================================================X== ``
``    ==X==============================================================X==          Copy+Paste this part. (If on a Mac, it is already copied!)     ==X==============================================================X==   DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L,25L, 25L), .Label       = c("A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y"), class = "factor"),       X1 = c(809L, 81L, 862L,747L, 224L, 721L, 310L, 53L, 853L, 642L),       X2 = c(926L, 409L,825L, 702L, 803L, 63L, 319L, 941L, 598L, 830L),       X16 = c(447L,164L, 8L, 775L, 471L, 196L, 30L, 420L, 47L, 327L),       X22 = c(335L,164L, 503L, 407L, 662L, 139L, 111L, 721L, 340L, 178L)), .Names = c("id","X1",       "X2", "X16", "X22"), class = "data.frame", row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))      ==X==============================================================X== ``