BMS8110复习(二):Lecture 2 - Programming in R

R is free and powerful programming language for statistical computing and data visualization.

1. R can be used to compute a large variety of classical statistic tests including:

  • Student's t-test: comparing the means of two groups of samples
  • Wilcoxon test: a non-parametric alternative of t-test
  • Analysis of variance (ANOVA): comparing the means off more than two  groups
  • Chi-square test: comparing proportions/distributions
  • Correlation analysis: is for evaluating the relationship between two or more variables

2. It's also possible to use R for performing classfication analysis such as:

  • Principal component analysis
  • clustering

3. Many types of graphs can be drawn using R, including: box plot, histogram, density curve, scatter plot, line plot, bar plot,...

Install R && R Studio

R grammar

Function and Package

R has five basic or "atomic" classes of objects: character, numeric(real number), integer, logical(TRUE/FALSE)

Operational Symbol

Variable

Basic Data Types in R: Vector, List, Matrix, Data frame, Factor

Control Structures

Common mistakes in using R:

  • Case sensitive
  • Forget the necessary quotes 
  • Forget to use parentheses when calling a function
  • Mix the usage of various sign
  • Need to discriminate the signs in English and Chinese, such as quote and comma

Statistic Functions: max(), min(), mean(), median(), sum(), sd(), summary()

# assignment operators:
a <- 6 ## recommend 推荐这种赋值方式
print(a)

b = 5 
print(5)

1. Variable2=c("learn","R") 

c本身在这里应该是“combine”的首字母,用于合并一系列数字从而形成向量/数列。

2. M = matrix(c('a','a','b','c','b','a'),nrow = 2, ncol = 3, byrow = FALSE)

byrow     logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.

3. factor_apple <- factor(apple) 

因子就是用于表示一组数据中的类别,可以记录这组数据中的类别名称及类别数目。

4. R语言命令行敲代码换行:shift+enter

5. NOLC1[-5]  ## print NOLC1 except for the 5th element

6. NOLC1[-(1:5)] ## print NOLC1 except for the first 5 elements

7. union(x, y) ## return the union of x and y(elements either in x or in y)
intersect(x, y)  ## return the intersect of x and y(elements both in x and in y)
setdiff(x, y)   ## return the elements only belongs to x while not belongs to y
identical(x, y)  ## test objects for strict equality

8. 

ct <- data.frame(
  treatment,
  GAPDH,
  NOLC1,
  EFN2B
)
ctValues <- ct[,c("GAPDH","NOLC1","EFN2B")]  ##select three colomns of ct
dim(ctValues)   ## return the dimension of ct
ncol(ctValues)   ## return the number of columns of ct
nrow(ctValues)   ## return the number of rows of ct
t(ctValues)   ## transpose ct
rowSums(ctValues)  ## calculate the sum of each row
rowMeans(ctValues)  ## calculate the mean of each row
colSums(ctValues)  ## calculate the sum of each column
colMeans(ctValues)   ## calculate the mean of each column

9. 画图细节还可以再仔细琢磨一下,最好是自己分析的数据来画一组图。

发布了273 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wxw060709/article/details/103268931
今日推荐