R之数据集
来源:互联网 发布:r语言编程艺术 pdf 编辑:程序博客网 时间:2024/04/27 03:43
在R中创建数据集有两步:
1、选择一种数据结构来存储数据;
2、将数据导入或输入到该数据结构中。
数据集:由数据构成的矩形数组,行表示观测、列表示变量。
存储数据的对象类型:标量、向量、矩阵、数组、数据框、列表。
向量
> setwd("D:\Documents\R\Rdataset")错误: 由""D:\D"开头的字符串中存在'\D',但没有这种逸出号> setwd("D:/Documents/R/Rdataset") #目录斜线只能是撇。> a <- c(1,2,8,3,-6,0) #数值型向量> b <- c("one","second","three") #字符型向量> c <- c(TRUE,FALSE,FALSE,TRUE,TRUE) #逻辑型向量> a[3] #和其他语言不同在于,没有“第零个”。[1] 8> a[c(3,4)][1] 8 3> a[3:6][1] 8 3 -6 0>
矩阵
> load("D:\\Documents\\R\\Rdataset\\.RData")> y <- matrix(1:20,nrow=5,ncol=4) #创建5*4矩阵> y [,1] [,2] [,3] [,4][1,] 1 6 11 16[2,] 2 7 12 17[3,] 3 8 13 18[4,] 4 9 14 19[5,] 5 10 15 20> cells <- c(1,26,24,68) > rnames <- c("R1","R2")> cnames <- c("C1","C2")> mymatrix <- matrix(cells,nrow=2,ncol=2,byrow=TRUE,dimnames=list(rnames,cnames))#创建2*2矩阵(按行填充)> mymatrix C1 C2R1 1 26R2 24 68> > mymatrixs <- matrix(cells,nrow=2,ncol=2,byrow=FALSE,dimnames=list(rnames,cnames))#创建2*2矩阵(按列填充)> mymatrixs C1 C2R1 1 24R2 26 68> > x <- matrix(1:10,nrow=2)> x [,1] [,2] [,3] [,4] [,5][1,] 1 3 5 7 9[2,] 2 4 6 8 10> x[2,][1] 2 4 6 8 10> x[,2][1] 3 4> x[1,4][1] 7> x[1,c(4,5)][1] 7 9>
数组
> 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))> z错误: 找不到对象'z'> Z, , C1 B1 B2 B3A1 1 3 5A2 2 4 6, , C2 B1 B2 B3A1 7 9 11A2 8 10 12, , C3 B1 B2 B3A1 13 15 17A2 14 16 18, , C4 B1 B2 B3A1 19 21 23A2 20 22 24> Z[1,2,3][1] 15>
数据框
> 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 status1 1 25 Type1 Poor2 2 34 Type2 Improved3 3 28 Type1 Excellent4 4 52 Type1 Poor> patientdata[1:2] patientID age1 1 252 2 343 3 284 4 52> patientdata[c("diabetes","status")] diabetes status1 Type1 Poor2 Type2 Improved3 Type1 Excellent4 Type1 Poor> patientdata$age[1] 25 34 28 52> table(patientdata$diabetes,patientdata$status) Excellent Improved Poor Type1 1 0 2 Type2 0 1 0
0 0
- R之数据集
- R语言之创建数据集
- R语言之创建数据集
- 大数据技术之R
- R语言之数据可视化
- R语言之数据可视化
- R语言之数据可视化
- R语言之数据操作
- R实战之从头到尾分析广告数据集
- R数据可视化之数据特征
- 数据挖掘之R与SQL
- R语言之数据挖掘篇
- R语言系列之数据生成
- R语言之数据挖掘包
- R语言之处理数据(一)
- R语言之处理数据(二)
- R语言数据分析系列之二
- R语言数据分析系列之三
- Unity 5 中的全局光照技术详解
- shell中的printf命令
- LeetCode 86. Partition List
- make命令行选项
- PHP: php_ldap.dll不能加载解决方案
- R之数据集
- jQuery——内容与表单选择器
- java sha1 加密
- LeetCode 83. Remove Duplicates from Sorted List
- Android Activity 生命周期
- numpy.random.shuffle打乱顺序函数
- MongoDB 学习三(数据库的增删改查)
- LeetCode 82. Remove Duplicates from Sorted List II
- 图解符号链接和硬链接的区别和i节点的介绍