- 本文梳理R语言数据结构:向量、矩阵、数据框;
- 数据IO及实用函数。
更多精彩:pythonic生物人
1、c()创建向量(Vector)
向量创建
查看向量数据类型
向量切片
2、 matrix()创建矩阵(matrix)
语法
创建矩阵
矩阵切片
3、data.frame()创建数据框
语法
创建数据框
数据框切片
4、R中数据导入|输出
R支持数据导入方式
R中数据Import/Export包
5、R处理数据对象实用函数
a <- c(1, 2, 5, 3, 6, -2, 4)#创建向量a
a
a[c(1, 4)]#提取a中的第一和第四个元素,向量中索引从1开始,区别于python从0开始
a[1:4]#提取1到4个元素#等价于a[c(1:4)]
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value, dimnames=list(
char_vector_rownames, char_vector_colnames))
vector 矩阵的元素
nrow和ncol 指定行和列的维数
dimnames 行名和列名
byrow 按行填充(byrow=TRUE)按列填充(byrow=FALSE) ,默认情况下按列填充。
cells <- c(1,26,'aa',68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
print('#取第一行')
print(mymatrix[1,])
print('#取第一列')
print( mymatrix[,1])
print('#取第一行,1,2列')
print(mymatrix[1,c(1,2)])
mydata <- data.frame(col1, col2, col3,...)
其中的列向量col1、 col2、 col3等可为任何类型(如字符型、数值型或逻辑型)。每一列的名
称可由函数names指定。
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)
print(patientdata)
print('#提取1,2列')
print(patientdata[1:2])#数字指定
print('#提取diabetes,status列')
print(patientdata[c("diabetes", "status")])#列名指定
print('#提取age列')#$列名指定
print(patientdata$age)
会涉及到如下多种包:参考 https://mirrors.tuna.tsinghua.edu.cn/CRAN//doc/manuals/R-data.pdf
DBI ( https://CRAN.R-project.org/package=DBI):#包名称及CRAN地址David A. James#包作者dataframes2xls ( https://CRAN.R-project.org/package=dataframes2xls ):Guido van Steenforeign ( https://CRAN.R-project.org/package=foreign ):Thomas Lumley, Saikat DebRoy, Douglas Bates, Duncan Murdoch andRoger Bivandgdata ( https://CRAN.R-project.org/package=gdata ):Gregory R. Warnesncdf4 ( https://CRAN.R-project.org/package=ncdf4 ):David PiercerJava ( https://CRAN.R-project.org/package=rJava ):Simon UrbanekRJDBC ( https://CRAN.R-project.org/package=RJDBC ):Simon UrbanekRMySQL ( https://CRAN.R-project.org/package=RMySQL ):David James and Saikat DebRoyRNetCDF ( https://CRAN.R-project.org/package=RNetCDF ):Pavel MichnaRODBC ( https://CRAN.R-project.org/package=RODBC ):Michael Lapsley and Brian RipleyROracle ( https://CRAN.R-project.org/package=ROracle ):David A. JamesRPostgreSQL ( https://CRAN.R-project.org/package=RPostgreSQL ):Sameer Kumar Prayaga and Tomoaki NishiyamaRSPerl : Duncan Temple LangRSPython :Duncan Temple LangRSQLite ( https://CRAN.R-project.org/package=RSQLite ):David A. JamesSJava : John Chambers and Duncan Temple LangWriteXLS ( https://CRAN.R-project.org/package=WriteXLS ):Marc SchwartzXLConnect ( https://CRAN.R-project.org/package=XLConnect ):Mirai Solutions GmbHXML ( https://CRAN.R-project.org/package=XML ):Duncan Temple Lang
函 数 用 途
length(object) 显示对象中元素/成分的数量
dim(object) 显示某个对象的维度
str(object) 显示某个对象的结构
class(object) 显示某个对象的类或类型
names(object) 显示某对象中各成分的名称
c(object, object,...) 将对象合并入一个向量
cbind(object, object, ...) 按列合并对象
rbind(object, object, ...) 按行合并对象
head(object) 列出某个对象的开始部分
tail(object) 列出某个对象的最后部分
参考资料
r-in-action-second-edition