我是刚刚学习R语言,学习得目的是要找一份数据分析的工作,虽然现在离这个目标很遥远,但坚信我会成功的多则一年半,少则3个月,在知乎上看到有人说学习R一定要在论坛里分享,找到志同道合的伙伴,那样学起来才有意思,学的也会更快和快乐。我的qq864748901,有一起学习得伙伴加我的qq,不过qq不经常用哈,不过没有关系可以扫一下下边的二维码加我的微信。感谢论坛给我这个机会,还有那位大牛觉得我是可塑之才,也可以指点我呀,哈哈,他日学成,必有所报,谢谢
- R语言有五种基本类型(classes of objects):
- 字符(character)
- 数值(numer : real numbers)
- 整数(integer)
- 复数(complex)
- 逻辑(logical : TRUE/FALSE)
> x <- 1 #x赋值1
> x #输出x
[1] 1 #[1]表示x的第一个元素
> class(x) #查看x的类型
[1] "numeric" #x是一个数值型变量,numeric可以使小数也可以是整数
> x <- 2L #加上L表示2为整数类型
> x #输出x
[1] 2
> class(x)
[1] "integer" #整数类型
> y <- "hello world" #字符串要加上“”
> class(y)
[1] "character" #字符型
向量(vector)
#Vector
x <- vector("character", length = 10) #使用vector函数生成类型为character的长度为10的空向量
x1 <- 1:4 #int类型
x2 <- c(1, 2, 3, 4) #num类型 使用c函数
x3 <- c(TRUE, 10, "a")
x4 <- c("a", "b", "c")
as.numeric(x4) #强制把x4转换成数值型向量
[1] NA NA NA
Warning message:
NAs introduced by coercion #因为R不知道怎么把“a”, “b”, “c”转换成数值型,所以用NA(缺失值)来处理
as.logical() #强制转换成逻辑型
as.character() #强制转换成字符型
> names(x1) <- c("a", "b", "c", "d") #给向量添加名称
> x1
a b c d
1 2 3 4
矩阵(matrix)
#matrix
> x <- matrix(nrow = 3, ncol = 2) #用matrix函数创建一个矩阵,nrow表示行,ncol表示列
> x
[,1] [,2]
[1,] NA NA
[2,] NA NA
[3,] NA NA
> x <- matrix(1:6, nrow = 3, ncol = 2) #1:6表示,加入参数,矩阵的内容
> x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> dim(x) #查看矩阵x的维度,多少行多少列
[1] 3 2 #x矩阵得维度为 3行2列
> attributes(x) #查看矩阵x都有什么属性
$dim #有维度的属性
[1] 3 2
> y <- 1:6 #矩阵就是由向量 +
> dim(y) <- c(2,3) #维度属性的
> y
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> y2 <- matrix(1:6, nrow = 2, ncol = 3)
> y2 #创建一个同样的矩阵,便于矩阵的拼接
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> rbind(y,y2) #按行对y,y2两个矩阵进行拼接
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 1 3 5
[4,] 2 4 6
> cbind(y,y2) #按列拼接
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 5 1 3 5
[2,] 2 4 6 2 4 6
数组(array) 不常用,了解一下
#array
> x <- array(1:24, dim = c(4,6)) #dim表示设置维度
> x #x是一个4行6列的数组,也是一个4行6列的矩阵
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 5 9 13 17 21
[2,] 2 6 10 14 18 22
[3,] 3 7 11 15 19 23
[4,] 4 8 12 16 20 24
> x1 <- array(1:24, dim = c(2, 3, 4)) #创建一个三位数组
> x1
, , 1 #第三个维度里第一个元素,从后往前直到遇到“矩阵”
[,1] [,2] [,3] #在第一个元素里边是一个2行3列的矩阵
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
列表(list)
#list
> l <- list("a", 2, 10L, 3+4i, TRUE) #使用list函数创建一个列表
> l #列
[[1]] #表
[1] "a" #的
#样
[[2]] #式
[1] 2
[[3]]
[1] 10
[[4]]
[1] 3+4i
[[5]]
[1] TRUE
> l2 <- list(a = 1, b = 2, c = 3) #给列表l2中的对象命名
> l2
$a
[1] 1
$b
[1] 2
$c
[1] 3
#创建一个每个元素包含的元素个数大于1的列表
> l3 <- list(a = c(1, 2, 3), b = c(4, 5, 6, 7))
> l3
$a
[1] 1 2 3
$b
[1] 4 5 6 7
> x <- matrix(1:6, nrow = 2, ncol = 3) #创建一个数组
> dimnames(x) <- list(c("a", "b"), c("c", "d", "e"))
> x #用dimnames函数和一个列表给数组x得维度命名
c d e
a 1 3 5
b 2 4 6
因子(factor)
- 用来处理分类数据, 分类数据分为有序和无序
- 有序数据,比如年级,低中高,还有年龄
- 无序数据,比如性别,无法比大小
- 可以把因子理解成一个整数型向量+一个标签(label)(优于整数向量)
#factor
> f <- factor(c("female", "female", "male", "male", "female"))
> f
[1] female female male male female
Levels: female male #Levels表示水平
> f2 <- factor(c("female", "female", "male", "male", "female"), levels = c("male", "female")) #通过levels设置那个因子是基线水平
> f2
[1] female female male male female
Levels: male female #谁在前谁就是基线水平,对以后统计分析非常重要
> table(f) #对因子进行整体性的了解
f
female male
3 2
> unclass(f) #去掉levels属性查看因子
[1] 1 1 2 2 1
attr(,"levels")
[1] "female" "male"
> class(unclass(f)) #去掉levels后的类型为int
[1] "integer"
数据框 (data frame)数据分析经常用到
- 经常存储表格数据(tabular data)
- 和列表,矩阵的关系非常密切
- 视为各元素长度相同的列表
- 每个元素代表每一列数据
- 每个元素的长度代表行数
- 元素类型可以不同
#data frame
> df <- data.frame(id = c(1:4), name = c("chenhong", "qipengge", "yuxin", "zhoamiao"),gender = c(TRUE, FALSE, TRUE, FALSE))
> df #按列排布
id name gender
1 1 chenhong TRUE
2 2 qipengge FALSE
3 3 yuxin TRUE
4 4 zhoamiao FALSE
> nrow(df) #查看数据框df的行数
[1] 4
> ncol(df) #查看数据框df的列数
[1] 3
> df2 <- data.frame(id = c(1:4), score = c(80, 86, 90, 100))
> df2
id score
1 1 80
2 2 86
3 3 90
4 4 100
> data.matrix(df2) #转换成矩阵
id score
[1,] 1 80 #每个元素类型都一样,符合矩阵的要求
[2,] 2 86
[3,] 3 90
[4,] 4 100
未完待续