当前位置:网站首页>[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
2022-07-31 04:43:00 【Su Nianxin】
前言
RThe language drawing looks terse though,But there are too many function parameters——虽然学了4天,But it's like a week of study,In fact, my current goal is to draw nice pictures,Then it would be nice to have a drawing template.Because the function calls in the book I bought are too concise,df$SODetc. Variable data comes from nowhere,So you have to accumulate templates yourself.
这里存在一些问题,Even if you have learned knowledge, you will have an impression even if you forget it,But if you forget too much, you have to learn it again!Like picking it up in months or years,used several timesmatlab,Every time I have to retype the syntax code and drawing code for a day or two,才能找到感觉……
目录
\;\\\;\\\;
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分组,将对应的vadd up the elements in
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分组,将对应的matadd up the elements in
#1 2
#3 8
tapply(mat,group,mean) #按group分组,将对应的matThe elements in are added together to find the mean
\;\\\;\\\;
lapply Multi-element computations are grouped by row
需要转成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 The border of the figure
par(mfrow=c(3,2))
bty <- c('o','l','7','c',']','u') #btyis the border of the graph
for(i in 1:6){
par(bty=bty[i])
plot(1:5,main=paste('bty=',bty[i],sep=':'))
}
\;\\\;\\\;
fg/bg 前景/背景
The foreground is the border of the graph
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 Coordinate axis horizontal and vertical numbers
#lasIndicates the horizontal and vertical axis numbers
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')
}
\;\\\;\\\;
边栏推荐
- (tree) Last Common Ancestor (LCA)
- 专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
- Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
- Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- View source and switch mirrors in two ways: npm and nrm
- el-image标签绑定点击事件后没有有用
- ERP生产作业控制 金蝶
- "A daily practice, happy water problem" 1331. Array serial number conversion
猜你喜欢
【云原生】DevOps(五):集成Harbor
MySQL数据库必会的增删查改操作(CRUD)
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
MySQL数据库增删改查(基础操作命令详解)
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
Unity Fighter
[C language] General method of base conversion
【C语言进阶】文件操作(一)
Component pass value provide/inject
随机推荐
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
XSS shooting range (3) prompt to win
Bubble sort, selection sort, insertion sort, binary search directly
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
[Paper reading] Mastering the game of Go with deep neural networks and tree search
Thinking about data governance after Didi fines
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
mysql基础知识(二)
el-image tag doesn't work after binding click event
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
open failed: EACCES (Permission denied)
pom文件成橘红色未加载的解决方案
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
剑指offer专项突击版第15天
已解决:不小心卸载pip后(手动安装pip的两种方式)
Gaussian distribution and its maximum likelihood estimation