ch3
This commit is contained in:
parent
5a8a1a43f6
commit
2f2b1a65c9
43
notes.R
43
notes.R
@ -48,3 +48,46 @@ decode
|
||||
decode[mat+1]
|
||||
|
||||
skim(mat)
|
||||
|
||||
`if`(FALSE,1,3:4) ## This is suposedly better than ifelse
|
||||
if (TRUE) 1 else 3:4
|
||||
|
||||
## 3.4.5.1
|
||||
|
||||
cv_split <- function(x, n){
|
||||
indices <- sample(rep_len(seq_len(n),length(x)))
|
||||
split(x,indices)
|
||||
}
|
||||
|
||||
cv_split(sample(30),4)
|
||||
|
||||
|
||||
## 3.4.5.2
|
||||
|
||||
set.seed(1);(x <- rnorm(10))
|
||||
|
||||
mean(x)
|
||||
|
||||
sample(100,replace = TRUE)
|
||||
|
||||
s <- replicate(1000, {
|
||||
x2 <- sample(x, replace = TRUE)
|
||||
mean(x2)
|
||||
})
|
||||
|
||||
ci <- quantile(s,probs=c(.025,.975))
|
||||
|
||||
hist(s)
|
||||
abline(v=ci)
|
||||
|
||||
# 3.4.5.3
|
||||
my_mtcars <- mtcars[c("mpg", "hp")]
|
||||
my_mtcars$my_col <- sample(c("mpg", "hp"), size = nrow(my_mtcars), replace = TRUE)
|
||||
head(my_mtcars)
|
||||
|
||||
|
||||
# Selects only col 1:2 to keep as numeric when converted to matrix.
|
||||
# Subsets by a matrix by match function. Much faster than for loop
|
||||
my_mtcars$my_val <- my_mtcars[1:2][cbind(seq_along(my_mtcars$my_col),
|
||||
match(my_mtcars$my_col,
|
||||
colnames(my_mtcars)[1:2]))]
|
||||
|
Loading…
Reference in New Issue
Block a user