R语言笔记(四):函数

news2024/11/29 8:36:47

文章目录

  • 一、Function basics
    • 1、Creating your own function
    • 2、Function structure
    • 3、Using your created function
    • 4、Multiple inputs
    • 5、Default inputs
  • 二、Return values and side effects
    • 1、Returning more than one thing
    • 2、Side effects
      • Example of side effect: plot
  • 三、Environments and design
    • 1、Environment: what the function can see and do
    • 2、Environment examples


一、Function basics

1、Creating your own function

Call function() to create your own function. Document your function with comments

# get.wordtab.king: get a word table from King's "I Have A Dream" speech
# Input: none
# Output: word table, i.e., vector with counts as entries and associated
#         words as names

get.wordtab.king = function() {
  lines = readLines("https://raw.githubusercontent.com/king.txt")
  text = paste(lines, collapse=" ")
  words = strsplit(text, split="[[:space:]]|[[:punct:]]")[[1]]
  words = words[words != ""]
  wordtab = table(words)
  return(wordtab)
}
  • Input: none
  • Output: word table, i.e., vector with counts as entries and associated words as names

Much better: create a word table function that takes a URL of web

# get.wordtab.from.url: get a word table from text on the web
# Input:
# - str.url: string, specifying URL of a web page 
# Output: word table, i.e., vector with counts as entries and associated
#   words as names

get.wordtab.from.url = function(str.url) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split="[[:space:]]|[[:punct:]]")[[1]]
  words = words[words != ""]
  wordtab = table(words)
  return(wordtab)
}
  • Input:

    • str.url: string, specifying URL of a web page
  • Output: word table, i.e., vector with counts as entries and associated words as names

2、Function structure

The structure of a function has three basic parts:

  • Inputs (or arguments): within the parentheses of function()
  • Body (code that is executed): within the braces {}
  • Output (or return value): obtained with function return()
  • (optional) Comments: description of functions by comments
# get.wordtab.from.url: get a word table from text on the web
# Input:
# - str.url: string, specifying URL of a web page 
# Output: word table, i.e., vector with counts as entries and associated
#   words as names

get.wordtab.from.url = function(str.url) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split="[[:space:]]|[[:punct:]]")[[1]]
  words = words[words != ""]
  wordtab = table(words)
  return(wordtab)
}

3、Using your created function

Our created functions can be used just like the built-in ones

# Using our function
king.wordtab.new = get.wordtab.from.url(
  "https://raw.githubusercontent.com/mxcai/BIOS5801/main/data/king.txt")
all(king.wordtab.new == king.wordtab)
## [1] TRUE

# Revealing our function's definition
get.wordtab.from.url
## function(str.url) {
## lines = readLines(str.url)
## text = paste(lines, collapse=" ")
## words = strsplit(text, split="[[:space:]]|[[:punct:]]")[[1]]
## words = words[words != ""]
## wordtab = table(words)
## return(wordtab)
## }

4、Multiple inputs

Our function can take more than one input

# get.wordtab.from.url: get a word table from text on the web
# Inputs:
# - str.url: string, specifying URL of a web page 
# - split: string, specifying what to split on
# Output: word table, i.e., vector with counts as entries and associated
#   words as names

get.wordtab.from.url = function(str.url, split) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split=split)[[1]]
  words = words[words != ""]
  table(words)
}
  • Inputs:
  • str.url: string, specifying URL of a web page
  • split: string, specifying what to split on
  • Output: word table, i.e., vector with counts as entries and associated words as names

5、Default inputs

Our function can also specify default values for the inputs (if the user doesn’t specify an input in the function call, then the default value is used)

# get.wordtab.from.url: get a word table from text on the web
# Inputs:
# - str.url: string, specifying URL of a web page 
# - split: string, specifying what to split on. Default is the regex pattern
#   "[[:space:]]|[[:punct:]]"
# - convert2lower: Boolean, TRUE if words should be converted to lower case before
#   the word table is computed. Default is TRUE
# Output: word table, i.e., vector with counts as entries and associated
#   words as names

get.wordtab.from.url = function(str.url, split="[[:space:]]|[[:punct:]]", 
                                convert2lower=TRUE) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split=split)[[1]]
  words = words[words != ""]
  # Convert to lower case, if we're asked to
  if (convert2lower) words = tolower(words)
  table(words)
}

