一、R基本操作
1、创建一个名为x的变量,它包含5个来自标准正态分布的随机误差;
eg、x<-norm(5)
2、赋值
age<-c(1,3,5,2,11,9,3,9,12,3)
3、求平均数
mean(weight)
4、求标准差
sd(weight)
5、求相关系数
cor(age,weight)
6、绘制曲线
plot(age,weight)
7、查看当前目录
> getwd()
[1] "C:/Users/yangxuejun/Documents"
8、创建并设定当前工作目录
dir.create("E:/Raction")
> setwd("E:/Raction")
9、列出当前工作空间中对象
> ls()
[1] "age" "weight"
二、创建数据集
数据集通常是有数据构成的矩形数组,行表示观测,列表式变量
2.1、R数据结构
1、 向量
同一向量中的数据必须具有相同模式
1)组合功能函数c()用来创建向量
a<-c(1,2,3,4,5,6)
2) 访问元素
> a<-c(1,2,3,4,5)
> a[3]
[1] 3
> a[c(1,3,4)]
[1] 1 3 4
> a[2:6]
[1] 2 3 4 5 NA
2、矩阵
通过函数matrix来创建矩阵
1) 创建矩阵
> y<-matrix(1:20,nrow=5,ncol=4)
> y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> cells<-c(1,26,24,68)
> rnames<-c("R1","R2")
> cnames<-c("C1","C2")
> mymatrix<-matrix(cells,nrow=2,ncol=2,byrow=TRUE,dimnames=list(rnames,cnames)) //创建并按行填充矩阵
> mymatrix
C1 C2
R1 1 26
R2 24 68
>mymatrix<matrix(cells,nrow=2,ncol=2,byrow=FALSE,dimnames=list(rnames,cnames)) //创建并按列填充矩阵
> mymatrix
C1 C2
R1 1 24
R2 26 68
2) 矩阵下标
x<-matrix(1:10,nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
3 数组
1) 创建数组—通过array()函数创建
> dim1<-c("A1","A2")
> dim2<-c("B1","B2","B3")
> dim3<-c("C1","C2","C3","C4")
> z<-array(1:24,c(2,3,4),dimnames=list(dim1,dim2,dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24
2) 访问元素
> z[1,2,3]
[1] 15
4 数据框 —- 数据具有多种模式,无法将数据集放入矩阵中,这种情况下数据框为最佳选择
使用data.frame()来创建
1) 创建数据框
> patientID<-c(1,2,3,4)
> age<-c(25,34,28,52)
> diabetes<-c("T1","T2","T2","T1")
> status<-c("Poor","Improved","Excellent","Poor")
> patientdata<-data.frame(patientID,age,diabetes,status)
> patientdata
patientID age diabetes status
1 1 25 T1 Poor
2 2 34 T2 Improved
3 3 28 T2 Excellent
4 4 52 T1 Poor
2)选取数据框中的元素
> patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52
> patientdata[c("diabetes","status")]
diabetes status
1 T1 Poor
2 T2 Improved
3 T2 Excellent
4 T1 Poor
$选取给定数据框中的某个特定变量
table(patientdata$diabetes,patientdata$status)
Excellent Improved Poor
T1 0 0 2
T2 1 1 0
3) attach()—-将数据框添加到R的搜索路径中
detach()—-将数据从R的搜索路径中移除
with()
attach(patientdata)
The following objects are masked _by_ .GlobalEnv:
age, diabetes, patientID, status
> summary(age)
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.00 27.25 31.00 34.75 38.50 52.00
> detach(patientdata)
> with(patientdata,{summary(age)})
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.00 27.25 31.00 34.75 38.50 52.00
使用特殊赋值符<<-创建在with()结构以外存在的对象
5 因子
变量可归结为名义型、有序型、连续型变量;
类别变量(名义型)和有序类别(有序型)变量在R中称为因子
> diabetes<-c("T1","T2","T3","T4")
> status<-c("Poor","Improved","Excellent","Poor")
> diabetes<-factor(diabetes)
> status<-factor(status,order=TRUE)
6 列表—-使用函数list()创建列表
> g<- "my first list"
> h<-c(25,26,18,39)
> j<-matrix(1:10,nrow=5)
> k<-c("one","two","three")
> mylist<-list(title=g,ages=h,j,k)
> mylist
$title
[1] "my first list"
$ages
[1] 25 26 18 39
[[3]]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
[[4]]
[1] "one" "two" "three"
> mylist[[2]]
[1] 25 26 18 39