Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
nilforooshan / Ndecimals.md
Last active October 2, 2024 12:04
R: Return the number of decimals

Return the number of decimals

nchar(strsplit(as.character(5.230000), "\\.")[[1]][2])

[1] 2

nchar(strsplit(as.character(5.230200), "\\.")[[1]][2])
@nilforooshan
nilforooshan / sum_per_ID.md
Last active October 2, 2024 22:11
R: An aggregate example

An aggregate example

(data1 <- data.frame(id=c(rep(1,3),rep(2,3)),num=1:6))
  id num
1  1   1
2 1 2
@nilforooshan
nilforooshan / inactivsate_Rblock.md
Last active October 2, 2024 22:22
R: Inactivate a code block

Inactivate a code block

if(FALSE) {
    print('Hi')
    print('Bye')
}
@nilforooshan
nilforooshan / drop_select_cols.md
Last active April 5, 2024 08:23
R: Drop/select columns

Drop/select columns

A sample data:

(df <- data.frame(x=1:5, y=2:6, z=3:7, u=4:8))
 x y z u
@nilforooshan
nilforooshan / print_unicode.md
Last active March 30, 2019 09:06
R: Print unicode character strings

Print unicode character strings

a <- "\U00B5"
cat(a)

µ

@nilforooshan
nilforooshan / remove_after_occurrence_string.md
Last active March 30, 2019 09:06
R: Remove part of strings after occurrence of a character

Remove part of strings after occurrence of a character

Remove part of strings after “.”

a <- c("NM_020506.1", "NM_020519.1", "NM_001030297.2", "NM_010281.2", "NM_011419.3", "NM_053155.2")
gsub("\\..*","",a)

[1] "NM_020506" "NM_020519" "NM_001030297" "NM_010281" "NM_011419" "NM_053155"

@nilforooshan
nilforooshan / match_example.md
Last active March 30, 2019 09:05
R: match example

match example

A sample data:

(target <- c("b", "c", "a", "d"))
(df <- data.frame(name=letters[1:4], value=c(rep(TRUE, 2), rep(FALSE, 2))))
@nilforooshan
nilforooshan / list_files_pattern.md
Last active March 30, 2019 09:03
R: List files in a directory and subdirectories

List files in a directory and subdirectories

List files matching the pattern in all subdirectories

list.files(pattern = "_input.txt$", recursive = TRUE)
@nilforooshan
nilforooshan / rm_examples.md
Last active March 30, 2019 09:02
R: rm examples

rm examples

Delete all objects

rm(list=ls())

Delete data

@nilforooshan
nilforooshan / Count_NA_rowwise.md
Last active September 26, 2024 23:37
R: Count missing values row-wise

Count missing values row-wise

A sample data:

(data = data.frame(x=c(NA,NA,3:10), y=11:20, z=c(21,NA,23:30)))
 x y z