二、Return values and side effects

1、Returning more than one thing

R doesn’t let your function have multiple outputs, but you can return a list

When creating a function in R, though you cannot return more than one output, you can return a list. This (by definition) can contain an arbitrary number of arbitrary objects

  • Inputs:
    • str.url: string, specifying URL of a web page
    • split: string, specifying what to split on. Default is the regex pattern “[[:space:]]|[[:punct:]]”
    • convert2lower: Boolean, TRUE if words should be converted to lower case before the word table is computed. Default is TRUE
    • keep.nums: Boolean, TRUE if words containing numbers should be kept in the word table. Default is FALSE
  • Output: list, containing word table, and then some basic numeric summaries
get.wordtab.from.url = function(str.url, 
                                split="[[:space:]]|[[:punct:]]",
                                convert2lower=TRUE, keep.nums=FALSE) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split=split)[[1]]
  words = words[words != ""]
  
  # Convert to lower case, if we're asked to
  if (convert2lower) {
    words = tolower(words)
  }
  
  # Get rid of words with numbers, if we're asked to
  if (!keep.nums) {
    words = grep("[0-9]", words, invert=TRUE, value=TRUE)
  }
  
  # Compute the word table
  wordtab = table(words)
  return(list(wordtab=wordtab,
              number.unique.words=length(wordtab),
              number.total.words=sum(wordtab),
              longest.word=words[which.max(nchar(words))]))
}
# King's "I Have A Dream" speech 
king.wordtab = get.wordtab.from.url(
  "https://raw.githubusercontent.com/king.txt")
lapply(king.wordtab, head)

## $wordtab
## words
## a able again ago ahead alabama
## 37 8 2 1 1 3
##
## $number.unique.words
## [1] 528
##
## $number.total.words
## [1] 1631
##
## $longest.word
## [1] "discrimination"

2、Side effects

A side effect of a function is something that happens as a result of the function’s body, but is not returned. Examples:

  • Printing something out to the console
  • Plotting something on the display
  • Saving an R data file, or a PDF, etc.

Example of side effect: plot

  • get.wordtab.from.url: get a word table from text on the web
  • Inputs:
    • str.url: string, specifying URL of a web page
    • split: string, specifying what to split on. Default is the regex pattern “[[:space:]]|[[:punct:]]”
    • convert2lower: Boolean, TRUE if words should be converted to lower case before the word table is computed. Default is TRUE
    • keep.nums: Boolean, TRUE if words containing numbers should be kept in the word table. Default is FALSE
    • plot.hist: Boolean, TRUE if a histogram of word lengths should be plotted as a side effect. Default is FALSE
  • Output: list, containing word table, and then some basic numeric summaries
get.wordtab.from.url = function(str.url, split="[[:space:]]|[[:punct:]]",
                                convert2lower=TRUE, keep.nums=FALSE, plot.hist=FALSE) {
  lines = readLines(str.url)
  text = paste(lines, collapse=" ")
  words = strsplit(text, split=split)[[1]]
  words = words[words != ""]
  
  # Convert to lower case, if we're asked to
  if (convert2lower) words = tolower(words)
  
  # Get rid of words with numbers, if we're asked to
  if (!keep.nums) 
    words = grep("[0-9]", words, invert=TRUE, value=TRUE)
  
  # Plot the histogram of the word lengths, if we're asked to
  if (plot.hist) 
    hist(nchar(words), col="lightblue", breaks=0:max(nchar(words)),
         xlab="Word length")
  
  # Compute the word table
  wordtab = table(words)
  
  return(list(wordtab=wordtab,
              number.unique.words=length(wordtab),
              number.total.words=sum(wordtab),
              longest.word=words[which.max(nchar(words))]))
}
# King's speech
king.wordtab = get.wordtab.from.url(
  str.url="https://raw.githubusercontent.com/mxcai/BIOS5801/main/data/king.txt",
  plot.hist=TRUE)

在这里插入图片描述


三、Environments and design

1、Environment: what the function can see and do

  • Each function generates its own environment
  • Variable names in function environment override names in the global environment
  • Internal environment starts with the named arguments
  • Assignments inside the function only change the internal environment
  • Variable names undefined in the function are looked for in the global environment

