R的数据结构

数据结构

 

查看数据类型 class(x)

对象的5种基本类型:

字符——character

数值——numeric:real numbers

整数——integer x <- 5L

复数——complex y <- 1+2i

逻辑——logical TRUE/FALSE 

 

属性:

——名称(name)

——维度(dimensions:matrix, array)

——类型(class)

——长度(length)

 

向量:#Vector

x1 <- vector("character", length=10)

x2 <- 1:4

x3 <-  c(1, 2, 3, 4)

x4 <- c(TRUE, "a", 10)

class(x4)

x5 <- c("a", "b", "c")

as.numeric(x5) 类型转换(强制)

as.logical()

as.character()

class(x5)

names(x2) <- c("a", "b", "c", "d")

x2

 

矩阵 & 数组

#Matrix & Array

矩阵

—— 向量 + 维度属性(整数向量:nrow, ncol)

#Matrix
x <- matrix(1:6, nrow = 3, ncol = 2)
x
#维度
dim(x)
attributes(x)#属性

y1 <- 1:6
dim(y1) <- c(2, 3)

# vector() + dim()
y2 <- matrix(1:6, 2, 3)

rbind(y1, y2)
cbind(y1, y2)

数组

#Array

—— 数组与矩阵类似,但是维度可以大于2,矩阵只能等于2


x <- array(1:24, dim <- c(4, 6))
x
x1 <- array(1:24, dim = c(2,3,4))
x1

 

最常用的还是向量、矩阵和数据框

 

列表

#list

—— 可以包含“不同类型“的对象

l <- list("a", 2, 10L, 3+5i, TRUE)
l
class(l)
l2 <- list(a=1, b=2, c=3)
l2
l3 <- list(c(1, 2, 3), c(4, 5, 6, 7))
x <- matrix(1:6, 2, 3)
dimnames(x) <- list(c("a", "b"), c("c", "d", "e"))

 

因子

#factor

-- 分类数据/ 有序 vs  无序

有序:高中低

无序:男女

-- 整数向量 + 标签

x <- factor(c("female", "female", "male", "male", "female"))
y <- factor(c("female", "female", "male", "male", "female"), levels = c("male", "female"))
table(x)
unclass(x)
unclass(y)

-- 创建因子

factor()

table()/unclass()

 

缺失值

#missing value

-- NA/NaN:NaN属于NA,NA不属于NaN

NaN一般用来表示数字型缺失值,而NA也能表示其他类型的值

-- NA有类型属性:integer NA,

          character NA等

-- 判断是否有NA

-- is.na()/is.nan()

 

x <- c(1, NaN, 2, NA, 3)
is.na(x)
is.nan(x)

 

数据框

# 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))

 

日期和时间
# date, time
-- 日期:date
    :距离19700101的天数julian(x) / date() / sys.date()

    :weekdays() / months() / quarters()

strptime(x, format)

posted @ 2016-04-12 23:58  莫青铜  阅读(311)  评论(0编辑  收藏  举报