R语言入门学习笔记(2)

课程源代码:https://github.com/miwu8512/IntroToR

视频地址: https://www.youtube.com/watch?v=rPj5FsTRboE&list=PLBTcf4SwWEI9_kCOJ-1o-Jwr-_Qb6bkeg

Lecture 2 数据集的结构和作图的主要变量

1 understanding the dataset

1.1 Vector 向量

Define: Vectors are one-dimersional arrays that can hold nurmeric, character, logical data.
Function:

c()
a = c(1,2,-3,4)
b = c("one","two")
c = c(TRUE,TRUE,FALSE,TRUE)

1.2 Matrix 矩阵

Define: Matrix is a two-dimersional array where each element has th esame mode(nurmeric, character, or logical).
Function:

matrix() 

x = matrix (1:20,nrow=5,ncol=4,byrow=TRUE)

> x
      [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20

x[2,]   #返回第二行数据
x[,2]   #返回第二列数据
x[1,4]
x[2,c(2,4)]
x[3:5,2]
rnames=c("apple","banana","orange","melon","corn")
cnames=c("cat","dog","bird","pig")
rownames(x)=rnames
colnames(x)=cnames

1.3 Array 数组

Define: Arrays are similar to matrics but can have more than two dimensions.
Function:

array()

dim1 = c("A1","A2")
dim2 = c("B1","B2","B3")
dim3 = c("C1","C2","C3","C4")
dim4 = c("D1","D2","D3") 
z = array(1:72,c(2,3,4,3), dimnames=list(dim1,dim2,dim3,dim4))

1.4 data frame

Define: A data frame is more general than a matrix in that different columns can cantain different modes of data(numeric, character, etc.). It is similar to the datasets you would typically see in SAS, SPSS, and Stata.

1.5 attach and detach

1.6 list

Define: Lists are the most complex of the R data types. A list allows you to gather a variety of (possible unrelated) objects under one name.

2 Graphs 作图

2.1 Graphical parameters

Define: customize many features of a graph (fonts,colors,axes,titles) through options called graphical parameters.
Function:

par() 

par(mfrow=c(2,2))  #设定多图布局网格
plot(rnorm(50),pch=19)          #pch:point的形状
plot(rnorm(20),type="l",lty=5)  #lty:line type 线的形状
plot(rnorm(100),cex=0.5)        #cex:放大比例
plot(rnorm(200),lwd=2)          #lwd: line width 粗细变换

2.2 title, axes, legends

title()  #标题
axis()   #坐标轴
legend() #图例

2.3 layout 布局

Define: layout() function has the form layout(mat) where mat is a matrix object specifying the location of the multiple plots to combine.

> attach(mtcars)
> layout(matrix(c(1,1,2,3),2,2,byrow= TRUE)) 
> hist(wt)
> hist(mpg)
> hist(disp)
> detach(mtcars)

你可能感兴趣的:(R语言入门学习笔记(2))