R语言 data.frame 大全
A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors n, s, b.
> s = c("aa", "bb", "cc")
> b = c(TRUE, FALSE, TRUE)
> df = data.frame(n, s, b) # df is a data frame
Build-in Data Frame
We use built-in data frames in R for our tutorials. For example, here is a built-in
data frame in R, called mtcars.
mpg cyl disp hp drat wt ...
Mazda RX4 21.0 6 160 110 3.90 2.62 ...
Mazda RX4 Wag 21.0 6 160 110 3.90 2.88 ...
Datsun 710 22.8 4 108 93 3.85 2.32 ...
............
The top line of the table, called the header, contains the column names. Each
horizontal line afterward denotes a data row, which begins with the name of the
row, and then followed by the actual data. Each data member of a row is called a
cell.
To retrieve data in a cell, we would enter its row and column coordinates in the
single square bracket "[]" operator. The two coordinates are separated by a comma.
In other words, the coordinates begins with row position, then followed by a comma,
and ends with the column position. The order is important.
Here is the cell value from the first row, second column of mtcars.
[1] 6
Moreover, we can use the row and column names instead of the numeric
coordinates.
[1] 6
Lastly, the number of data rows in the data frame is given by the nrow function.
[1] 32
And the number of columns of a data frame is given by the ncol function.
[1] 11
Further details of the mtcars data set is available in the R documentation.
Preview
Instead of printing out the entire data frame, it is often desirable to preview it with
the head function beforehand.
mpg cyl disp hp drat wt ...
Mazda RX4 21.0 6 160 110 3.90 2.62 ...
............
访问元素
student[1,]
student[,2]
idname<-student[1:2]
idname<-student[c("ID","Name”)]
name<-student[[2]] 或者name<-student[[“Name”]] 或者name<-student$Name
attach(student)
print(Name)
detach(student)
with(student,{
n<-Name
print(n)
})
修改列数据类型
student$Name<-as.character(student$Name)
student$Birthdate<-as.Date(student$Birthdate)
添加新列
student$Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(student$Birthdate,"%Y”))
student<-within(student,{
Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(Birthdate,"%Y"))
})
查询/子集
Frame,返回一个满足条件的子集,这相当于数据库中的表查询,是非常常见的操作。使用行和列的Index来获取子集是最简单的方法,前面已经提到过。如果我们使用布尔向量,配合which函数,可以实现对行的过滤。比如我们要查询所有Gender为F的数据,那么我们首先对student$Gender==“F”,得到一个布尔向量:FALSE
FALSE TRUE,然后使用which函数可以将布尔向量中TRUE的Index返回,所以我们的完整查询语句就是:
student[which(student$Gender=="F"),]
student[which(student$Gender=="F"),"Age”]
subset(student,Gender=="F" & Age<30 ,select=c("Name","Age"))
x.sub1 <- subset(x.df, y > 2 & V1 > 0.6)
newdata <- subset(mydata, age >= 20 | age < 10, select=c(ID, Weight))
newdata <- subset(mydata, sex=="m" & age > 25, select=weight:income)
x.sub2 <- subset(x.df, y > 2 & V2 > 0.4, select = c(V1, V4))
x.sub3 <- subset(x.df, y > 3, select = V2:V5)
x.sub4 <- x.df[x.df$y == 1, ]
x.sub5 <- x.df[x.df$y %in% c(1, 4), ]
library(sqldf)
result<-sqldf("select Name,Age from student where Gender='F' and Age<30")
连接/合并
score<-data.frame(SID=c(11,11,12,12,13),Course=c("Math","English","Math","Chinese","Math"),Score=c(90,80,80,95,96))
result<-merge(student,score,by.x="ID",by.y="SID")
student2<-data.frame(ID=c(21,22),Name=c("Yan","Peng"),Gender=c("F","M"),Birthdate=c("1982-2-9","1983-1-16"),Age=c(32,31))
rbind(student,student2)
R语言 data.frame 大全的更多相关文章
- R语言Data Frame数据框常用操作
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...
- 转载:R语言Data Frame数据框常用操作
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...
- R语言data.table包fread读取数据
R语言处理大规模数据速度不算快,通过安装其他包比如data.table可以提升读取处理速度. 案例,分别用read.csv和data.table包的fread函数读取一个1.67万行.230列的表格数 ...
- R 给data.frame(dataframe)添加一列
x<-data.frame(apple=c(1,4,2,3),pear=c(4,8,5,2)) x # apple pear # 1 1 4 # 2 4 8 # 3 2 5 # 4 3 2 x$ ...
- R语言数据分析利器data.table包 —— 数据框结构处理精讲
R语言data.table包是自带包data.frame的升级版,用于数据框格式数据的处理,最大的特点快.包括两个方面,一方面是写的快,代码简洁,只要一行命令就可以完成诸多任务,另一方面是处理 ...
- R语言基因组数据分析可能会用到的data.table函数整理
R语言data.table包是自带包data.frame的升级版,用于数据框格式数据的处理,最大的特点快.包括两个方面,一方面是写的快,代码简洁,只要一行命令就可以完成诸多任务,另一方面是处理快,内部 ...
- R语言数据分析利器data.table包—数据框结构处理精讲
R语言数据分析利器data.table包-数据框结构处理精讲 R语言data.table包是自带包data.frame的升级版,用于数据框格式数据的处理,最大的特点快.包括两个方面,一方面是写的快,代 ...
- keep or remove data frame columns in R
You should use either indexing or the subset function. For example : R> df <- data.frame(x=1:5 ...
- R之data.table速查手册
R语言data.table速查手册 介绍 R中的data.table包提供了一个data.frame的高级版本,让你的程序做数据整型的运算速度大大的增加.data.table已经在金融,基因工程学等领 ...
随机推荐
- MySQL 5.6学习笔记(索引的创建与删除)
1. 创建索引 1.1 创建新表时同时建立索引 语法: create table table_name[col_name data_type] [unique|fulltext|spatial][in ...
- Linux应用server搭建手冊—Weblogic服务域的创建与部署
前一篇谈到了Weblogic的安装,接下来整理weblogic服务域的创建及项目的部署 一.webLogic服务域创建 使用root用户登录后使用命令:#su W ...
- Atitit jdbc 处理返回多个结果集
Atitit jdbc 处理返回多个结果集 Statement接口提供了三种执行SQL语句的方法: executeQuery.executeUpdate和execute.使用哪一个方法由SQL语句所 ...
- Atitit opencv 模板匹配
Atitit opencv 模板匹配 1.1. 图片1 1.2. Atitit opencv 模板匹配 6中匹配算法貌似效果区别不大1 1.3. 对模板缩放的影响 一般的缩放可以,太大了就歇菜了.. ...
- C++11 explicit的使用
C++中的explicit关键字只能用于修饰只有一个参数的类构造函数 , 它的作用是表明该构造函数是显示的, 而非隐式的,跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况 ...
- linux 命令(43):bash 快捷键操作
ctrl 命令行快捷键: CTRL+A: – 快速移动到行首. 我们假设你输入了像下面这样的命令.当你在第 N 行时,你发现在行首字符有一个输入错误 $ gind . -mtime -1 -type ...
- redis使用日志(4):如何让外部服务器访问
开启redis 允许外网IP 访问 在 Linux 中安装了redis 服务,当在客户端通过远程连接的方式连接时,报could not connect错误. 错误的原因很简单,就是没有连接上redis ...
- Spring boot下添加filter
https://www.cnblogs.com/OnlyCT/p/7133639.html ****************************************************** ...
- Django admin 常用方法
1.调整页面头部显示内容和页面标题 #admin.py admin.site.site_header = '广告业务系统' admin.site.site_title = '广告业务系统'
- zoj 3761(并查集+搜索)
题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...