Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools research support package, R4Econ examples page, PkgTestR packaging guide, or Stat4Econ course page.
Names of each array become row names automatically.
ar_row_one <- c(-1,+1)
ar_row_two <- c(-3,-2)
ar_row_three <- c(0.35,0.75)
mt_n_by_2 <- rbind(ar_row_one, ar_row_two, ar_row_three)
kable(mt_n_by_2) %>%
kable_styling_fc()
ar_row_one | -1.00 | 1.00 |
ar_row_two | -3.00 | -2.00 |
ar_row_three | 0.35 | 0.75 |
# An empty matrix with Logical NA
mt_named <- matrix(data=NA, nrow=2, ncol=2)
colnames(mt_named) <- paste0('c', seq(1,2))
rownames(mt_named) <- paste0('r', seq(1,2))
mt_named
## c1 c2
## r1 NA NA
## r2 NA NA
Allocate with NA or NA_real_ or NA_int_. Clarity in type definition is preferred.
# An empty matrix with Logical NA
mt_na <- matrix(data=NA, nrow=2, ncol=2)
str(mt_na)
## logi [1:2, 1:2] NA NA NA NA
# An empty matrix with numerica NA
mt_fl_na <- matrix(data=NA_real_, nrow=2, ncol=2)
mt_it_na <- matrix(data=NA_integer_, nrow=2, ncol=2)
str(mt_fl_na)
## num [1:2, 1:2] NA NA NA NA
str(mt_fl_na)
## num [1:2, 1:2] NA NA NA NA
Random draw from the normal distribution, random draw from the uniform distribution, and combine resulting matrixes.
# Generate 15 random normal, put in 5 rows, and 3 columns
mt_rnorm <- matrix(rnorm(15,mean=0,sd=1), nrow=5, ncol=3)
# Generate 15 random normal, put in 5 rows, and 3 columns
mt_runif <- matrix(runif(15,min=0,max=1), nrow=5, ncol=5)
# Combine
mt_rnorm_runif <- cbind(mt_rnorm, mt_runif)
# Display
kable(round(mt_rnorm_runif, 3)) %>% kable_styling_fc()
-0.237 | 0.696 | 0.857 | 0.077 | 0.406 | 0.715 | 0.077 | 0.406 |
-0.544 | 0.542 | 1.490 | 0.615 | 0.974 | 0.724 | 0.615 | 0.974 |
-1.295 | -1.222 | -0.214 | 0.234 | 0.916 | 0.694 | 0.234 | 0.916 |
-0.085 | -0.730 | -0.827 | 0.069 | 0.374 | 0.145 | 0.069 | 0.374 |
-0.177 | -0.957 | 0.145 | 0.396 | 0.468 | 0.478 | 0.396 | 0.468 |
Now we generate a matrix with sequential integers, and either fill matrix by columns or fill matrix by rows.
# with byrow set to FALSE, will fill first col, then second col, etc..
mt_index_colbycol <- matrix(seq(0, 15), nrow=4, ncol=4, byrow=FALSE)
# Display
kable(mt_index_colbycol,
caption= "with byrow=FALSE, the default, will fill col by col") %>%
kable_styling_fc()
0 | 4 | 8 | 12 |
1 | 5 | 9 | 13 |
2 | 6 | 10 | 14 |
3 | 7 | 11 | 15 |
# with byrow set to TRUE, will fill row by row
mt_index_rowbyrow <- matrix(seq(0, 15), nrow=4, ncol=4, byrow=TRUE)
# Display
kable(mt_index_rowbyrow,
caption= " with byrow=TRUE, will fill row by row") %>%
kable_styling_fc()
0 | 1 | 2 | 3 |
4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 |
For values in matrix that fall below or above some thresholds, we will replace these values by NA_real_.
fl_max_val <- 0.8
fl_min_val <- 0.2
mt_rnorm_runif_bd <- mt_rnorm_runif
mt_rnorm_runif_bd[which(mt_rnorm_runif < fl_min_val)] <- NA_real_
mt_rnorm_runif_bd[which(mt_rnorm_runif > fl_max_val)] <- NA_real_
# Print
print(mt_rnorm_runif_bd)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] NA 0.6963672 NA NA 0.4062423 0.7153587 NA 0.4062423
## [2,] NA 0.5418076 NA 0.6151660 NA 0.7240569 0.6151660 NA
## [3,] NA NA NA 0.2341533 NA 0.6941645 0.2341533 NA
## [4,] NA NA NA NA 0.3736810 NA NA 0.3736810
## [5,] NA NA NA 0.3958477 0.4675057 0.4778807 0.3958477 0.4675057
Now we sort within each row or within each column of the random matrix.
# Within row sort
mt_rnorm_runif_row_sort <- t(apply(
mt_rnorm_runif, 1, sort
))
# Within column sort, note no transpose
mt_rnorm_runif_col_sort <- apply(
mt_rnorm_runif, 2, sort
)
# Display
kable(round(mt_rnorm_runif_row_sort, 3),
caption="Each row sort low to high") %>%
kable_styling_fc()
-0.237 | 0.077 | 0.077 | 0.406 | 0.406 | 0.696 | 0.715 | 0.857 |
-0.544 | 0.542 | 0.615 | 0.615 | 0.724 | 0.974 | 0.974 | 1.490 |
-1.295 | -1.222 | -0.214 | 0.234 | 0.234 | 0.694 | 0.916 | 0.916 |
-0.827 | -0.730 | -0.085 | 0.069 | 0.069 | 0.145 | 0.374 | 0.374 |
-0.957 | -0.177 | 0.145 | 0.396 | 0.396 | 0.468 | 0.468 | 0.478 |
kable(round(mt_rnorm_runif_col_sort, 3),
caption="Each column sort low to high") %>%
kable_styling_fc()
-1.295 | -1.222 | -0.827 | 0.069 | 0.374 | 0.145 | 0.069 | 0.374 |
-0.544 | -0.957 | -0.214 | 0.077 | 0.406 | 0.478 | 0.077 | 0.406 |
-0.237 | -0.730 | 0.145 | 0.234 | 0.468 | 0.694 | 0.234 | 0.468 |
-0.177 | 0.542 | 0.857 | 0.396 | 0.916 | 0.715 | 0.396 | 0.916 |
-0.085 | 0.696 | 1.490 | 0.615 | 0.974 | 0.724 | 0.615 | 0.974 |
Compute column and row means, and also column and row sums
print(paste0('colSums=',
paste(round(
colSums(mt_rnorm_runif),3), collapse=',')
))
## [1] "colSums=-2.337,-1.671,1.45,1.392,3.137,2.756,1.392,3.137"
print(paste0('colMeans=',
paste(round(
colMeans(mt_rnorm_runif),3), collapse=',')
))
## [1] "colMeans=-0.467,-0.334,0.29,0.278,0.627,0.551,0.278,0.627"
print(paste0('rowSums=',
paste(round(
rowSums(mt_rnorm_runif),3), collapse=',')
))
## [1] "rowSums=2.999,5.39,0.264,-0.611,1.215"
print(paste0('rowMeans=',
paste(round(
rowMeans(mt_rnorm_runif),3), collapse=',')
))
## [1] "rowMeans=0.375,0.674,0.033,-0.076,0.152"
Given some matrix of information, add a column, where all rows of the column have the same numerical value. Use the matrix created prior. - R add column to matrix - r append column to matrix constant value
fl_new_first_col_val <- 111
fl_new_last_col_val <- 999
mt_with_more_columns <- cbind(rep(fl_new_first_col_val, dim(mt_rnorm_runif)[1]),
mt_rnorm_runif,
rep(fl_new_last_col_val, dim(mt_rnorm_runif)[1]))
# Display
kable(mt_with_more_columns) %>% kable_styling_fc_wide()
111 | -0.2366139 | 0.6963672 | 0.8573265 | 0.0769430 | 0.4062423 | 0.7153587 | 0.0769430 | 0.4062423 | 999 |
111 | -0.5437052 | 0.5418076 | 1.4897541 | 0.6151660 | 0.9736633 | 0.7240569 | 0.6151660 | 0.9736633 | 999 |
111 | -1.2946558 | -1.2221654 | -0.2142978 | 0.2341533 | 0.9162547 | 0.6941645 | 0.2341533 | 0.9162547 | 999 |
111 | -0.0847676 | -0.7297558 | -0.8274860 | 0.0694605 | 0.3736810 | 0.1447993 | 0.0694605 | 0.3736810 | 999 |
111 | -0.1772940 | -0.9573648 | 0.1448672 | 0.3958477 | 0.4675057 | 0.4778807 | 0.3958477 | 0.4675057 | 999 |