2、Environment examples

  • Variable names here override names in the global environment

    • y is 2 in the global environment
    • y is 10 in the function environment, and only exists when the function is under execution
  • Variable assignments inside the function environment would (generally) not change the variable in the global environment

    • x remains to be 1 in the global environment
x <- 1
y <- 2
addone = function(y) { 
  x = 1+y
  x 
}
addone(10)
## [1] 11

y
## [1] 2

x
## [1] 1
  • Variable names undefined in the function are looked for in the global environment
circle.area = function(r) { pi*r^2 }
circle.area(1:3)
## [1] 3.141593 12.566371 28.274334

true.pi = pi # to back up the sanity
pi = 3 
circle.area(1:3)
## [1] 3 12 27

pi = true.pi # Restore sanity
circle.area(1:3)
## [1] 3.141593 12.566371 28.274334

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2227289.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于熵权法的TOPSIS模型

基于熵权法的TOPSIS模型 1. 简介 数学建模可以结合 熵权法 和 T O P S I S TOPSIS TOPSIS 法各自的特点&#xff0c;进行评价&#xff0c;这种组合模型的使用在数学建模比赛中使用的非常多。 在 2023 美赛 O 奖中就有使用该方法的&#xff0c;往年国赛国奖中也有 2. 熵权法介…

Chromium HTML5 新的 Input 类型tel对应c++

一、Input 类型: tel <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>test</title> </head> <body><form action"demo-form.php">电话号码: <input type"tel" name…

Cesium基础-(Entity)-(point)

** 里边包含Vue、React框架代码 ** 1、point 点 效果&#xff1a; Cesium中点(Point)的详细解读如下&#xff1a; Entity API简介&#xff1a; Cesium提供了Entity API&#xff0c;它是一个高级别的数据驱动的API&#xff0c;用于管理一组相关性的可视化对象。Entity API使…

宠物空气净化器有哪些功能呢?优品宠物空气净化器使用体验分享

前段时间&#xff0c;我朋友家生二胎了&#xff0c;为了家里的宝宝&#xff0c;准备入手一台空气净化器。跑来问我&#xff0c;让我推荐一款能净化空气的空气净化器。让他描述描述需求&#xff0c;才知道&#xff0c;他家除了新添了二胎外&#xff0c;最近还养了一只猫&#xf…

学习私服并配置到项目中

下载地址 一、安装 1.将下载好的压缩包进行解压 2.进人bin目录下cmd&#xff0c;执行 nexus.exe /run nexus3.访问nexus界面 在浏览器中输入&#xff1a;http://localhost:8081 4.登录 Username为admin Password根据提示到安装包中去找 二、本地仓库与私服连接 1.创建…

安信金业:18k和24k黄金的区别

黄金&#xff0c;作为永恒的贵金属&#xff0c;在人们心中拥有着独特的地位。而18K金和24K金&#xff0c;作为两种常见的黄金饰品&#xff0c;却常常让消费者感到困惑。今天&#xff0c;我们就来深入探讨18K金和24K金之间的区别&#xff0c;帮助你更好地理解它们各自的特性和价…

基于Qt的多线程并行和循序运行实验Demo

致谢&#xff08;Acknowledgement&#xff09;&#xff1a; 感谢Youtube博主Qt With Ketan与KDAB精心录制的Qt多线程处理应用教程&#xff0c;感谢Bilibili博主爱编程的大丙对Qt多线程与线程池内容深入浅出的讲解。 一、计算机线程相关概念 线程概念[1]&#xff1a; 在计算机科…

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-27

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-27 目录 文章目录 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-27目录1. Large Language Model-based Augmentation for Imbalanced Node Classification on Text-Attributed Graphs摘要研究背…

ESD防静电监控系统提高3C电子厂的稳定生产

在当今信息技术迅速发展的时代&#xff0c;3C电子产品的生产需求日益增加。然而&#xff0c;随着生产规模的扩大和技术的不断进步&#xff0c;生产过程中面临的挑战也日益凸显。静电放电作为影响电子产品质量与可靠性的一个重要因素&#xff0c;受到越来越多厂商的重视。ESD防静…

matlab线性度计算程序

matlab线性度计算程序 环境 matlab2023a ads2020 原理 其中f(v)是曲线&#xff0c;fmax是f(v)的最大值&#xff0c;fmin是f(v)的最小值&#xff0c;vmax为fmax对应v值&#xff0c;vmin为fmin对应v值。 L∆fmax/(fmax-fmin) (1) ∆fmaxmax⁡[f(v)-[fmin-K*(v-vmin)]] (2) K(…

