R语言_数据结构

#向量
my_vector <- c(1, 2, -8, 9, 16) 
my_vector[2:4]

#矩阵
#矩阵行列命名,默认先排列
cells <- c(1, 36, 24, 12)
row_names <- c(“R1″, “R2″)
col_names <- c(“C1″, “C2″)
my_matrix1 <- matrix(cells, nrow=2, ncol=2, dimnames=list(row_names, col_names)) 
#矩阵行列命名,并且先排行
my_matrix2 <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(row_names, col_names))        
#矩阵下标的使用
my_matrix3 <- matrix(1:10, nrow=2)
my_matrix3
my_matrix3[1,2]
my_matrix3[1,]
my_matrix3[,1]
my_matrix3[c(1, 2), c(2, 3)]

#数组
dim1 <- c(“A1″, “A2″)
dim2 <- c(“B1″, “B2″, “B3″)
dim3 <- c(“C1″, “C2″, “C3″, “C4″)
my_array <- array(1:24, dim=c(2, 3, 4), dimnames=list(dim1, dim2, dim3))

#数据框
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[1:2]
patientdata[c("diabetes","status")] 
patientdata$age 
#组合
cbind
rbind

#因子
sex = factor(c("male","female","female"))   #无序性因子
sex = factor(c("male","female","female"),order=TRUE,levels=c("male","female"))  #有序性因子
sex1 = factor(c(1,0,0),levels=c(0,1),labels=c("male","female")) #构建因子的简单表示

#列表
ch1 <- “My First List” #创建一个字符标量
vec1 <- c(25, 26, 18, 39)#创建一个向量
matrix1 <- matrix(1:10, nrow=5)#创建一个矩阵
ch2 <- c(“one”, “two”, “three”)#创建一个字符向量
my_list <- list(title=ch1, ages=vec1, matrix1, ch2)
my_list[1]
my_list[[1]]
my_list["title"]
my_list$title
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值