r语言随笔

这周整理了r语言的数据集的一些用法和一些基础构建,代码如下
#获取矩阵的函数和列数
m
mrow(m)
ncol(m)
#矩阵删除元素
m <- matrix(1:12,nrow =4)
m
m[-1,]
m[,-1]
m[-1,-1]
#改变矩阵的值
m
m.value <- matrix(rep(6.0),nrow = 2)
m.value
m[c(1,3),]
m[c(1,3),] <- m.value
m
#应用:修改图片
#install.packages(“pixmap”)
library(pixmap)
p <- read.pnm(python.pgm)
p
plot§
str§
p2 <- p
p2 @grey[80:103,10:60] < -1
plot(p2)
#apply函数,对矩阵的行或者列调用函数
?apply
#apply(m,dim.index, f)dim.index:1代表每一行应用函数f,2代表每一列应用函数f
m <- matrix(1:6,nrow =3)
m
#求每一行最大值
apply(m,1,max)
#求每一列最大值
apply(m,2,max)
#cbind()rbind()组合矩阵数据
m <- matrix(1:12, nrow = 3)
m
col.v <- matrix(21:26, nrow = 3)
cbind(m,col.v)
rbind(m,col.v)
row.v <- matrix(21:28, nrow = 2)
row.v
rbind(m,row.v)
#数组
#数组
test1 <- matrix(sample(90:100,6,replace =F), nrow = 2)
test1
test2 <- matrix(sample(90:100,6,replace = F), nrow = 2)
test2
test <- array(data=c(test1,test2),dim=c(2,3,2))
test
attributes(test)
str(test)
test[,1]
test[1,1]
test[,1,1]
test[1,1,1]
#列表
#创建列表
stu.lst <- list (name = “xiaoming”, id=1001,is.past.exam = T)
stu.lst
stu.lst2 <- list(“xiaoming”,1001,T”) #可以不给列表加标签
stu.lst2
new.1 <- vector(mode = “list”)
new.l
new.l[[“name”]] <-“xiaoming”
new.1
#列表常规操作
#列表索引
stu.lst
stu.lstKaTeX parse error: Expected 'EOF', got '#' at position 123: …me2 str(name2) #̲双中括号一次提取出的是列表的一…age <- 19
stu.lst
length(stu.lst)#获取列表的长度
stu.lst[[5]] <- 98
stu.lst
stu.lst[6:8] <- 1:3
stu.lst
#列表删除元素
stu.lst
stu.lst[[8]] <- NULL
stu.lst
stu.lst[6:7] <- NULL
stu.lst
stu.lstKaTeX parse error: Expected 'EOF', got '#' at position 21: …- NULL stu.lst #̲将多个列表拼接为一个列表 st…stu.name
stu.info[,1]
stu.info[1,]
#添加新列
stu.infoKaTeX parse error: Expected 'EOF', got '#' at position 26: …7,18);stu.info #̲查询 stu.info[whi…age>=18),]
stu.info[stu.info$age>=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,s9,10o)
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、付费专栏及课程。

余额充值