This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
tosscoin <- function(n) {
df <- expand.grid(replicate(n, c("cara", "sello"), simplify = FALSE),
KEEP.OUT.ATTRS = FALSE,
stringsAsFactors = FALSE)
names(df) <- paste0("X", seq_len(n))
return(df)
}
tosscoin(3)
## X1 X2 X3
## 1 cara cara cara
## 2 sello cara cara
## 3 cara sello cara
## 4 sello sello cara
## 5 cara cara sello
## 6 sello cara sello
## 7 cara sello sello
## 8 sello sello sello
urnsamples <- function(x, size, ordered = TRUE, replace = FALSE) {
if (ordered) {
# Producto cartesiano
expand.grid(replicate(size, x, simplify = FALSE),
KEEP.OUT.ATTRS = FALSE)
} else {
# Combinaciones sin orden
combn(x, size, simplify = FALSE) |>
do.call(what = rbind)
}
}
urnsamples(1:3, size = 2, ordered = FALSE, replace = FALSE)
## [,1] [,2]
## [1,] 1 2
## [2,] 1 3
## [3,] 2 3
nsamp <- function(n, k, replace = FALSE, ordered = TRUE) {
if (ordered) {
if (replace) {
return(n^k) # Producto cartesiano con repetición
} else {
return(factorial(n) / factorial(n - k)) # Permutaciones sin repetición
}
} else {
if (replace) {
return(choose(n + k - 1, k)) # Combinaciones con repetición
} else {
return(choose(n, k)) # Combinaciones sin repetición
}
}
}
nsamp(n = 5, k = 5, replace = FALSE, ordered = TRUE)
## [1] 120