首页 > 其他分享 >R语言中switch语句结构

R语言中switch语句结构

时间:2022-08-27 14:12:11浏览次数:58  
标签:语句 plot 语言 10 16 pch switch col cex

 

001、

feelings <- c("sad", "afraid")
for (i in feelings) {
  print(
    switch (i,                              ## 依次匹配i
      happy = "a am glad you are haay",
      afraid = "there is nothing to fear",
      sad = "cheer up",
      angry = "calm down now"
    )
  )  
}

 

 

002、可以匹配代码块

par(mfrow = c(1,3))
switch ("a",                      
        a = {plot(1:10, cex = 3, pch = 16, col = "red")},      ## 匹配代码块
        b = {plot(1:10, cex = 3, pch = 16, col = "blue")},
        c = {plot(1:10, cex = 3, pch = 16, col = "purple")}
)

switch ("b",                      
        a = {plot(1:10, cex = 3, pch = 16, col = "red")},
        b = {plot(1:10, cex = 3, pch = 16, col = "blue")},
        c = {plot(1:10, cex = 3, pch = 16, col = "purple")}
)

switch ("c",                      
        a = {plot(1:10, cex = 3, pch = 16, col = "red")},
        b = {plot(1:10, cex = 3, pch = 16, col = "blue")},
        c = {plot(1:10, cex = 3, pch = 16, col = "purple")}
)

 

 

003、默认的匹配关键字是EXPR

xx <- switch (
  EXPR  = "c", 
  a = c("kkk", "ppp", "qqq"),
  b = c("iii", "yyy", "dfd"),
  c = c("yyy", "jjj", "www")
)
xx

xx <- switch (
  EXPR  = "a",
  a = c("kkk", "ppp", "qqq"),
  b = c("iii", "yyy", "dfd"),
  c = c("yyy", "jjj", "www")
)
xx

 

 

标签:语句,plot,语言,10,16,pch,switch,col,cex
From: https://www.cnblogs.com/liujiaxin2018/p/16630472.html

相关文章