BUUCTF之web篇

第一题 [极客大挑战 2019]EasySQL 打开靶机后可以看到这是一个登陆的页面 我们可以尝试两种方式登录 弱口令爆破&#xff08;burpsuite&#xff09; 通过SQL注入里的万能密码来跳过账户和密码验证的过程 这里就需要万能密码aor true # 在这里单引号的作用是结束用户名或者密码…

Vue笔记-element ui中关于table的前端分页

对于 Element UI 表格的前端分页&#xff0c;可以在组件中使用 JavaScript 来实现数据的分页显示&#xff0c;而不必从后端获取已分页的数据。以下是一个简单的示例&#xff0c;演示如何在前端进行 Element UI 表格的分页&#xff1a; <template><div><el-tabl…

Tenda路由器 敏感信息泄露

0x01 产品描述&#xff1a; ‌ Tenda路由器‌是由深圳市吉祥腾达科技有限公司&#xff08;Tenda&#xff09;生产的一系列网络通信产品。Tenda路由器以其高性能、高性价比和广泛的应用场景而闻名&#xff0c;适合家庭、办公室和各种网络环境。0x02 漏洞描述&#xff1a…

助力AI智能化时代:全国产化飞腾FT2000+/64+昇腾310B服务器主板

在信息技术快速发展的今天&#xff0c;服务器作为数据处理和存储的核心设备&#xff0c;肩负着越来越重要的使命。全国产化的服务器主板&#xff0c;采用飞腾FT2000/64核处理器&#xff0c;搭配华为昇腾310的AI芯片&#xff0c;提供卓越的性能与可靠性。 核心配置&#xff0c;强…

SpringMVC学习(3)

目录 一、控制器Controller 二、RESTful风格 2.1 实际应用 三、结果跳转方式 3.1 ModelAndView 3.2 SpringMVC 3.2.1 无需视图解析器 3.2.2 需要视图解析器 3.3 ServletAPI 四、数据处理 4.1 处理提交数据 4.1.1 提交的域名称和处理方法的参数名一致 4.1.2 提交的…

淘宝商品评价API的获取与应用

随着电子商务的飞速发展&#xff0c;商品评价成为消费者做出购买决策的重要依据之一。对于电商平台而言&#xff0c;如何有效地管理和利用这些评价数据&#xff0c;提升用户体验和销售额&#xff0c;成为一个重要课题。淘宝作为国内最大的电商平台之一&#xff0c;其商品评价AP…

背包九讲——分组背包问题

目录 分组背包问题 问题定义 解题算法 问题解法 朴素解法&#xff1a; 一维优化解法 变式题型 背包问题第六讲——分组背包问题 背包问题是一类经典的组合优化问题&#xff0c;通常涉及在限定容量的背包中选择物品&#xff0c;以最大化某种价值或利益。问题的一般描述是…

模型 五遍沟通法(企业管理)

系列文章 分享 模型&#xff0c;了解更多&#x1f449; 模型_思维模型目录。确保信息准确&#xff0c;促进共识。 1 五遍沟通法的应用 1.1 五遍沟通模型案例&#xff1a;新员工入职培训 一家日本科技公司新招聘了一批员工&#xff0c;人力资源部门需要确保新员工对公司的文化…

学习笔记——路由——IP组播-PIM-DM(密集模式)前言概述

7、PIM-DM(密集模式) (1)前言 PIM-DM(PIM Dense Mode)使用“推(Push)模式”转发组播报文&#xff0c;一般应用于组播组成员规模相对较小、相对密集的网络。 在实现过程中&#xff0c;它会假设网络中的组成员分布非常稠密&#xff0c;每个网段都可能存在组成员。当有活跃的组…

Oracle自动处理表空间不足脚本

关注过我的朋友们应该知道我分享过一些常用的监控脚本&#xff0c;其中最常用的就是监控表空间使用率的脚本&#xff0c;具体可以参考如下链接​&#xff1b; oracle常用监控脚本&#xff08;纯干货&#xff0c;没有EMCC,ZABBIX也不怕&#xff09;_oracle 监控及日常处理脚本-…