Skip to content

Instantly share code, notes, and snippets.

View nilforooshan's full-sized avatar

Mohammad Ali Nilforooshan nilforooshan

View GitHub Profile
@nilforooshan
nilforooshan / cov2corcov.md
Created June 20, 2024 08:26
R: Replace the upper triangle of a covariance matrix with a correlation matrix

Replace the upper triangle of a covariance matrix with a correlation matrix

(x <- matrix(c(1,0.2,0.5,1.2,0.2,2,0.5,1,0.5,0.5,3,1.6,1.2,1,1.6,4), nrow = 4))
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.2  0.5  1.2
[2,] 0.2 2.0 0.5 1.0
@nilforooshan
nilforooshan / Loop_Through_Array.md
Created June 12, 2024 08:49
sh: Loop Through Array Values

Loop Through Array Values

countries=("India" "France" "United Kingdom")

# Loop through the array
for country in "${countries[@]}"
do
   echo "$country"
done
@nilforooshan
nilforooshan / repeat_n_times.sh
Created June 12, 2024 08:37
sh: Repeat a string "n" times
for i in {1..10}; do echo -n "Hi"; done
@nilforooshan
nilforooshan / install_arrow.md
Last active April 19, 2024 10:20
R: Install R package arrow on a Linux server

Install R package arrow on a Linux server

sudo apt-get update && sudo apt-get install -y --no-install-recommends r-base # Install R
sudo apt-get -y install libcurl4-openssl-dev libssl-dev make g++ # Install dependencies for arrow package
sudo R -e "install.packages('arrow', dependencies=TRUE)" # Install R package arrow
@nilforooshan
nilforooshan / vec2sqrmat.md
Last active July 10, 2024 21:47
R: Turn a vector to a symmetric matrix (row-wise) - specially useful for APEX

Turn a vector to a symmetric matrix (row-wise)

vec2symmat <- function(x) {
    mat.dim <- (sqrt(1 + 8*length(x)) - 1)/2
    if(mat.dim != round(mat.dim)) stop("The vector length does not match with a square matrix dimension.")
    output <- matrix(0, nrow= mat.dim, ncol=mat.dim)
    rownum <- colnum <- c()
    for(i in 1:mat.dim)
 {
@nilforooshan
nilforooshan / get_colClasses.md
Created March 18, 2024 00:12
R: Get colClasses of a data.frame

Get colClasses of a data.frame

sapply(iris, class)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
   "numeric"    "numeric"    "numeric"    "numeric"     "factor"
@nilforooshan
nilforooshan / param_in_file.md
Created March 13, 2024 10:19
sh: Store a parameter in a file and read it as a variable

Store a parameter in a file and read it as a variable

cat file.txt

121

read -r myNumber &lt; file.txt
@nilforooshan
nilforooshan / cor2cov.md
Last active March 13, 2024 02:50
R: R function for converting correlations and variances to covariances

R function for converting correlations and variances to covariances

The R "stats" package provides the cov2cor function. However, there is no cor2cov function available.

cor2cov <- function(cor.mat, var.vec) {
    # Check inputs
    stopifnot(isSymmetric(cor.mat))
    stopifnot(identical(diag(cor.mat), rep(1, length(diag(cor.mat)))))
 stopifnot(is.vector(var.vec))
@nilforooshan
nilforooshan / datatable_column_as_vector.md
Created March 11, 2024 21:15
R: Three ways of extracting a data.table column as a vector

Three ways of extracting a data.table column as a vector

DT = data.table(
    ID = c("b","b","b","a","a","c"),
    a = 1:6,
    b = 7:12,
    c = 13:18
)
DT[[1]]
@nilforooshan
nilforooshan / count_pattern_each_column.md
Created March 11, 2024 03:09
awk: Iterate over columns of a file and count a pattern in each column

Iterate over columns of a file and count a pattern in each column

Count -9999 occurrences in each column of file.csv.

awk -F',' '{
    for (i = 1; i <= NF; i++) {
        if ($i == -9999) {
            count[i]++
 }