美文网首页
R数据结构及子集提取

R数据结构及子集提取

作者: guguaihezi | 来源:发表于2019-03-05 19:12 被阅读0次

R的数据结构和数据类型

向量(vector)

一维结构。向量中的所有元素必须是相同数据类型。

  • 数值型向量 Numeric/ Integer/ Double
  • 字符型向量 Charactor
  • 逻辑型向量 Logical
(3 > 5) & (4 == 4)  #与
FALSE
(TRUE == TRUE) | (TRUE == FALSE)  #或
TRUE
((111 >= 111) | !(TRUE)) & ((4 + 1) == 5)   #!A是对A的否定
TRUE
  • 因子 Factor
a <- c(1, 2, 5, 3, 6, -2, 4)     # 数值型向量 
b <- c("one", "two", "three")    # 字符型向量 
c <- c(TRUE, NA, FALSE)          # 逻辑型向量 
province <- c("四川", "湖南", "江苏", "四川", "四川", "四川", "湖南", "江苏", "湖南", "江苏")   pf <- factor(province)          #创建 province 的因子 pf 
pf
[1] 四川 湖南 江苏 四川 四川 四川 湖南 江苏 湖南 江苏
Levels: 湖南 江苏 四川
矩阵(matrix)

二维结构。矩阵中所有元素也必须是统一数据类型,如不一样,R会强制转换数据类型。

my_matrix <- matrix(1:20, nrow = 4, ncol = 5)
my_matrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

(mat1<-matrix(1:12,6, byrow=T))    #可以加括号,直接看到赋值结果。byrow=T代表按行排列,=FALSE表示按列排列
mat1
mat2<-matrix(1:12,3,4, byrow=F)
mat2
数组(array)

数组可以是二维的也可以是多维的。

a <- array(data=1:18,dim=c(3,3,2))
a
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18
数据框(data frame)

二维结构。数据框中同一列元素类型一致,不同列可以不同;每一列有名称,列的名称不能以数字开头;列名称一定不能重复;行名和列名不能重复。

patientID <- c(1, 2, 3, 4) 
age <- c(25, 34, 28, 52) 
diabetes <- c("Type1", "Type2", "Type1", "Type1") 
status <- c("Poor", "Improved", "Excellent", "Poor") 
patientdata <- data.frame(patientID, age, diabetes, status) 
patientdata
  patientID age diabetes    status
1         1  25    Type1      Poor
2         2  34    Type2  Improved
3         3  28    Type1 Excellent
4         4  52    Type1      Poor

列表(list)

list好比是一个仓库,不限制数据类型,可以是vector,matrix,array,dataframe,甚至是list
list是一维的!!!

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,patientdata) 
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"

[[5]]
  patientID age diabetes    status
1         1  25    Type1      Poor
2         2  34    Type2  Improved
3         3  28    Type1 Excellent
4         4  52    Type1      Poor

向量提取和产生

a <- c(1, 2, 5, 3, 6, -2, 4)
a[3]
a[c(1, 3, 5)]    #提取第1,3,5位的元素
a[2:6]           #提取2,3,4,5,6位的元素
a[a>3&a<5]       #如果去掉中括号就变成了逻辑判断语句啦
a[-1]            #提取a中除了第1位以外的元素
a[-1:-3]         #提取a中除了第1,2,3位以外的元素
a[-c(1, 3, 5)]   #负号可以提出来,表示提取a中除了第1,3,5位以外的元素
  • rnorm()
    rnorm(n, mean = 0, sd = 1):r==random。类似的还有dnorm, pnorm, qnorm。d= density = 密度, p= probability = 概率 , q=quantile = 分位。
y <- rnorm(1000)    #产生1000个服从正态分布的随机数
  • rep()
    rep(x, times = 1, length.out = NA, each = 1)
    each:x中每个元素被重复each次
    times: 重复x这个向量的次数
    length.out:输出向量的期望长度
rep(2:5, 2)     
[1] 2 3 4 5 2 3 4 5
rep(2:5, 1:4)   
[1] 2 3 3 4 4 4 5 5 5 5
rep(1:3, times = 4, each = 2) 
[1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3
rep(2:5, 2, length.out = 10)
[1] 2 3 4 5 2 3 4 5 2 3
  • seq()
    seq(from = , to = , by = , length.out = , ...)
    from :向量起点
    to: 向量终点
    by: 步长
    length.out: 期望输出的向量长度
seq(1, 10, by = 0.5)
[1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0  8.5  9.0  9.5 10.0

矩阵提取

x <- matrix(1:6, 2, 3) 
x[2,2]
x[2,]
x[,2]
x[1:2,2:3]
y <- c(3, 4, 5, 6)
pmax(x, y)           #pmin,pmax用来求相同位置的最大数和最小数
     [,1] [,2] [,3]
[1,]    3    5    5
[2,]    4    6    6
Warning message:
In pmax(x, y) : an argument will be fractionally recycled

数组提取

a <- array(data=1:18,dim=c(3,3,2))
a[2, 3, 2]
[1] 17

数据框提取

数据框可用“$”提取特定列

patientdata[,"age"]
[1] 25 34 28 52
patientdata$age
[1] 25 34 28 52

列表提取

[]代表提取list中的特定元素,其数据结构还是list;[[]]代表提取list中特定元素的内容,其数据结构跟提取元素一致。

> mylist[3]    
[[1]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

> mylist[[3]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10
> class(mylist[3])
[1] "list"
> class(mylist[[3]])
[1] "matrix"

相关文章

网友评论

      本文标题:R数据结构及子集提取

      本文链接:https://www.haomeiwen.com/subject/nuaouqtx.html