Appendix V. R Basics

How to make professional and beautiful plots

Tips:

Do things with multiple ways

Efficient Writing: Bash > R > Perl/Python > Excel > C,C++

1. Install and Use R

a. Install R

See the official site of R: https://www.r-project.org/ (Docker中已经安装好)

Here is an example of installation in Linux:

  • For CentOS

    yum -y install epel-release
    yum -y install R
    
  • For Ubuntu

    apt -y install r-base
    

b. Install RStudio

If you like a nice interface to use R instead of Linux command line, you can try RStudio: https://www.rstudio.com/

We have some instruction of Rstudio in Appendix. Plot with R

c. Use R

  • how to enter?

    • On Mac or Linux, you can open a Terminal and type

      R
      
    • On Windows or Mac, you can find a program with this logo:

  • How to quit?

    q()

  • How to get help?

    ?t.test

2. Basic Data Types

> x = 10.5 # assign a decimal value
> x # print the value of x
[1] 10.5
> class(x) # print the class name of x
[1] "numeric"

> y = as.integer(3)
> y # print the value of y
[1] 3
> class(y) # print the class name of y
[1] "integer"

> z = 1 + 2i # create a complex number
> z # print the value of z
[1] 1+2i
> class(z) # print the class name of z
[1] "complex"

> x = 1; y = 2 # sample values
> z = x > y # is x larger than y?
> z # print the logical value
[1] FALSE
> class(z) # print the class name of z
[1] "logical"

> x = as.character(10.5)
> x # print the character string
[1] "10.5"
> class(x) # print the class name of x
[1] "character"
  • vector
> c(2, 3, 5)
[1] 2 3 5

> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> c(n, s)
[1] "2" "3" "5" "aa" "bb" "cc" "dd" "ee"

> s = c("aa", "bb", "cc", "dd", "ee")
> s[3]
[1] "cc"
  • matrix
> B = matrix(
+ c(2, 4, 3, 1, 5, 7),
+ nrow=3,
+ ncol=2)

> B # B has 3 rows and 2 columns
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
  • list
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
> x = list(n, s, b, 3) # x contains copies of n, s, b
  • data frame
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc")
> b = c(TRUE, FALSE, TRUE)
> df = data.frame(n, s, b) # df is a data frame
> df
n s b
1 2 aa TRUE
2 3 bb FALSE
3 5 cc TRUE

3. Examples

A simple example of t test:

  • start R and type:
x=c(5.6,7.9,8.9,19.5,20.5,39.5)
y=c(6.5,8.3,9.1,17.9,29.4,22.8)
write.table(x, file = "foo1")
write.table(y, file = "foo2")

a<-read.table("foo1") # read the file
b<-read.table("foo2") # read the file

t.test(a,b,alternative=c("less") )
t.test(a,b,altrenative=c("less"),paired=TRUE) # paired t test
  • You can also write these into a file, script.R, then run

    R CMD BATCH script.R

More R Examples:

4. More Readings and Practices

Basic:

  • << Bioinformatics Data Skills >>
    • A Rapid Introduction to the R Language
  • Quick R 可以从如下章节开始 :
    • Learning R
    • R Interface
    • Data Input
    • Statistics

Advanced:

results matching ""

    No results matching ""