R语言数据结构

来源:互联网 发布:纳粹十字勋章淘宝 编辑:程序博客网 时间:2024/06/09 06:50

对象的基本类型

  1. 字符(character)
    • x <- “sjming”
  2. 数值(numeric;real numbers)
    • x <- 3.14
  3. 整数(integer)
    • x <- 2L
  4. 复数(complex)
    • x <- 1+2i
  5. 逻辑(logical)(TF必须大写)
    x <- TRUE
  6. 常用方法
    • 查看对象类型:class(x)
    • 大小写敏感

对象的属性

  1. 名称
  2. 维度
  3. 类型
  4. 长度

向量

  1. 创建

    # 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.
  2. 强制类型转换函数
    • as.numeric(x)
    • as.logical(x)
    • as.character(x)
  3. 查看属性
    • class(x) //类型
    • names(x) <- c(“a”, “b”, “c”, “d”) //名称

矩阵

  1. 相当于 向量+维度(nrow,ncol)
  2. 创建

    # 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)
  3. 查看属性
    • dim(x) //查看定义
    • attributes(x) //查看所有属性
    • rbind(x1, x2) //矩阵按行拼接
    • cbind(x1, x2) //矩阵按列拼接

数组(了解即可)

  1. 与矩阵类似,但是维度可以大于2,矩阵维度只能等于2
  2. 创建

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

列表

  1. 可以包含不同类型的对象
  2. 创建

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

因子

  1. 处理分类数据,有序和无序
    因子可以理解为:整数向量+标签
    常用语线性模型
  2. 创建

    # factorx <- factor(c("female", "female", "male", "male", "female"))y <- factor(c("female", "female", "male", "male", "female"), levels = c("male", "female"))

    注意:这里用levels设置标签,在前的是基线水平

  3. 查看属性
    • table(x)
    • unclass(x) //去掉属性,变为integer向量

缺失值

  1. NA/NaN
    • NaN属于NA,NA不属于NaN
    • NaN一般表示整数的缺失值,而NA可以表示各种缺失值
  2. 常用方法
    • is.na()/is.nan() //判断一个向量中是否有缺失值

数据框

  1. 存储表格数据
    • 视为各元素长度相同的列表
      • 每个元素代表一列数据
      • 每个数据的长度代表行数
      • 元素类型可以不同
  2. 创建

    # data framedf <- data.frame(id = c(1,2,3,4), name = c("a","b","c","d"), gender = c(TRUE,TRUE,FALSE,FALSE))
  3. 常用函数
    • nrow(df) //查看行数
    • ncol(df) //查看列数
    • data.matrix(df) // transform to matrix

日期和时间

  1. Date
    • 距离1970-1-1的天数
  2. 使用

    # 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
  3. 时间POSIXct/POSIXlt
    • 距离1970的秒数
    • POSIXct, 整数, 常用于存入数据框
    • POSIXlt, 列表,包含星期,年月日的信息
  4. 使用

    # 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
原创粉丝点击