当前位置:网站首页>【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
2022-07-31 04:31:00 【苏念心】
前言
R语言绘图虽然看着简洁,但是函数参数实在是太多了——虽然学了4天,但是像是学了一周,其实我目前目的是画好看的图,那么如果有绘图模板就好了。由于买的书中的函数调用太多简洁,df$SOD等变量数据不知从何而来,所以要自己积累模板了。
这里存在一些问题,虽然学了知识就算忘了也有印象,但是忘的太多又要重学!比如几个月或是几年才捡起来,用几次的matlab,每次都要重敲一两天的语法代码和绘图代码,才能找到感觉……
目录
\;\\\;\\\;
apply
m <- array(
c(1:10),
dim=c(2,5)
# byrow=TRUE #按行填充 array没有这个参数
)
m
a=apply(m,1,sum) #按行求和
a
a=apply(m,2,sum) #按列求和
a
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> a
[1] 25 30
> a
[1] 3 7 11 15 19
a <- array(
c(1:8),
dim=c(2,2,2)
)
a
res=apply(a,1,sum) #第1层2x2,第2层2x2
res
res=apply(a,2,sum) #第1片2x2,第2片2x2
res
res=apply(a,c(1,2),sum) #6根1x2
res
> a
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
> res
[1] 16 20
> res
[1] 14 22
> res
[,1] [,2]
[1,] 6 10
[2,] 8 12
\;\\\;\\\;
tapply 分组计算
v <- c(1:5)
v
group <- c(rep('a',3),rep('b',2))
group
#a b
#6 9
tapply(v,group,sum) #按group分组,将对应的v中的元素加起来
mat <- matrix(c(1:10),nrow=2)
mat
group <- matrix(c(rep(1,5),rep(2,5)),nrow=2)
group
# 1 2
#15 40
# tapply(mat,group,sum) #按group分组,将对应的mat中的元素加起来
#1 2
#3 8
tapply(mat,group,mean) #按group分组,将对应的mat中的元素加一起求均值
\;\\\;\\\;
lapply 多元素按行分组计算
需要转成data.frame
l <- list(
a=c(1:5),
b=c(6:10)
)
#返回一个list
res <- lapply(l,mean ) # 3 8
#转成data.frame
res <- as.data.frame(res)
res
> res
a b
1 3 8
rownames <- paste('a',LETTERS,sep='#')
rownames
#去掉前缀a#
names <- unlist(lapply(
strsplit(rownames,'#'),
function(x) x[2] #取第二个元素
))
names
> rownames
[1] "a#A" "a#B" "a#C" "a#D" "a#E" "a#F" "a#G" "a#H" "a#I"
[10] "a#J" "a#K" "a#L" "a#M" "a#N" "a#O" "a#P" "a#Q" "a#R"
[19] "a#S" "a#T" "a#U" "a#V" "a#W" "a#X" "a#Y" "a#Z"
> names
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N"
[15] "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
\;\\\;\\\;
sapply 简化版lapply
l <- list(
a=c(1:5),
b=c(6:10)
)
sapply(l,mean,simplify=T) #T表示不用unlist了
a b
3 8
\;\\\;\\\;
mapply 多data计算
#可以传入多个data
mapply(
sum,
list(a=1,b=2,c=3),
list(a=10,b=20,d=30)
)
mapply(
sum,
list(a=1,b=2,c=3),
list(a=10,b=20,c=30),
list(a=15,b=25,d=35)
)
mapply(
function(x,y) x%%y,
c(1:50),
c(1:10)
)
\;\\\;\\\;
par
bty 图的围边
par(mfrow=c(3,2))
bty <- c('o','l','7','c',']','u') #bty是图的围边
for(i in 1:6){
par(bty=bty[i])
plot(1:5,main=paste('bty=',bty[i],sep=':'))
}

\;\\\;\\\;
fg/bg 前景/背景
前景就是图的围边
par(
fg='red', #前景(边框)
bg='white' #背景
)
par(mfrow=c(1,1),
font=3, #1普通,2粗体,3斜体,4粗斜体
family='serif') #字体serif就是Times New Roman
#气泡图
plot(1:5,
main="Times New Roman",
cex=1:5, #cex是点的大小
col=1:5 #col是颜色
)

\;\\\;\\\;
las 坐标轴横竖数字
#las表示坐标轴数字的横竖
par(mfrow=c(2,2))
for( i in 0:3){
par(las=i)
plot(1:5,main=paste('las=',i))
}

\;\\\;\\\;
lty 线的类型
par(mfrow=c(2,3))
for( i in 1:6){
par(lty=i,lwd=i/2) #lwd默认是1,线段粗细
plot(1:5,type='l',main=paste('lty=',i),sub='subtitle')
}

\;\\\;\\\;
边栏推荐
- ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
- XSS shooting range (3) prompt to win
- Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- Smartcom Programming Level 4 - Magic Academy Lesson 6
- 递归实现汉诺塔问题
- 进程间通信
- el-image tag doesn't work after binding click event
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- 高等数学---第九章二重积分
猜你喜欢

binom二项分布,

数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开

Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held

Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held

MySQL数据库备份

重磅 | 开放原子校源行活动正式启动

已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined

高等数学---第九章二重积分

(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
随机推荐
【线性神经网络】softmax回归
Explanation of
Exsl file preview, word file preview web page method
PCL 计算点云坐标最值及其索引
MySQL基础操作
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
The BP neural network
[shell basics] determine whether the directory is empty
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
binom二项分布,
重磅 | 开放原子校源行活动正式启动
STM32HAL库修改Hal_Delay为us级延时
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
Recursive implementation of the Tower of Hanoi problem
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
el-image标签绑定点击事件后没有有用
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块