Programing

lapply와 do.call의 차이점은 무엇입니까?

lottogame 2020. 7. 2. 07:49
반응형

lapply와 do.call의 차이점은 무엇입니까?


나는 최근에 R을 배우고 있으며 두 가지 기능에 혼동합니다 : lapplydo.call. 그것들은 mapLisp의 기능 과 비슷합니다 . 그러나 왜 다른 이름을 가진 두 가지 기능이 있습니까? 왜 R은 그냥 함수를 사용하지 map않습니까?


Map다른 언어의 map과 비슷한 함수 가 있습니다.

  • lapply X와 동일한 길이의 목록을 리턴합니다. 각 요소는 FUN을 X의 해당 요소에 적용한 결과입니다.

  • do.call 이름이나 함수 및 전달할 인수 목록에서 함수 호출을 구성하고 실행합니다.

  • Map주어진 벡터의 해당 요소에 함수를 적용합니다 ... Common Lisp의 mapcar와 비슷하지만 결과를 단순화하려고 시도하지 않는 Map간단한 래퍼입니다 mapply(그러나 인수는 재활용됩니다). 이후 버전에서는 결과 유형을 일부 제어 할 수 있습니다.


  1. Map 래퍼입니다 mapply
  2. lapply 특별한 경우입니다 mapply
  3. 따라서 Map그리고 lapply많은 경우에 유사합니다.

예를 들면 다음과 같습니다 lapply.

lapply(iris, class)
$Sepal.Length
[1] "numeric"

$Sepal.Width
[1] "numeric"

$Petal.Length
[1] "numeric"

$Petal.Width
[1] "numeric"

$Species
[1] "factor"

그리고 같은 사용 Map:

Map(class, iris)
$Sepal.Length
[1] "numeric"

$Sepal.Width
[1] "numeric"

$Petal.Length
[1] "numeric"

$Petal.Width
[1] "numeric"

$Species
[1] "factor"

do.call함수를 입력으로 취하고 다른 인수를 함수에 뿌립니다. 예를 들어, 목록을 더 간단한 구조로 구성하는 데 널리 사용됩니다 (종종 rbind또는로 cbind).

예를 들면 다음과 같습니다.

x <- lapply(iris, class)
do.call(c, x)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
   "numeric"    "numeric"    "numeric"    "numeric"     "factor" 

lapply리스트에 함수를 적용하고, do.call인수리스트와 함께 함수를 호출합니다. 그것은 나에게 상당히 다른 것처럼 보입니다 ...

목록이있는 예제를 제공하려면 다음을 수행하십시오.

X <- list(1:3,4:6,7:9)

lapply를 사용하면 다음과 같이 목록의 모든 요소의 평균을 얻습니다.

> lapply(X,mean)
[[1]]
[1] 2

[[2]]
[1] 5

[[3]]
[1] 8

do.call "trim"인수가 1 일 것으로 예상되므로 오류가 발생합니다.

반면 rbind에 모든 인수를 행 방향으로 바인딩합니다. X를 행 단위로 바인딩하려면 다음을 수행하십시오.

> do.call(rbind,X)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

을 사용 lapply하면 R은 rbind목록의 모든 요소에 적용 되어 다음과 같이 말도 안됩니다.

> lapply(X,rbind)
[[1]]
     [,1] [,2] [,3]
[1,]    1    2    3

[[2]]
     [,1] [,2] [,3]
[1,]    4    5    6

[[3]]
     [,1] [,2] [,3]
[1,]    7    8    9

Map과 같은 것을 가지려면 ?mapply, 이것이 필요합니다 . 예를 들어 X의 모든 요소의 평균을 얻으려면 다른 트리밍으로 다음을 사용할 수 있습니다.

> mapply(mean,X,trim=c(0,0.5,0.1))
[1] 2 5 8

lapply is similar to map, do.call is not. lapply applies a function to all elements of a list, do.call calls a function where all the function arguments are in a list. So for a n element list, lapply has n function calls, and do.call has just one function call. So do.call is quite different from lapply. Hope this clarifies your issue.

A code example:

do.call(sum, list(c(1, 2, 4, 1, 2), na.rm = TRUE))

and:

lapply(c(1, 2, 4, 1, 2), function(x) x + 1)

In most simple words:

  1. lapply() applies a given function for each element in a list,so there will be several function calls.

  2. do.call() applies a given function to the list as a whole,so there is only one function call.

The best way to learn is to play around with the function examples in the R documentation.


lapply() is a map-like function. do.call() is different. It is used for passing the arguments to a function in list form instead of having them enumerated. For instance,

> do.call("+",list(4,5))
[1] 9

Although there have been many answers, here is my example for reference. Suppose we have a list of data as :

L=list(c(1,2,3), c(4,5,6))

The function lapply returns a list.

lapply(L, sum) 

The above means something like below.

list( sum( L[[1]]) , sum( L[[2]]))

Now let us do the same thing for do.call

do.call(sum, L) 

It means

sum( L[[1]], L[[2]])

In our example, it returns 21. In short, lapply always returns a list while the return type of do.call really depends on the function executed.


The difference between both are :

lapply(1:n,function,parameters)

=> This send 1,parameters to function => this sends 2,parameters to function and so on

do.call 

Just sends 1…n as a vector and parameters to function

So in apply you have n function calls,in do.call you have just one

참고URL : https://stackoverflow.com/questions/10801750/whats-the-difference-between-lapply-and-do-call

반응형