Creating Functions and Iterations

Basics

rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE, finite = TRUE) #The option finite = TRUE to range() will drop all non-finite elements, and NA is a non-finite element.
  y <- (x - rng[1]) / (rng[2] - rng[1])
  y[y == -Inf] <- 0
  y[y == Inf] <- 1
  y
}

rescale01(c(Inf, -Inf, 0:5, NA))
#> [1] 1.0 0.0 0.0 0.2 0.4 0.6 0.8 1.0  NA

#name, argument and body are needed for a function
#the last expression is the value that returns by default;use `return()` to specially return

commas <- function(...) stringr::str_c(..., sep = ", ", collapse = ", ")
commas(letters[1:10])
#use `...` to set the input arbitrary values

ifelse

if (condition) {
  # code executed when condition is TRUE
} else {
  # code executed when condition is FALSE
}
#`else{}` can be omitted`

#multiple conditions
x <- 3L
if (identical(x, 2L)) {
  cat("x is integer 2")
} else if (identical(x, 3L)) {
  cat("x is integer 3")
} else {
  cat("x is neither integer 2 nor integer 3")
}

#a simplified version
ifelse(cond,s1,s2) #if cond is T,execute s1,else execute s2

map

##use `map()` to directly compute the values and form a list
df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

map(df, mean) # the second parameter denotes the function it uses
map(df, median)

#`map_dbl()`,`map_int()` and so on form a vector

##simulate some random normals with different means
mu <- list(5, 10, -3)
mu %>% 
  map(rnorm, n = 5) %>% 
  str()

##simulate some random normals with different means and sds
mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
map2(mu, sigma, rnorm, n = 5) %>% str() #`map2` receives two lists as parameters

##simulate some random normals with different means, sds and numbers of samples
n <- list(1, 3, 5)
mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
args2 <- list(n = n, mean = mu, sd = sigma)
args2 %>% 
  pmap(rnorm) %>% 
  str()

switch(omitted for test)

switch(3,2*3,sd(1:5),runif(3))
#[1] 0.13854046 0.04598363 0.06802463

#the first value denotes which one to execute

for Circulation(omitted for test)

df <- tibble(
  a=rnorm(10),
  b=rnorm(10),
  c=rnorm(10),
  d=rnorm(10)
)

#compute the medians of every column
output <- vector("double",ncol(df)) #preserve space for the vector beforehand
for (i in seq_along(df)) {  #`seq_along()` is similar to `1:length()`,but more safe
  output[[i]] <- median(df[[i]])
}
output
##ncol() returns to the number of columns of the dataset
##in `vector`,the first parameter can be `character`,`integer`...and `list`

#generate 10 numbers derived from the normal distributions which means are -10,0,10,and 100
mn<-c(-10,0,10,100)
nd_list <- vector("list",length(mn))
for (i in seq_along(mn)) {
  nd_list[[i]] <-rnorm(10,mn[[i]])
}
str(nd_list)
##use `str(unlist(nd_list))` to collapse the list into a single vector

while(omitted for test)

while(cond){
#circulation body
}

#find how many tries it takes to get three heads in a row when flipping a coin 
flip <- function() sample(c("T", "H"), 1) # return T or H

flips <- 0
nheads <- 0

while (nheads < 3) {
  if (flip() == "H") {
    nheads <- nheads + 1
  } else {
    nheads <- 0
  }
  flips <- flips + 1
}
flips

Parameter

#create a function to compute t-statistics
sim.t <- function(n,me=0,std=1){
  x <- rnorm(n,me,std)
  (mean(x)-me)/(sd(x)/sqrt(n))
 }

sim.t(10)
sim.t(10,5,2)
#values of parameters are by default the original ones when creating the function;it is practical to input different values if needed 

Special

temp <- seq(-10, 50, by = 5)
cut(temp, c(-Inf, 0, 10, 20, 30, Inf),
  right = FALSE, #To have intervals open on the left (using <)
  labels = c("freezing", "cold", "cool", "warm", "hot")
)
#>  [1] freezing freezing cold     cold     cool     cool     warm     warm    
#>  [9] hot      hot      hot      hot      hot     
#> Levels: freezing cold cool warm hot

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
毕业设计,基于SpringBoot+Vue+MySQL开发的社区医院管理系统,源码+数据库+毕业论文+视频演示 信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的社区医院管理系统实现了病例信息、字典表、家庭医生、健康档案、就诊信息、前台、药品、用户、用户、用户表等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让社区医院管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。 关键字:社区医院管理系统;信息管理,时效性,安全性,MySql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值