R 数据结构
小结
- 5种对象类型
character
,numeric
,integer
,complex
,logical
- 向量
- +2维度 - 矩阵
- +>维度 - 数组
- +不同类型 - 列表 - +元素长度相同 - 数据框 - data.matrix() - 矩阵
- 整数+标签 - 因子
- 日期/时间
对象基本类型
对象的5种基本类型(classes of objects)
- 字符(character)
- 数值(numeric: real numbers)
- 整数(integer)
- 复数(complex: 1+2i)
- 逻辑(logical: TRUE/FALSE)
> x <- 1
> class(x)
[1] "numeric"
> x <- 1L
> class(x)
[1] "integer"
> x <- "hello"
> class(x)
[1] "character"
> x <- 1 + 2i
> class(x)
[1] "complex"
> x <- TRUE
> class(x)
[1] "logical"
对象属性
- 属性(attribute)
- 名称(name)
- 维度(dimensions: matrix, array)
- 类型(class)
- 长度(length)
向量
- 向量(vector)
- 只能包含 同一类型 的对象
- 创建向量
.vector()
.c()
.as.logical()
/as.numeric()
/as.character()
> x <- vector("character", length =10)
> x
[1] "" "" "" "" "" "" "" "" "" ""
> x1 <- 1:4
> x1
[1] 1 2 3 4
> x2 <- c(1,2,4)
> x2
[1] 1 2 4
# 如果输入类型不一致,R会强制转换为一种类型
> x3 <- c(TRUE, 10, "a")
> x3
[1] "TRUE" "10" "a"
> x4 <- c("a", "b", "c")
> x4
[1] "a" "b" "c"
# 强制转换
> as.numeric(x4)
[1] NA NA NA
Warning message:
强制改变过程中产生了NA
as.logical(..)
as.character(..)
# 获取类型
> class(x1)
[1] "integer"
# 添加名称
names(x1) <- c("a", "b", "c", "d")
矩阵 和 数组
- 矩阵(
matrix
)- 向量+维度属性(整数向量:
nrow
,ncol
) - 创建矩阵
matrix()
: 先列后行vector()
+dim()
cbind()
,rbind()
attributes()
- 向量+维度属性(整数向量:
矩阵赋值
x <- matrix(nrow=3, nclo =2)
x <- matrix(1:6, nrow=3, nclo =2)
矩阵填充时,列优先
dim(x) # 查看矩阵 行列
查看矩阵属性
attributes(x)
y <- 1:6
dim(y) <- c(2,3)
矩阵拼接
y2 <- matrix(1:6, nrow=3, ncol =3)
# 行拼接
rbind(y,y2)
# 列拼接
cbind(y,y3)
- 数组(array)
- 与矩阵类型,但维度可以 大于2
- 创建数组
x <- array(1:24, dim=c(4,6))
x2 <- array(1:24, dim=c(2, 3, 4))
列表
- 列表(list)
- 可以包含 不同类型 对象
- 创建列表
list()
# list
> l <- list("a", 2, 10L, 3+4i, TRUE)
> l
[[1]]
[1] "a"
[[2]]
[1] 2
[[3]]
[1] 10
[[4]]
[1] 3+4i
[[5]]
[1] TRUE
# with name
> l2 <-list(a=1, b=2, c=3)
> l2
$a
[1] 1
$b
[1] 2
$c
[1] 3
> l3 <- list(c(1,2,3), c(4,5,6,7))
> l3
[[1]]
[1] 1 2 3
[[2]]
[1] 4 5 6 7
x <- matric(1:6, nrow=2, ncol=3)
dimnames(x) <- list(c("a", "b"), c("c", "d", "e"))
因子
- 因子(
factor
)- 分类 数据/ 有序 vs 无序
- 整数向量+标签(label)(优于整数向量) Male/Female vs. 1/2 常用于
lm()
,glm()
- 创建因子
factor()
table()
/unclass()
x <- factor(c("female", "female", "male", "male", "female"))
y <- factor(c("female", "female", "male", "male", "female"), levels = c("male", "female"))
通过levels 设置factor中基线水平
table(x)
去掉factor属性
unclass(x)
class(unclass(x))
缺失值
- 缺失值(missing value)
- NA/NaN: NaN属于NA, NA不属于NaN NaN一般表示数字的缺失
- NA有类型属性: integer NA, character NA
is.na()
/is.nan()
> x <- c(1, NA, 2, NA, 3)
> is.na(x)
[1] FALSE TRUE FALSE TRUE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE
> x <- c(1, NaN, 2, NaN, 3)
> is.na(x)
[1] FALSE TRUE FALSE TRUE FALSE
> is.nan(x)
[1] FALSE TRUE FALSE TRUE FALSE
数据框
- 数据框(data frame)
- 存储表格数据(tabular data)
- 视为各元素长度相同的 列表
- 每个元素代表一列数据
- 每个元素的长度代表行数
- 元素类型可以不同
> df <- data.frame(id = c(1,2,3,4), name = c('a', 'b', 'c', 'd'), gender = c(TRUE,TRUE, FALSE, FALSE))
> nrow(df)
[1] 4
> ncol(df)
[1] 3
> df2 <- data.frame(id= c(1,2,3,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
日期与时间
- 日期与时间(date, time)
- 日期: Date
- 距离1970-01-01的天数/
date()
/Sys.Date()
weekdays()
/months()
/quarters()
- 距离1970-01-01的天数/
- 时间:
POSIXct
/POSIXlt
- 距离1970-01-01的秒数/Sys.time()
POSIXct
:整数,常用于存入数据框POSIXlt
:列表,包含星期、年、月、日等。
- 字符 to 日期/时间
as.Date()
as.POSIXct()
/as.POSIXlt()
/strptime()
- 日期: Date
日期
> x <- date()
> x
[1] "Wed Dec 13 22:41:28 2017"
>
> class(x)
[1] "character"
>
> x2 <- Sys.Date()
> x2
[1] "2017-12-13"
>
> class(x2)
[1] "Date"
>
> x3 <- as.Date("2015-01-02")
> x3
[1] "2015-01-02"
>
> class(x3)
[1] "Date"
>
> weekdays(x3)
[1] "星期五"
> months(x3)
[1] "一月"
> quarters(x3)
[1] "Q1"
> julian(x3)
[1] 16437
attr(,"origin")
[1] "1970-01-01"
>
> x4 <- as.Date(("2016-01-01"))
>
> x4-x3
Time difference of 364 days
>
> as.numeric(x4-x3)
[1] 364
时间
> x <- Sys.time()
> x
[1] "2017-12-13 22:57:25 CST"
>
> class(x)
[1] "POSIXct" "POSIXt"
>
> p <- as.POSIXlt(x)
> p
[1] "2017-12-13 22:57:25 CST"
>
> class(p)
[1] "POSIXlt" "POSIXt"
>
> # unclass(p), 属性去掉,内容留下
> names(unclass(p))
[1] "sec" "min" "hour" "mday" "mon" "year"
[7] "wday" "yday" "isdst" "zone" "gmtoff"
>
> p$sec
[1] 25.32764
> as.POSIXct(p)
[1] "2017-12-13 22:57:25 CST"
>
> as.Date("2015-02-23")
[1] "2015-02-23"
>
> # 一月,不可是Jan, 按机器设置, current locale
> x1 <- "一月 1, 2015 01:01"
> strptime(x1, "%B %d, %Y %H:%M")
[1] "2015-01-01 01:01:00 CST"
> ?strptime