R语言数据结构
来源:互联网 发布:纳粹十字勋章淘宝 编辑:程序博客网 时间:2024/06/09 06:50
对象的基本类型
- 字符(character)
- x <- “sjming”
- 数值(numeric;real numbers)
- x <- 3.14
- 整数(integer)
- x <- 2L
- 复数(complex)
- x <- 1+2i
- 逻辑(logical)(TF必须大写)
x <- TRUE - 常用方法
- 查看对象类型:class(x)
- 大小写敏感
对象的属性
- 名称
- 维度
- 类型
- 长度
向量
创建
# Vectorx <- vector("character", length = 10) #This is annotationx1 <- 1:4 #integerx2 <- c(1,2,3,4) #numric,If the type is not same, they will be transform to one.
- 强制类型转换函数
- as.numeric(x)
- as.logical(x)
- as.character(x)
- 查看属性
- class(x) //类型
- names(x) <- c(“a”, “b”, “c”, “d”) //名称
矩阵
- 相当于
向量+维度(nrow,ncol)
创建
# Matrix x <- matrix(nrow = 3, ncol = 2)x1 <- matrix(1:6, nrow = 3, ncol = 2)y <- 1:6 # by Vectordim(y) <- c(2,3)y2 <- matrix(1:6, nrow = 2, ncol = 3)rbind(y, y2)cbind(y, y2)
- 查看属性
- dim(x) //查看定义
- attributes(x) //查看所有属性
- rbind(x1, x2) //矩阵按行拼接
- cbind(x1, x2) //矩阵按列拼接
数组(了解即可)
- 与矩阵类似,但是维度可以大于2,矩阵维度只能等于2
创建
# Arrayx <- array(1:24, dim = c(4,6))x1 <- array(1:24, dim = c(2, 3, 4))
列表
- 可以包含不同类型的对象
创建
# listl <- list("a", 2, 10L, 3+4i, TRUE)l1 <- list(a=1, b=2, c=3) #name is a,b,c,value is 123l2 <- list(c(1,2,3), c(4,5,6,7))x <- matrix(1:6, nrow = 2, ncol = 3) # name for matrixdimnames(x) <- list(c("a","b"), c("c", "d", "e"))
因子
- 处理分类数据,有序和无序
因子可以理解为:整数向量+标签
常用语线性模型 创建
# factorx <- factor(c("female", "female", "male", "male", "female"))y <- factor(c("female", "female", "male", "male", "female"), levels = c("male", "female"))
注意:这里用levels设置标签,在前的是基线水平
- 查看属性
- table(x)
- unclass(x) //去掉属性,变为integer向量
缺失值
- NA/NaN
- NaN属于NA,NA不属于NaN
- NaN一般表示整数的缺失值,而NA可以表示各种缺失值
- 常用方法
- is.na()/is.nan() //判断一个向量中是否有缺失值
数据框
- 存储表格数据
- 视为各元素长度相同的列表
- 每个元素代表一列数据
- 每个数据的长度代表行数
- 元素类型可以不同
- 视为各元素长度相同的列表
创建
# data framedf <- data.frame(id = c(1,2,3,4), name = c("a","b","c","d"), gender = c(TRUE,TRUE,FALSE,FALSE))
- 常用函数
- nrow(df) //查看行数
- ncol(df) //查看列数
- data.matrix(df) // transform to matrix
日期和时间
- Date
- 距离1970-1-1的天数
使用
# data framex <- date() # standard timeclass(x) # characterx2 <- Sys.Date() # 2016-05-19class(x2) # Datex3 <- as.Date("2016-01-01") #new Dateclass(x3) # Dateweekdays(x3) # Chinese, Fridaymonths(x3) # Chinese, Januaryquarters(x3) # Q1, first quarterjulian(x3) # 16801# math calculatex4 <- as.Date("2016-05-01") x4-x3 # Time difference of 121 daysas.numeric(x4-x3) # 121
- 时间POSIXct/POSIXlt
- 距离1970的秒数
- POSIXct, 整数, 常用于存入数据框
- POSIXlt, 列表,包含星期,年月日的信息
使用
# Timex <- Sys.time() # "2016-05-19 09:50:46 CST"class(x) # "POSIXct" "POSIXt" p <- as.POSIXlt(x) # "2016-05-19 09:54:33 CST"class(p) # "POSIXlt" "POSIXt" names(unclass(p)) # "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"p$sec # 33.33464x1 <- "1 1, 2016 01:01"strptime(x1, "%m %d, %Y %H:%M") # "2016-01-01 01:01:00"
0 0
- R语言中数据结构
- R语言中的数据结构
- R语言_数据结构
- R语言的数据结构
- R语言数据结构
- R语言数据结构
- R语言-数据结构
- R语言 数据结构
- R语言数据结构-数据框
- R语言数据结构实例入门
- R语言数据结构2—matrix
- R语言数据结构3—array
- R语言数据结构4—dataframe
- R语言数据结构5—factor
- R语言数据结构6—list
- R语言数据结构1—vector
- R语言中的数据结构——向量
- R语言的数据类型和数据结构
- Leetcode 347. Top K Frequent Elements
- GoAhead2.5源代码分析之3-用户管理实例(umui.c)
- Mac使用docker及docker-machine命令
- JSON 解析
- Centos修改网卡配置
- R语言数据结构
- webdriver获取input元素值的几种方法
- 使用 javacomm20-win32.zip串口包eclispe配置注意点
- 将一个链表翻转
- android 中动态监听Edittext控件内容长度 TextWatcher监听事件
- Oracle中dual虚拟表的用途
- Oracle trunc()函数的用法
- plsql客户端连接远程和本地数据库
- em与px的区别