R中的函数

Function

To understand computations in R, two slogans are helpful:
1. Everything that exists is an object.
2. 2. Everything that happens is a function call.

Something for fun

Sys.date()will give us today's date based on our computer system.

to get the average of the elements in a vector, we have
mean()
try something like mean(c(2, 4, 5))

Inputs to functions are often called arguments. Providing arguments to a function is also sometimes called passing arguments to that function.
Arguments you want to pass to a function go inside the function's parentheses.

How to Assign a Function

You're about to write your first function! Just like you would assign a value to a variable with the assignment operator, you assign functions in the following way:

function_name <- function(arg1, arg2){
 Manipulate arguments in some way
 Return a value
}

The "variable name" you assign will become the name of your function. arg1 and
arg2 represent the arguments of your function. You can manipulate the arguments
you specify within the function. After sourcing the function, you can use the function by typing:

function_name(value1, value2)

Below we will create a function called boring_function. This function takes the argument x as input, and returns the value of x without modifying it.

 boring_function <- function(x){
    x
}

The last R expression to be evaluated in a function will become the return value of that function.
Be sure to save this script and type submit() in the console(控制台) after you make your changes.

If you want to see the source code for any function, just type the function name without any arguments or parentheses. Let's try this out with the function you just created.

Let's write a function to calculate the average

my_mean <- function(my_vector) {
  S <- sum(my_vector)
  L <- length(my_vector)
  S/L
}

don't forget to save and submit it the console.

Setting default argument

increment <- function(number, by = 1){
    number + by
 }

If you take a look in between the parentheses you can see that I've set "by" equal to 1. This means that the "by" argument will have the default value of 1.

However if I want to provide a value for the "by" argument I still can! The expression: increment(5, 2) will evaluate to 7.

  1. to see the name of a function, type its name without parentheses.
  2. to only see the arguments use
    args(function_name)

according to the function args(),we see that we can pass functions as arguments as well! This is a very powerful concept.

pass functions as arguments in a funtion

evaluate <- function(func, dat){
  func(dat)
}

it should works as following

  1. evaluate(sum, c(2, 4, 6)) should evaluate to 12
  2. evaluate(median, c(7, 40, 9)) should evaluate to 9
  3. evaluate(floor, 11.1) should evaluate to 11

Use ellipses to be an argument

telegram <- function(...){
  paste("START", ..., "STOP")
}

or we go further

mad_libs <- function(...){
  args <- list(...)
  place = args[["place"]]
  adjective = args[["adjective"]]
  noun = args[["noun"]]
  # Don't modify any code below this comment.
  # Notice the variables you'll need to create in order for the code below to
  # be functional!
  paste("News from", place, "today where", adjective, "students took to the streets in protest of the new", noun, "being installed on campus.")
}

this is really tricky, go back to use swirl() if you're still confused about these line of code and find some details of importance.

To Create my Binary Operators(左右运算符)

Let's say I wanted to define a binary operator that multiplied two numbers and then added one to the product. An implementation of that operator is below:

 "%mult_add_one%" <- function(left, right){ **# Notice the quotation marks!**
  left * right + 1
}

then we have
4 %mult_add_one% 5 == 21 is TRUE

你可能感兴趣的:(R中的函数)