• R语言实战


    4. 控制流

    - 语句(statement)是一条单独的R语句或一组复合语句(包含在花括号{}中的一组R语句,使用分号分隔);

    - 条件(cond)是一条最终被解析为真(TRUE)或假(FALSE)的表达式;

    - 表达式(expr)是一条数值或字符串的求值语句;

    - 序列(seq)是一个数值或字符串序列

    4.1 重复和循环

    > for (var in seq) statement
    > 
    > for (i in 1:10) print("Hello")
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    > 
    > while (cond) statement
    > 
    > i <- 10
    > while (i > 0) {print("Hello"); i <- i - 1}
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    [1] "Hello"
    > 
    

    4.2 条件执行

    > if (cond) statement
    > if (cond) statement1 else statement2
    > 
    > if (is.character(grade)) grade <- as.factor(grade)
    > if (!is.factor(grade)) grade <- as.factor(grade) else print("Grade already is a factor")
    > 
    > 
    > ifelse(cond, sttement1, statement2)
    > 
    > ifelse(score > 0.5, print("Passed"), print("Failed"))
    > outcome <- ifelse(score>0.5, "Passed", "Failed")
    > 
    > switch(expr, ...)
    > 
    
    > feelings <- c("sad", "afraid")
    > for (i in feelings)
    +   print(
    +     switch(i,
    +       happy = "I am glad you are happy",
    +       afraid = "There is nothing to fear",
    +       sad = "Cheer up",
    +       angry = "Calm down now"
    +     )
    +   )
    [1] "Cheer up"
    [1] "There is nothing to fear"
    > 
    

    5. 用户自编函数

    > myfunction <- function(arg1, arg2, ...){
    +   statements
    +   return(object)
    + }
    > 
    > # 选择性地给出参数统计量(均值和标准差)
    > # 和非参数统计量(中位数和绝对中位数)
    > mystets <- function(x, parametric=TRUE, print=FALSE){
    +   if (parametric) {
    +     center <- mean(x); spread <- sd(x)
    +   } else {
    +     center <- median(x); spread <- mad(x)
    +   }
    +   if (print & parametric) {
    +     cat ("Mean=", center, "
    ", "SD=", spread, "
    ")
    +   } else if (print & !parametric) {
    +     cat("Median=", center, "
    ", "MAD=", spread, "
    ")
    +   }
    +   result <- list(center=center, spread=spread)
    +   return(result)
    + }
    > 
    > set.seed(1234)
    > x <- rnorm(500)
    > 
    > y <- mystets(x)
    > 
    > y <- mystets(x, parametric=FALSE, print=TRUE)
    Median= -0.021 
    MAD= 1 
    > 
    
    > mydate <- function(type="long"){
    +   switch(type,
    +     long = format(Sys.time(), "%A %B %d %Y"),
    +     short = format(Sys.time(), "%m-%d-%y"),
    +     cat(type, "is not a recognized type
    ")
    +   )
    + }
    > 
    > mydate("log")
    log is not a recognized type
    > mydate("long")
    [1] "Saturday September 16 2017"
    > mydate("short")
    [1] "09-16-17"
    > mydate()
    [1] "Saturday September 16 2017"
    > 
    
  • 相关阅读:
    记录一下我的2017年阅读书单
    大型网站技术架构(二)--大型网站架构演化
    《大型网站技术架构:核心原理与案例分析》读书笔记系列
    过年了,别忘记给家人的礼物
    2017总结
    MyBatis + MySQL返回插入成功后的主键id
    微信公众号问题:{"errcode":40125,"errmsg":"invalid appsecret, view more at http://t.cn/LOEdzVq, hints: [ req_id: kL8J90219sg58 ]"}
    git删除本地分支
    Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存
    Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十一)redis密码设置、安全设置
  • 原文地址:https://www.cnblogs.com/wnzhong/p/7525894.html
Copyright © 2020-2023  润新知