编写函数

本文详细探讨了R语言中的函数特性,包括args函数参数检查、函数内变量的作用范围、自编函数的创建与调用,以及如何从复杂数据结构中提取特定属性。通过实例展示了如何利用自编函数处理list数据,提取所需信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. args 函数

Argument List of a Function

args(mean)

查看 mean()函数的参数设置要求。

2. 函数内的变量

Variables that are defined inside a function are not accessible outside that function.
函数里面定义的变量在函数外面无法调用。

R function cannot change the variable that you input to that function.
函数不能改变函数外面已有变量的值。

3. 自编函数

背景:logs是一个list,里面的每一个元素也是list。见下面。

List of 96
 $ :List of 3
  ..$ success  : logi TRUE
  ..$ details  :List of 1
  .. ..$ message: chr "check"
  ..$ timestamp: POSIXct[1:1], format: "2015-09-14 23:01:07"
 $ :List of 3
  ..$ success  : logi TRUE
  ..$ details  :List of 1
  .. ..$ message: chr "all good"
  ..$ timestamp: POSIXct[1:1], format: "2015-09-15 00:00:13"
 $ :List of 3
  ..$ success  : logi TRUE
  ..$ details  :List of 1
  .. ..$ message: chr "check"
  ..$ timestamp: POSIXct[1:1], format: "2015-09-15 01:00:43"

任务:提取任何一个 list 里面的任何一个 property

# 1. 自编函数
extract_info <- function(x, property) {
  info <- c()
  for (log in x) {
   info <- c(info, log[[property]])
  }
  return(info)
}

# 2. 调用函数
# Call extract_info() on logs, set property to "timestamp"
extract_info(logs, "timestamp")

# Call extract_info() on logs, set property to "success"
extract_info(logs, "success")

特别需要注意的点1:

如果不写在函数体里面,单纯的想要提取logs 这个list 里面的内容,可以使用 $ 符号,例如:

> logs[[1]]$timestamp
[1] "2015-09-14 23:01:07 UTC"

如果写在函数体里面,不能使用 $ 符号,必须使用 [[]]。上文的命令中 for循环内部的语句不能写成:

   info <- c(info, log$property)

解释:You cannot use the $ notation if the element you want to select is a variable and not the actual name of a list。
如果你想要选择的元素是一个变量,而不是列表的名称,那么不能使用 $ 符号。

特别需要注意的点2:

函数调用时,第二个参数要加双引号。

extract_info(logs, "timestamp")

4. 自编函数复杂化的过程

代码来源:datacamp 课程
intermediate-r-practice

# 1. for loop, extract "timestamp" information in "logs" list.
info <- c()
for (log in logs) {
  info <- c(info, log$timestamp)
}

# 2. step 1 function, substitute for "list"
extract_info <- function(x) {
  info <- c()
  for (log in x) {
    info <- c(info, log$timestamp)
  }
  return(info)
}

extract_info(logs)

# 3. step 2 function, substitute for "list" and "property"
extract_info <- function(x, property) {
  info <- c()
  for (log in x) {
    info <- c(info, log[[property]])
  }
  return(info)
}

extract_info(logs, "timestamp")

# 4. step 3 funtion, set a default "property" value
extract_info <- function(x, property = "success") {
  info <- c()
  for (log in x) {
    info <- c(info, log[[property]])
  }
  return(info)
}

extract_info(logs)
extract_info(logs, property = "timestamp")

# 5. step 4 function, select part of list
extract_info <- function(x, property = "success", include_all = TRUE) {
info <- c()
for (log in x) {
  if (include_all || !log$success){
    info <- c(info, log[[property]])
  }
}
return(info)
}

extract_info(logs) # select all list elements' "success"
extract_info(logs, include_all = FALSE) # select list elements which  "success" is false 

# 6. step 5 function, select a nested list elements
extract_info(logs, property = c("details", "message"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值