R语言Data Frame常用用法

Data Frame是R中最常用的数据结构,由行和列组成,相当于R中的表,与Matrix每列数据类型必须相同的区别是,数据框每个列可以是不同的数据类型。

Data Frame每一列有列名,每一行也可以指定行名。如果不指定行名,那么就是从1开始自增的Sequence来标识每一行。

(1) 创建数据框

> patientID <- c(1:4)
> age <- c(25,31,42,57)
> diabetes <- c("Type1","Type2","Type3","Type4")
> status <- c("Poor","Improved","Excellent","Poor")
> patientdata <- data.frame(patientID,age,diabetes,status)
> patientdata
patientID age diabetes status
1 1 25 Type1 Poor
2 2 31 Type2 Improved
3 3 42 Type3 Excellent
4 4 57 Type4 Poor

(2)与Matrix一样,使用[行Index,列Index]的格式可以访问具体的元素。

> patientdata
patientID age diabetes status
1 1 25 Type1 Poor
2 2 31 Type2 Improved
3 3 42 Type3 Excellent
4 4 57 Type4 Poor
> patientdata[1,]
patientID age diabetes status
1 1 25 Type1 Poor
> patientdata[,1]
[1] 1 2 3 4
> patientdata[1]
patientID
1 1
2 2
3 3
4 4

patientdata[1:2]
patientdata[c("patientID","age")]

patientID age
1 1 25
2 2 31
3 3 42
4 4 57

(3) attach()、 detach()和with()    使用attach和detach函数可以使得访问列时不需要总是跟着变量名在前面。

比如要打印所有age,那么可以写成:

> print(age)
[1] 25 31 42 57
> detach(patientdata)

with实现相同的功能

> with(patientdata,{
+ print(age)
+ })
[1] 25 31 42 57

(4) 查看数据类型

> str(patientdata)
'data.frame': 4 obs. of 4 variables:
$ patientID: int 1 2 3 4
$ age : num 25 31 42 57
$ diabetes : Factor w/ 4 levels "Type1","Type2",..: 1 2 3 4
$ status : Factor w/ 3 levels "Excellent","Improved",..: 3 2 1 3

修改数据类型

patientdata$diabetes<-as.character(patientdata$diabetes)

(5) 增加列:> patientdata$name <- c("Bob","Allen","Tom","Jack")

(6) 查询/子集

查询一个Date Frame,返回一个满足条件的子集,这相当于数据库中的表查询,是非常常见的操作。使用行和列的Index来获取子集是最简单的方法,前面已经提到过。如果我们使用布尔向量,配合which函数,可以实现对行的过滤。

这里我们想得到status为Poor的人的情况:

> patientdata[which(patientdata$status=="Poor"),]
patientID age diabetes status name
1 1 25 Type1 Poor Bob
4 4 57 Type4 Poor Jack

如果只想知道status为Poor的人的姓名:

> patientdata[which(patientdata$status=="Poor"),"name"]
[1] "Bob" "Jack"

还可以用subset更为简洁:

> subset(patientdata,status=="Poor" & age < 30,select = c("name","diabetes"))
name diabetes
1 Bob Type1

还可以用sql语句:

> library(sqldf)
> result<-sqldf("select * from patientdata where status='Poor' and age<30")
> result
patientID age diabetes status name
1 1 25 Type1 Poor Bob

(7)数据框连接

> patientdata1 <- patientdata
> rbind(patientdata,patientdata1)   ##按照列连接,列数必须相同
patientID age diabetes status name
1 1 25 Type1 Poor Bob
2 2 31 Type2 Improved Allen
3 3 42 Type3 Excellent Tom
4 4 57 Type4 Poor Jack
5 1 25 Type1 Poor Bob
6 2 31 Type2 Improved Allen
7 3 42 Type3 Excellent Tom
8 4 57 Type4 Poor Jack
> cbind(patientdata,patientdata1)   ##按照行连接,行数必须相同
patientID age diabetes status name patientID age diabetes status name
1 1 25 Type1 Poor Bob 1 25 Type1 Poor Bob
2 2 31 Type2 Improved Allen 2 31 Type2 Improved Allen
3 3 42 Type3 Excellent Tom 3 42 Type3 Excellent Tom
4 4 57 Type4 Poor Jack 4 57 Type4 Poor Jack

 (8) 更改数据框所有数据的格式

 df<- lapply(df,as.numeric)

转载于:https://www.cnblogs.com/W-Seven/p/5920331.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值