zoukankan      html  css  js  c++  java
  • 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"
    > 
    
  • 相关阅读:
    使用Google浏览器做真机页面调试
    JavaScript从作用域到闭包
    用canvas实现一个colorpicker
    你还在为移动端选择器picker插件而捉急吗?
    我是这样写文字轮播的
    高性能JS-DOM
    ExtJs4学习(四):Extjs 中id与itemId的差别
    MongoDB 安装与启动
    UML应用:业务内涵的分析抽象&amp;表达
    MySQL 错误日志(Error Log)
  • 原文地址:https://www.cnblogs.com/wnzhong/p/7525894.html
Copyright © 2011-2022 走看看