r语言第四节课

#数据框
#Q:目前在R语言中我们学习了哪些数据结构?如向量。。。
#数据框
#创建数据框
stu.name <- c ( “xiaoming” ,“xiaohua”)
stu.id<- c(1001,1002)
stu.info <- data.frame (stu.name,stu.id) ; stu.info
str(stu.info)
#访口
stu.infoKaTeX parse error: Expected 'EOF', got '#' at position 36: …] stu.info[1,] #̲添加新列 stu.infoage <- c(17,18);stu.info
#查询
stu.info[which (stu.info a g e > = 18 ) , ] s t u . i n f o [ s t u . i n f o age>=18),] stu.info[stu.info age>=18),]stu.info[stu.infoage>=18,]
#矩阵转数据框
mat <- matrix (1:12,nrow=3) ; mat
mat.df <- as.data.frame(mat) ;mat.df
#列表的高级操作
#merge
stu.id.math <- c (1001,1003)
stu.math.score <- c(89,90)
stu.math.df <- data.frame (stu.id.math,stu.math.score) ;stu.math.df
?merge
merge(stu.info,stu.math.df,by.x = “stu.id”,by.y = “stu.od.math”)
#合并
#cbind,rbind
stu.info
new.stu <- data.frame(list(stu.name=“xiaohua”,stu.id=1004,age=19)) ;new.stu
stu.info <- rbind(stu.info, new.stu) ; stu.info
python.score <- c (99,100,100)
stu.info <-cbind(stu.info,python.score); stu.info
r.score <- c (98,99,100)
stu.info <-cbind(stu.info,r.score); stu.info
lapply (stu.info, max)
lapply(stu.info[3:ncol(stu.info)], max)
sapply(stu.info[3:ncol(stu.info)], max)
#因子
sex <- c(1,2);sex
#创建因子
sex.f <- factor(sex);sex.f
str(sex.f)
stu.sex <- sample(1:2,10,replace= T); stu.sex
stu.sex.f<-factor(stu.sex) ; stu.sex.f
stu.sex.f<- factor(stu.sex, levels = c(1,2));stu.sex.f
stu.sex.v <- as.vector(stu.sex.f);stu.sex.v
stu.sex.v <- as.vector(stu.sex.f);stu.sex.v
#有序因子
stu.sex.ord<- ordered (stu.sex, levels = c(1,2));stu.sex.ord
stu.sex.ord<- ordered (stu.sex,levels = c(2,1));stu.sex.ord
#cut函数
stu.r.score<- c(60,sample(60:100,8,replace = T),100);stu.r.score
stu.r.score.level <- cut (stu.r.score,breaks = 4);stu.r.score.level
#tapply()函数
tapply(stu.r.score, stu.sex, mean)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值