R - 数据结构

it2025-01-14  32

一. 两种基本数据结构

概念 (1)原子向量:包含单个数据类型的数组 (2)泛型向量:也称为列表,是原子向量的集合。列表是递归的包含其他列表的

原子向量 (1)原子向量是用c()函数组合起来的元素一维数组

> paceed <- c(TRUE,TRUE) > paceed [1] TRUE TRUE

(2)R没有标量型数据:标量是一个单一元素。即

k<-2 是 k<-c(2) 的简写

(3)许多R的数据类型都是带有特定属性的原子向量     i. 矩阵是一个带有dim属性的原子向量。dim属性包含2个元素(行数和列数)

> x <- c(1,2,3,4,5,6,7,8,9,0) # 向量 > class(x) [1] "numeric" > x [1] 1 2 3 4 5 6 7 8 9 0 > attr(x,"dim") <- c(2,5) # 为向量增加dim属性:attr方法 > x [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 0 > class(x) # x类型 [1] "matrix" > attributes(x) # x的属性 $dim [1] 2 5 # 增加dimnames属性 > attr(x,"dimnames") <- list(c("A1","A2"),c("B1","B2","B3","B4","B5")) > x B1 B2 B3 B4 B5 A1 1 3 5 7 9 A2 2 4 6 8 0

    ii. 矩阵可以通过取出dim属性,变成一维数组

> attr(x,"dim") <- NULL > x [1] 1 2 3 4 5 6 7 8 9 0

泛型向量R > head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosaR > attributes(iris)R > unclass(iris) (1) 索引       i. 提取向量中的元素,使用下标来取。R的下标从1开始

> x <- c(20,30,40) # 声明向量 > x[3] # x[3]:取第3个元素 [1] 40 > x[c(1,2)] # 取2个元素:c(2,3) [1] 20 30

      ii. 有命名的原子变量可以使用名字提取成分

> x <- c(A=20,B=30,C=40) # 命名的原子向量 > x # x打印 A B C 20 30 40 > x[c("A","B")] # 用命名提取 A B 20 30

二. 流程控制

for循环 (1)语法结构R for (var in seq){ statements } (2)例子 ```R

for(i in 1:5){

print (1:i)} [1] 1 [1] 1 2 [1] 1 2 3 [1] 1 2 3 4 [1] 1 2 3 4 5

```

if与else (1)语法R if(condition){ statement1 }else{ statement2 } (2)ifelse() 函数       i. ifelse() 是 if的量化版本,可以对向量中的每个元素进行流程判断       ii. 代码示例R > pvalues <- c(.087,.0018,.0054,.1572,.0183,.5386) # 声明向量 > pvalues [1] 0.0870 0.0018 0.0054 0.1572 0.0183 0.5386 > result <- ifelse(pvalues<.015,"Significant","Not significant") # ifelse函数 > result # 判断结果 [1] "Not significant" "Significant" "Significant" "Not significant" [5] "Not significant" "Not significant" (3)等价的显式循环R > results <- vector(mode="character",length=length(pvalues)) # 声明结果 > for(i in 1:length(pvalues)){ # 显式循环 + if (pvalues[i]<0.5) + results[i] <- "signnificant" + else + results[i] <- "Not Significant" + } > > results [1] "signnificant" "signnificant" "signnificant" "signnificant" [5] "signnificant" "Not Significant"

三. 创建函数

函数的声明方式

functionName <- function( parameters ) { statements return value } > f <- function(x,y,z=1){ + result <- x + 2*y + 3*z + return(result) + } > f(1,2) [1] 8 > args(f) # args返回函数的参数和默认值,用户交互式R终端 function (x, y, z = 1) NULL > formals(f) # formals,用于代码中获取函数参数 $x $y $z [1] 1

环境变量 (1)生命新的环境,在环境中设置变量,从环境中取变量

> myenv <- new.env() > assign("x","Homer",env=myenv) > ls(myenv) [1] "x" > x [1] 5 > get("x",env=myenv) [1] "Homer" > get("x") [1] 5

(2)查看环境的父环境R > parent.env(myenv) # 顶级环境的父环境为空 <environment: R_GlobalEnv>

四. 面向对象编程

泛型函数 (1)R使用对象的类来确定,当一个泛型函数被调用时,应该采用什么样的行动。 ```R

mymethod <- function(x,...) UseMethod("mymethod") mymethod.a <- function(x) print("Using A") mymethod.b <- function(x) print("Using B") mymethod.default <- function(x) print("Using Default")

class(x) <- "a" mymethod(x) [1] "Using A"

class(x) <- c("c","a","b") # 当一个对象被分为2个类型时,泛型函数取第一个存在于泛型函数列表的类型 mymethod(x) [1] "Using A" # x的类型为c,a,b:c不在myfunction的类型表里面,取后面的类型a

```

五. 数据结构

向量 (1)是存储同一种数据类型的一位数组R a <- c(1,2,3,5,6,-2) # c:组合函数 print(a) (1)标量:致函一个元素的向量 b <- 3 print(b) (2)不同类型的标量,存在于一个数组中,会类型转换:(布尔->int->字符串)

c <- c(TRUE,TRUE,'a',2) # "TRUE" "TRUE" "a" "2" print(c)

(3)方括号中指明数字,来取向量的第几个元素 ###

print( a[2] ) # 2 print( a[c(2,3)] ) # 2 3 : 访问a第2和第3个元素 print( a[2:4] ) # 2 3 5

矩阵是存储同一种数据类型的二维数组

y <- matrix(1:20 , nrow=5 , ncol=4 , byrow=TRUE) # 行填充 print(y)

数组是存储同一种数据类型的三维数组

dim1 <- c("A1","A2") # 行 dim2 <- c("B1","B2","B3") # 列 dim3 <- c("C1","C2","C3","C4") # 面 z <- array(1:24 , c(2,3,4) , dimnames=list(dim1,dim2,dim3)) print(z) print(z[1,2,3])

数据框和矩阵类似,是一种二维数组,但数据框的每列,类型可以不一样 ,列内元素类型一致

col1 <- c(1,2,3,4) col2 <- c("zhangsan","lisi","wangwu","zhaoliu") col3 <- c("Poor","Improved","Excellent","Poor") df <- data.frame(col1,col2,col3) print(df[1:2]) # 打印第1,2列

因子

diabets <- c("Type1","Type2","Type1","Type1") #因子 print(diabets) # 用factor函数后,将向量转换成有序的因子:内部存储为(1,2,1,1),并做映射 1<->Type1, 2<->Type2 diabets <- factor(diabets,ordered=TRUE) # 排序的有序性 print(diabets)

列表:各种对象的有序集合 ```R g <- "My First List" h <- c(25,26,27,34) j <- matrix(1:10,nrow=5) ### 数组 k <- c("one","two","three")

mylist <- list(title=g,ages=h,j,k) print(mylist\(title) print(mylist\)ages) print(mylist[[3]]) ```

转载于:https://www.cnblogs.com/moonlord/p/6039154.html

相关资源:I
最新回复(0)