Let's learn R programming together - the application of the do.call function

The R language is composed of basic functions. Skillful and flexible application of these basic functions will help us better learn R packages and programming. This column may not be of interest to many people, but it is very helpful for self-improvement. Interested friends come and learn together. Today we will introduce the usage of the do.call function.
insert image description here
The do.call function is a very practical, powerful and flexible function. Let’s first look at its function description. do.call constructs and executes a function call according to a name or function and the parameter list to be passed to it.
Seeing this, we can understand that the do.call function is generally used in lists, but data frames can also be applied. It can pass names or functions to lists and execute them, saving our tedious process. Let's demonstrate it below. We first generate a data

tmp <- expand.grid(letters[1:2], 1:3, c("+", "-"))
head(tmp)
##   Var1 Var2 Var3
## 1    a    1    +
## 2    b    1    +
## 3    a    2    +
## 4    b    2    +
## 5    a    3    +
## 6    b    3    +

We use a paste function to glue variables together. In the current environment, it is the same with or without quotation marks

do.call("paste", c(tmp, sep = ""))
##  [1] "a1+" "b1+" "a2+" "b2+" "a3+" "b3+" "a1-" "b1-" "a2-" "b2-" "a3-" "b3-"
do.call(paste, c(tmp, sep = ""))
##  [1] "a1+" "b1+" "a2+" "b2+" "a3+" "b3+" "a1-" "b1-" "a2-" "b2-" "a3-" "b3-"

Prescriptive bonding is also possible

a<-list(c("a","b","c"),c(c("1","2","3")))
do.call(paste, a, quote = TRUE)
## [1] "a 1" "b 2" "c 3"

Assuming there is a list of data, we want to convert it into a matrix, which can be easily done using the do.call function

vectors <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))
combined_matrix <- do.call(rbind, vectors)
combined_matrix
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Using the do.call function combined with the lapply function, you can list multiple data for statistical operations. We first generate a list of data

data_frames <- list(
  data.frame(a = 1:3), 
  data.frame(a = 4:6), 
  data.frame(a = 7:9)
)

insert image description here
Perform multiple data mean calculations

mean_results <- do.call(
  rbind, 
  lapply(data_frames, function(df) mean(df$a))
)
mean_results
##      [,1]
## [1,]    2
## [2,]    5
## [3,]    8

You can set conditions to operate on the data in the list. When the length of vectors is 2, the data is added, and when it is equal to 3, it is multiplied

vectors <- list(c(1, 2, 3), c(4, 5, 6))
if (length(vectors) == 2) {
    
    
  result <- do.call("+", vectors)
} else if (length(vectors) == 3) {
    
    
  result <- do.call("*", vectors)
}
result
## [1] 5 7 9

If it is not list data, we need to add a list to operate on it

a<-c(1:5)
a
## [1] 1 2 3 4 5
do.call(mean, list(a))
## [1] 3

There is also an envir environment parameter in the do.call function. If you use this parameter, it indicates that you are calling the function from this environment, and the function used needs to be enclosed in double quotation marks. Suppose A and function f in the current environment

A <- 2
f <- function(x) print(x^2)

Recreate A and an f in a new environment

env <- new.env()
assign("A", 10, envir = env)
assign("f", f, envir = env)
f <- function(x) print(x)

Let's first look at the new environment A and f are completely different from the current environment, the current environment A=2, f=function(x) print(x), in the new environment, A=10, f=function(x) print(x^2)

env$A
## [1] 10
env$f
## function(x) print(x^2)

Let's run the difference between adding and not adding the environment

do.call("f", list(A))  
## [1] 2
do.call("f", list(A), envir = env)
## [1] 4
do.call( f,  list(A), envir = env) 
## [1] 2
do.call("f", list(quote(A)), envir = env) #
## [1] 100
do.call( f,  list(quote(A)), envir = env) # 10
## [1] 10
do.call("f", list(as.name("A")), envir = env) # 100
## [1] 100

It can be seen that adding envir = env, the function f with quotation marks is different, f with quotation marks calls the new environment, and without quotation marks still calls the current environment. Adding quote(A) or as.name("A") to A will call the new environment. If not adding it, it will still call A in the current environment. The call function is the same for this rule.

eval(call("f", A))                      # 2
## [1] 2
eval(call("f", quote(A)))               # 2
## [1] 2
eval(call("f", A), envir = env)         # 4
## [1] 4
eval(call("f", quote(A)), envir = env)  # 100
## [1] 100

There is also writing usage is to allocate data for the function written by myself in R package programming, using the iris data as an example, assuming that I have already written a drawing function and want to allocate data for my own function, I will write a random one

pf<-function(df) {
    
    
  x<-df$Sepal.Length;y<-df$Petal.Width
  plot(x,y,col="red")
}
pf
## function(df) {
    
    
##   x<-df$Sepal.Length;y<-df$Petal.Width
##   plot(x,y,col="red")
## }

Let's cut and distribute the iris data randomly, divide it into 3 data, and put them in the list

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris.split <- split(iris,as.factor(iris$Species))

insert image description here
Batch plotting with do.call

do.call(rbind, lapply(iris.split, pf)
        )

insert image description here
insert image description here
insert image description here
In this way, the picture of multiple data is drawn.

Guess you like

Origin blog.csdn.net/dege857/article/details/131347488