R learning

Introduction of dpiyr

Bioconductor Manuals

Making genomics plots with circles, sushi, ggbio

Those packages are useful for making genome wide result plot.

Image analysis

EBImage and imager package

Those packages provide general purpose functions for image processing and analysis.

Running Structure-like Population Genetic Analyses with R

package: LEA -Manual

Save r function

When you write a R function, you want save it and use it when you need, you can save it with dump

for example

exam<-function(x)

{

return(x+2)

}

dump("exam","exam.R")

When you need it, you can use source to read in the function

source("exam.R")

Identify the number of row with special attribution in a matrix

#create a data

df <- data.frame(x = c(1,1,2,3,4,5,6,3),y=c(1,3,2,3,4,2,1,3))

# which row have the same data example 3

intersect(which(df$y==3),which(df$x==3))

# the number of row

length(intersect(which(df$y==3),which(df$x==3)))

Replace a number with another number in a dataframe

df<-data.frame("C1"=1:6,"C2"=3:8,"C3"=4:9)

rownames(df)=c("R1","R2","R3","R4","R5","R6")

df

C1 C2 C3

R1 1 3 4

R2 2 4 5

R3 3 5 6

R4 4 6 7

R5 5 7 8

R6 6 8 9

df[df == 3] <- 100

C1 C2 C3

R1 1 100 4

R2 2 4 5

R3 100 5 6

R4 4 6 7

R5 5 7 8

R6 6 8 9

Remove all special characters in a given string

x<-c("^FG$@!%^")

x=gsub("[[:punct:]]", "", x)

x

[1] "FG"

Find common elements from multiple vectors

X<-c("d","s","x","y","t","r","e","f","s","c","b","a")

Y<-c("d","s","g","d","s","t","e","g","s","g","f")

Z<-c("h","f","d","s","a","g","s","d","g","f")

intersect(intersect(Z,Y),Z)

or

Reduce(intersect,list(Z,Y,Z))

$ the number of common elements

length(intersect(intersect(Z,Y),Z))

or

length(Reduce(intersect,list(Z,Y,Z)))

Count the number of element in a list

x<-c("A","T","T","T")

grep("T",x) #give the position of the elements

[1] 2 3 4

length(grep("T",x)) # give the number

[1] 3

grepl("T",x)#returns a logical vector

[1] FALSE TRUE TRUE TRUE

# exact match with grep()

vect<-c("AB","A","BA","AB")

grep("A",vect)

[1] 1 2 3 4

grep("^A",vect)

[1] 1 2 4

grep("^A$",vect)

[1] 2

grep("A$",vect)

[1] 2 3