Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
nilforooshan / EC2_GitHub.md
Created May 6, 2025 23:24
aws: Access GitHub on EC2

Access GitHub on EC2

  • On the EC2 instance, type ssh-keygen.
  • Got to: GitHub » Settings » SSH and GPG keys » New SSH Key
  • Give it a title and copy the contents of ~/.ssh/id_rsa.pub in the EC2 into the Key box.
  • To check the success, type ssh [email protected] on the EC2.
@nilforooshan
nilforooshan / get_assign.md
Last active October 24, 2024 11:14
R: get & assign

get & assign

This simple example illustrates the usage of get and assign commands in R.

test1 = 1:10
test2 = 10:1
for(i in 1:2)
{
 tmp = get(paste0("test", i))
@nilforooshan
nilforooshan / ipynb2html.md
Created October 2, 2024 03:10
misc: Export a Jupyter notebook to HTML

Export a Jupyter notebook to HTML

jupyter nbconvert --to html notebook.ipynb

Source

@nilforooshan
nilforooshan / install_pip.md
Last active September 26, 2024 21:10
py: Install PIP on Ubuntu

Install pip on Ubuntu

sudo apt install python3-pip -y
@nilforooshan
nilforooshan / select_col_data_table.md
Created August 31, 2024 06:14
R: Various ways to select multiple columns in data.table

Various ways of selecting multiple columns in a data.table

An example data.table:

DT = data.table(
    ID = c("b","b","b","a","a","c"),
    a = 1:6,
    b = 7:12,
 c = 13:18
@nilforooshan
nilforooshan / NA20.md
Created August 28, 2024 04:34
R: Replace NA with 0 in a range of columns

Replace NA with 0 in a range of columns

In this example, NA is replaced with 0 in the last two columns.

ped
#   id sire dam
# 1  1    6  11
# 2  2   NA  12
# 3 3 8 NA
@nilforooshan
nilforooshan / renamecol_x_y.md
Created August 28, 2024 04:15
R: Renaming columns with their name ending with a pattern

Renaming columns with their name ending with a pattern

Merging two data frames sharing columns with the same name, those columns will receive .x and .y suffixes for the first and the second merged data frame. Follow this example to renew those columns.

(test <- data.frame(a = 1:5, b.x = 6:10, b.y = 11:15, c.x = 16:20, c.y = 21:25))
@nilforooshan
nilforooshan / remove0s.md
Last active September 26, 2024 22:26
R: Remove leading 0s in an array of strings

Remove leading 0s in an array of strings

id <- c("00001","00010","00022","07432")
as.character(as.integer(id))
[1] "1"    "10"   "22"   "7432"
@nilforooshan
nilforooshan / ipynb2rmd.md
Last active September 26, 2024 22:27
R: Export an R Jupyter notebook to RMD

Export an R Jupyter notebook to RMD

filename <- "FILENAME"
fileConn <- file(paste0(filename, '.rmd'))
writeLines(xfun::file_string(rmarkdown:::convert_ipynb(paste0(filename, '.ipynb'))), fileConn)
close(fileConn)
@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