Ctrl+“+” 放大窗口
Ctrl+“-” 缩小窗口
向量 创建向量 1 2 3 4 a<- c ( 1 , 2 , 3 ) ( b<- c ( "one" , "two" , "three" ) ) ( c <- c ( TRUE , FALSE ) ) a<- vector( )
vector() 产生一个指定模式和长度的向量
mode = “logical” 设置指定模式
length = 0 设置向量长度
使用向量 定位向量中某个位置的值
1 2 3 4 5 a<- ( 1 , 2 , 3 , 4 , 5 , 6 ) a[ 1 ] a[ 6 ] a[ - 1 ] a[ - c ( 1 : 3 , 6 ) ]
我们可以发现向量值的下标是从1开始的!
我们可以使用:
来生成一个序列
1 2 3 > v<- c ( 1 : 5 , 10 : 15 ) > v [ 1 ] 1 2 3 4 5 10 11 12 13 14 15
向量类型
使用class()
或mode()
函数可以查询向量的类型
class()与mode()是有区别的!
1 2 3 4 5 6 a<- c ( 1 , 2 , 3 ) ( b<- c ( "one" , "two" , "three" ) ) ( c <- c ( TRUE , FALSE ) ) mode( a) class ( b) class ( c )
1 2 3 4 5 6 > mode( a) [ 1 ] "numeric" > class ( b) [ 1 ] "character" > class ( c ) [ 1 ] "logical"
生成数值型向量的符号和函数 seq()
可生成等差序列
1 2 3 4 5 6 7 8 9 10 11 12 13 seq( from = 1 , to = 5 , by = 0.5 ) seq( from = 10 , to = 1 , by = - 1 ) seq( 1 , 5 , length.out = 8 ) seq( 1 , 5 , len = 10 ) seq( 1 , 5 , along.with = 1 : 3 ) seq( 1 , 5 , along.with = 2 : 5 )
rep()
用于生成重复值
1 2 3 4 5 6 7 8 rep ( c ( 1 , 3 ) , times = 5 ) rep ( c ( 1 , 3 ) , each = 5 ) rep ( c ( 1 , 3 ) , len = 9 ) vector4 <- rep ( c ( 1 , 2 ) , times = 4 ) length ( vector4)
逻辑型向量 数值运算 1 2 3 4 5 > logit <- rep ( c ( TRUE , FALSE ) , len = 5 ) > logit[ 1 ] TRUE FALSE TRUE FALSE TRUE > sum ( logit) [ 1 ] 3
我们可以知道,在进行数值计算时,TRUE=1,FALSE=0
逻辑表达式 1 2 3 4 5 6 7 8 9 比较符号: > ,大于< ,小于== ,因为= 可以用来赋值,所以不能用来进行逻辑判断!= ,不等于 >= ,大于等于<= ,小于等于& ,与|| ,或
1 2 3 > logit2 <- c ( 100 != 99 , 100 >= 102 , 1 > 2 || 1 < 3 , 5 == 5 & 7 >= 8 ) > logit2[ 1 ] TRUE FALSE TRUE FALSE
字符串向量 类型转换 如果向量中既有数值型数据又有字符串型数据,则数值型数据被强制转换为字符型数据
1 2 3 4 string <- c ( "abc" , "def" , 1 , 2 ) > string <- c ( "abc" , "def" , 1 , 2 ) > class ( string[ 3 ] ) [ 1 ] "character" class ( string[ 3 ] )
R语言内置向量 letters
它表示a到z的26个字母,且为小写
LETTERS
它表示A到Z的26个字母,且为大写
1 2 3 4 5 6 7 8 > letters [ 1 ] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" [ 13 ] "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" [ 25 ] "y" "z" > LETTERS [ 1 ] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" [ 13 ] "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" [ 25 ] "Y" "Z"
factor()
用于生成因子型向量
1 2 factor( x = character( ) , levels, labels = levels, exclude = NA , ordered = is.ordered( x) , nmax = NA )
参数说明
x:向量。
levels:指定各水平值, 不指定时由x的不同值来求得。
labels:水平的标签, 不指定时用各水平值的对应字符串。
exclude:排除的字符。
ordered:逻辑值,用于指定水平是否有序。
nmax:水平的上限数量。
gl()
用于生成因子水平
1 gl( n, k, length = n* k, labels = seq_len ( n) , ordered = FALSE )
参数说明
n : 设置 level 的个数
k : 设置每个 level 重复的次数
length : 设置长度
labels : 设置 level 的值
ordered : 设置是否 level 是排列好顺序的,布尔值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 > my_fac4 <- gl( n= 2 , k= 5 , labels = c ( "Control" , "Treatment" ) ) > my_fac4 [ 1 ] Control Control Control Control Control Treatment [ 7 ] Treatment Treatment Treatment Treatment Levels: Control Treatment > my_fac5 <- gl( n= 2 , k= 1 , labels = c ( "Control" , "Treatment" ) ) > my_fac5[ 1 ] Control TreatmentLevels: Control Treatment > my_fac5 <- gl( n= 2 , k= 1 , length = 8 , labels = c ( "Control" , "Treatment" ) ) > my_fac5[ 1 ] Control Treatment Control Treatment Control Treatment[ 7 ] Control TreatmentLevels: Control Treatment
可以省略n,k直接写个数与重复次数。
类型转换 as.factor()
字符串型->因子型
1 2 3 4 5 6 7 > temp_string <- c ( 'A' , 'B' , 'AB' , 'O' ) > my_fac6 <- as.factor( temp_string) > my_fac6[ 1 ] A B AB O Levels: A AB B O > class ( my_fac6) [ 1 ] "factor"
as.charactor()
因子型->字符串型
1 2 3 > my_fac6<- as.character ( my_fac6) > class ( my_fac6) [ 1 ] "character"
求因子水平个数和具体值 nlevels()
求level个数
1 2 > nlevels( my_fac6) [ 1 ] 4
levels
求具体值
1 2 > levels( my_fac6) [ 1 ] "A" "AB" "B" "O"
relevel()
生成哑变量
需要导入Epi包
1 2 3 4 > my_fac7 <- relevel( my_fac6, ref= 'B' ) > my_fac7[ 1 ] A B AB O Levels: B A AB O
ref:作为参考的值,效果是在levels中会出现在首位
改变有序变量的排序 1 2 3 4 5 > x <- c ( 'plocebo' , '20mg' , '10mg' , '50mg' ) > my_order_fac <- factor( x, ordered = TRUE ) > my_order_fac[ 1 ] 0 mg 10 mg 20 mg ploceboLevels: 0 mg < 10 mg < 20 mg < plocebo
plocebo:微量,应该是要在0mg前的,如何实现呢?
首先导入DescTools包
1 2 install.packages( "DescTools" ) library( DescTools)
1 2 3 4 5 x <- c ( 'plocebo' , '10mg' , '20mg' , '50mg' ) my_order_fac <- factor( x, ordered = TRUE ) library( DescTools) my_order_fac2 <- reorder.factor( my_order_fac, new.order = x) my_order_fac2
1 2 [ 1 ] plocebo 10 mg 20 mg 50 mg Levels: plocebo < 10 mg < 20 mg < 50 mg
如果我们调整一下
1 2 3 4 5 6 > x <- c ( '10mg' , 'plocebo' , '20mg' , '50mg' ) > my_order_fac <- factor( x, ordered = TRUE ) > my_order_fac2 <- reorder.factor( my_order_fac, new.order = x) > my_order_fac2[ 1 ] 10 mg plocebo 20 mg 50 mg Levels: 10 mg < plocebo < 20 mg < 50 mg
我们发现plocebo变化10mg后了!
总结一下就是,利用DescTools中的reorder.factor()
函数,在new.order
后指定我们需要的顺序,这样就改变了因子水平的顺序了!
数据框 定义数据框 data.frame()
类似excel表格
首先创建数据
1 2 3 4 patientID <- c(1 , 2 , 3 , 4 )age <- c(25 , 34 , 28 , 52 )diabetes <- c("Type1" , "Type2" , "Type1" , "Type1" )status <- c("Poor" , "Improved" , "Excellent" , "Poor" )
设置数据类型
1 2 diabetes <- factor( diabetes) status <- factor( status, order= TRUE )
定义数据框&输出
1 2 patientdata <- data.frame( patientID, age, diabetes, status) patientdata
1 2 3 4 5 6 > patientdata patientID age diabetes status 1 1 25 Type1 Poor2 2 34 Type2 Improved3 3 28 Type1 Excellent4 4 52 Type1 Poor
我们可以发现,我们定义的变量名作为了列名。
需要注意 定义的变量作为数据框时需要具备相同的维度!
输出数据框的结构
str()
1 2 3 4 5 6 > str( patientdata) 'data.frame' : 4 obs. of 4 variables: $ patientID: num 1 2 3 4 $ age : num 25 34 28 52 $ diabetes : Factor w/ 2 levels "Type1" , "Type2" : 1 2 1 1 $ status : Ord.factor w/ 3 levels "Excellent" < "Improved" < ..: 3 2 1 3
输出统计数据
1 2 3 4 5 6 7 8 > summary( patientdata) patientID age diabetes status Min. : 1.00 Min. : 25.00 Type1: 3 Excellent: 1 1 st Qu.: 1.75 1 st Qu.: 27.25 Type2: 1 Improved : 1 Median : 2.50 Median : 31.00 Poor : 2 Mean : 2.50 Mean : 34.75 3 rd Qu.: 3.25 3 rd Qu.: 38.50 Max. : 4.00 Max. : 52.00
以视图的形式输出 view()
去除某列 1 2 3 4 5 6 7 8 > my_df <- data.frame( one= c ( 1.2 , 2.3 , 3.4 , 4.5 , 5.6 ) , two = c ( 'Tom' , 'Andy' , 'Mary' , 'Bob' , 'Leo' ) , three = c ( TRUE , FALSE , TRUE , FALSE , TRUE ) , stringsAsFactors = TRUE ) > my_df one two three 1 1.2 Tom TRUE 2 2.3 Andy FALSE 3 3.4 Mary TRUE 4 4.5 Bob FALSE 5 5.6 Leo TRUE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 > my_df[ , - 2 ] one three 1 1.2 TRUE 2 2.3 FALSE 3 3.4 TRUE 4 4.5 FALSE 5 5.6 TRUE > my_df one two three 1 1.2 Tom TRUE 2 2.3 Andy FALSE 3 3.4 Mary TRUE 4 4.5 Bob FALSE 5 5.6 Leo TRUE
1 2 3 4 5 6 7 8 > my_df$ two <- NULL > my_df one three 1 1.2 TRUE 2 2.3 FALSE 3 3.4 TRUE 4 4.5 FALSE 5 5.6 TRUE
使用<-null
为第二列赋予null值,实现永久删除
新增一列
使用方法类似赋空值
1 2 my_df$ four <- LETTERS [ 1 : 5 ] my_df
使用可视化数据框进行修改 edit()或fix()
1 2 my_df <- edit( my_df) my_df
快速查看数据框中的数据元素 head()
tail()
加载psych包
可以提供最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻辑型向量的频数统计
describe()
计算非缺失值的数量、平均数、标准差、中位数、截尾均值、绝对中位差、最小值、最大值、值域、偏度、峰度和平均值的标准误
在局部环境中使用数据框的变量 table()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > patientdata[ 1 : 2 ] patientID age 1 1 25 2 2 34 3 3 28 4 4 52 > patientdata[ c ( "diabetes" , "status" ) ] diabetes status 1 Type1 Poor2 Type2 Improved3 Type1 Excellent4 Type1 Poor> patientdata$ age [ 1 ] 25 34 28 52 > > table( patientdata$ diabetes, patientdata$ status) Excellent Improved Poor Type1 1 0 2 Type2 0 1 0
用 table() 函数统计因子各水平的出现次数(称为频数或频率)。也可以对一般的向量统计每个不同元素的出现次数。
attach()、detach()和with() 使用内置数据
1 2 3 4 5 6 > View( mtcars) > summary( mtcars$ mpg) Min. 1 st Qu. Median Mean 3 rd Qu. Max. 10.40 15.43 19.20 20.09 22.80 33.90 > plot( mtcars$ mpg, mtcars$ disp) > plot( mtcars$ mpg, mtcars$ wt)
attach()&detach 1 2 3 4 5 attach( mtcars) summary( mpg) plot( mpg, disp) plot( mpg, wt) detach( mtcars)
如果环境变量
中有变量与使用的数据重名,则会优先使用环境变量中的数据
1 2 3 4 mpg <- c ( 25 , 36 , 47 ) attach( mtcars) plot( mpg, wt) mpg
with() 1 2 3 4 5 with( mtcars, { print( summary( mpg) ) plot( mpg, disp) plot( mpg, wt) } )
使用with()不会受重名变量的影响
在with()中产生的对象是局部变量,只在{}中有效
定义矩阵 matrix()
1 matrix( data = NA , nrow = 1 , ncol = 1 , byrow = FALSE , dimnames = list ( rownames, colnames) )
参数说明
data 向量,矩阵的数据
nrow 行数
ncol 列数
byrow 逻辑值,为 FALSE 按列排列,为 TRUE 按行排列
dimname 设置行和列的名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 > 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
定位数据 1 2 3 4 5 6 7 8 9 10 11 12 13 > 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 > x[ 1 , 4 ] [ 1 ] 7 > x[ 1 , c ( 4 , 5 ) ] [ 1 ] 7 9
中括号中省略某一边,代表整行或整列
可以在中括号中使用向量,同时选中多个行或列
1 2 3 4 5 6 7 8 > my_matrix2 <- matrix( data = 1 : 10 , nrow = 5 ) > my_matrix2 [ , 1 ] [ , 2 ] [ 1 , ] 1 6 [ 2 , ] 2 7 [ 3 , ] 3 8 [ 4 , ] 4 9 [ 5 , ] 5 10
默认情况下,按行排列
若未规定行数、列数的某一个的具体值,则会根据数据自行生成,但必须保证数据长度能够被已知行或列数整除,否则会报错。
如:
1 2 3 Warning message: In matrix( data = 1 : 9 , nrow = 5 ) : data length [ 9 ] is not a sub- multiple or multiple of the number of rows [ 5 ]
转置 t()
1 2 3 4 5 6 7 8 9 10 11 12 13 > my_matrix5 <- matrix( data = 1 : 12 , nrow = 3 , ncol = 4 , + dimnames = list ( c ( 'A' , 'B' , 'C' ) , c ( 'V1' , 'V2' , 'V3' , 'V4' ) ) ) > my_matrix5 V1 V2 V3 V4 A 1 4 7 10 B 2 5 8 11 C 3 6 9 12 > t( my_matrix5) A B C V1 1 2 3 V2 4 5 6 V3 7 8 9 V4 10 11 12
数组
定义数组 array()
1 array( data = NA , dim = length ( data) , dimnames = NULL )
参数说明
data 向量,数组元素。
dim 数组的维度,默认是一维数组。
dimnames 维度的名称,必须是个列表,默认情况下是不设置名称的。分别是:行、列、各个维度名
1 2 3 4 5 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 , , 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
维度信息 返回维度信息 dim()
1 2 3 > my_array <- array( data = 1 : 16 , dim = c ( 2 , 4 , 2 ) ) > dim ( my_array) [ 1 ] 2 4 2
重新设置维度 dim()<-c()
1 dim ( my_array) <- c ( 4 , 2 , 2 )
命名 1 my_array2 <- array( 1 : 16 , dim = c ( 4 , 2 , 2 ) , dimnames = list ( c ( LETTERS [ 1 : 4 ] ) , c ( 'col1' , 'col2' ) , c ( 'first' , 'second' ) ) )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 , , first col1 col2 A 1 5 B 2 6 C 3 7 D 4 8 , , second col1 col2 A 9 13 B 10 14 C 11 15 D 12 16
提取元素 1 2 3 4 5 6 7 > my_array2[ , 1 , 1 ] A B C D 1 2 3 4 > my_array2[ 2 , 2 , 1 ] 6
列表 定义列表 list()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 > 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"
list()中每一个逗号分割一个元素
可以每个元素定义新名字,默认为[[n]]
提取元素 第二个数组元素 1 2 > mylist$ ages[ 1 ] 25 26 18 39
这里我们直接使用了$+名字
引用了第二个数组元素
提取one元素 1 2 > mylist[[ 4 ] ] [[ 1 ] ] [ 1 ] "one"
若列表中嵌套了列表 查找“A”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > my_list3 <- list ( 1 : 10 , letters [ 1 : 5 ] , list ( 11 : 14 , LETTERS [ 1 : 5 ] ) ) > my_list3[[ 1 ] ] [ 1 ] 1 2 3 4 5 6 7 8 9 10 [[ 2 ] ] [ 1 ] "a" "b" "c" "d" "e" [[ 3 ] ] [[ 3 ] ] [[ 1 ] ] [ 1 ] 11 12 13 14 [[ 3 ] ] [[ 2 ] ] [ 1 ] "A" "B" "C" "D" "E" > my_list3[[ 3 ] ] [[ 2 ] ] [ 1 ] [ 1 ] "A"
用于管理工作空间的部分标准命令
getwd()
setwd(“mydirectory”)
ls()
rm(objectlist)
help(options)
options()
history(#)
savehistory(“myfile”)
保存命令历史到文件myfile中(默认值为.Rhistory)
loadhistory(“myfile”)
载入一个命令历史文件(默认值为.Rhistory)
save.image(“myfile”)
保存工作空间到文件myfile中(默认值为.RData)
save(objectlist, file=”myfile”)
load(“myfile”)
读取一个工作空间到当前会话中(默认值为.RData)
q()
判断数据类型,返回布尔值 1 2 3 4 5 is.integer ( ) is.character ( ) is.double ( ) is.numeric ( ) is.logical ( )
使用is.对象
还可以对很多东西进行判断。