R语言数据类型使用技巧
一、R语言数据类型(变量)
1、标量(Scalar) character,integer,logical,double等
2、向量(Vector)
3、因子(Factor)
4、矩阵(Matrix)
5、数组(Array)
6、列表(List)
7、数据框(Data frame)
x <- "hello world" #变量赋值
二、一些基本的常用函数
1)连接字符串 paste() 函数
a <- "hello"
b <- "how are"
b <- "you"
paste(a,b,c) #输出结果为 "hello how are you"
paste(a,b,c,sep = "-") #这里指定连接符为- ,输出结果为 "hello-how are-you"
paste(a,b,c,sep ="") #输出结果为 "hellohow areyou"
2)计算字符串的字符数 nchar() 函数
result <- nchar("A B C D")
result #输出结果为 7
3)toupper() 和 tolower() 函数的基本语法是
a <- toupper(" A B c d")
a #输出结果为 " A B C D"
b <- tolower(" A B c d")
b #输出结果为 " a b c d"
4)截取字符串的一部分 substring() 函数
cut <- substring("hello world",1,5)
cut #输出结果为 "hello"
三、变量
a <- c(1,2,3,4) #标量
b <- c("one","two","three","four") #字符串
c <- c(TURE,TURE,FALSE) #逻辑变量
d <- 1:100 #产生连续的数字
a <- c(1,2,3,4,"one")
a #输出结果为 "1" "2" "3" "4" "one"
如果其中一个元素是字符,则非字符值则被强制转换为字符类型
rep(1:4,each=2) #输出结果为 1 1 2 2 3 3 4 4
seq(from=22,to=50,by=3) #输出结果为 22 25 28 31 34 37 40 43 46 49
seq(22,50,3) #输出结果为 22 25 28 31 34 37 40 43 46 49
四、向量的操作
1、索引截取
b <- c("one","two","three","four")
b[1] #输出结果为 "one"
b[1:3] #截取连续的 输出结果为 "one" "two" "three"
b[c(1,3)] #输出结果为 "one" "three"
2、逻辑筛选
a <- c(1,3,5,6,7,1,3)
b <- a[a>3]
b #输出结果为 5 6 7
letters #输出结果为 "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
letters[1:5] #输出结果为 "a" "b" "c" "d" "e"
3、向量运算
#可以添加,减去,相乘或相除两个相同长度的向量,将结果作为向量输出
v1 <- c(1,3,4,5,3,2)
v2 <- c(1,3,4,4,2,5)
add.result <- v1+v2
add.result #输出结果为 2 6 8 9 5 7
sub.result <- v1-v2
sub.result #输出结果为 0 0 0 1 1 -3
multi.result <- v1*v2
multi.result #输出结果为 1 9 16 20 6 10
divi.result <- v1/v2
divi.result #输出结果为 1.00 1.00 1.00 1.25 1.50 0.40
#向量循环利用
#如果我们对不等长的两个向量应用算术运算,则较短向量的元素被循环以完成操作
v1 <- c(2,1,3,4,5,1,6)
v1 <- c(1,2)
add.result <- v1+v2
add.result #输出结果为 2 5 5 6 3 7
五、因子(Factor),因子的用处是给数据分组
diabetes=c("T1","T2","T3","T4")
diabetes #输出结果为 "T1" "T2" "T3" "T4"
diabetes=factor(diabetes)
diabetes #输出结果为 T1 T2 T3 T4
Levels: T1 T2 T3 T4
#有序因子
status=factor(status,order=TRUE,levels=c("one","three","two"))
status #输出结果为 one two three one three
Levels: one < three < two
nlevels(status) #输出结果为 3
levels(status) #输出结果为 "one" "three" "two"
六、矩阵(Matrix),函数matrix()用于创建一个矩阵,矩阵数据类型只能是一种
y = matrix(1:12,nrow=3,ncol=4)
y #输出结果为 [,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
y = matrix(1:12,nrow=3,ncol=4,byrow=TRUE)
y #输出结果为 [,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
七、数组 数组和矩阵类似
z <- array(1:24,c(3,4,2))
z #输出结果为 , , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
八、数据框,重点
数据框与矩阵类似,也是二维的,与矩阵不同的是,数据框中各列的数据类型可以不同,但是长度必须相同
1、数据框的创建
patientID <- c(1,2,3,4)
age <- c(22,33,44,45)
diabetes <- c("T1","T2","T3","T4")
status = c("poor","excellent","improved","poor")
status = factor(status,order=TRUE,levels=c("poor","improved","excellent"))
patientData <- data.frame(patientID,age,diabetes,status)
patientData
#输出结果
patientID age diabetes status
1 1 22 T1 poor
2 2 33 T2 excellent
3 3 44 T3 improved
4 4 45 T4 poor
2、数据框常用筛选方法,以mtcars为列,mtcars是R中自带的内置数据框
mtcats
# mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
head(mtcars)
tail(mtcars)
colnames(mtcars)
#输出结果 "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
rownames(mtcars)
#输出结果 [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
nrow(mtcars) #输出结果为 32
ncol(mtcars) #输出结果为 11
row.names(mtcars)
#[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
dim(mtcars) #输出结果为 32 11
#rbind 按行合并数据框
#cbind 按列合并数据框
3、行索引提取
mtcars[1:2,] #提取第1,2行
mtcars[,1:2] #提取第1,2列
不连续
mtcars[c(2,4),c(3,6,9)] #提取第2,4行的第3,6,9列
通过列名称提取
matcars[,c("mpg","hp")]
mtcars $hp #利用$符来取一列数据,返回的是一个向量
mtcars[,c("mpg")] #返回一列是一个向量,但是我们想要返回数据框,通过加上一个drop=FALSE处理
mtcars[,c("mpg"),drop=FALSE]
4、筛选,产生逻辑向量进行筛选
interestcol <- colnames(mtcars)%in%c("hp","wt")
#interestcol #输出结果为 FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
mtcars[, interestcol] #将interestcol中是TRUE的列筛选出来
mtcars $hp>100
mtcars[mtcars$hp>100,] #将hp列大于100的行取出来
mtcars$hp>100 & mtcars$cy1>6
mtcars[mtcars $hp>100 & mtcars $cyl>6,]
subset方法
subset(mtcars,hp>100 & cyl>6)
数据排序,order产生一个排序后的索引
orderIDdex <- order(mtcars[,1]) #获取mtcars的第一列的索引赋给orderID
orderIDdex #输出结果 15 16 24 7 17 31 14 23 22 29 12 13 11 6 5 10 25 30 1 2 4 32 21 3 9 8 27 26 19 28 18 20
mtcars[orderIDdex,] #按第一列从小到大输出mtcars
orderIDdex <- order(mtcars[,1],decreasing=TRUE)
mtcars[orderIDdex,] #按第一列从大到小输出mtcars
九、list
a <- c(1,4,5,6,8)
b <- matrix(1:12,nrow=2,ncol=6)
c <- mtcars
mylist <- list(a,b,c)
mylist
names(mylist)
mylist[1] #输出结果为 1 4 5 6 8
mylist[["a"]]