0

Running this code in R :

pr <- function(x) {
  x
}

lapply(c("a" , "b") , pr)

sapply(c("a" , "b") , pr)

apply(c("a" , "b") , pr)

returns :

> pr <- function(x) {
+   x
+ }
> 
> lapply(c("a" , "b") , pr)
[[1]]
[1] "a"

[[2]]
[1] "b"

> 
> sapply(c("a" , "b") , pr)
  a   b 
"a" "b" 
> 
> apply(c("a" , "b") , pr)
Error in match.fun(FUN) : argument "FUN" is missing, with no default

Here is my understanding of returns values of lapply , sapply , apply in above context:

lapply returns a list , is this signified by [[1]]?

sapply returns a vector but why are vector dimensions [1] not returned in response?

apply returns an error , why is this occurring?

lmo
  • 37,904
  • 9
  • 56
  • 69
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 1
    The `apply` needs a `MARGIN` argument. In this case it is 2. BTW, it won't even work here because it is for `data.frame` or `matrix` i.e. `apply(t(c("a" , "b")) , 2, pr)` – akrun Nov 28 '17 at 12:05
  • 1
    I highly recommend the following read "Advanced R" (functionals chapter) by Hadley Wickham: [http://adv-r.had.co.nz/Functionals.html](http://adv-r.had.co.nz/Functionals.html). And also have a look at the standard documentation of the functions in R (and the examples) :-) – Manuel Bickel Nov 28 '17 at 12:08
  • 2
    May I suggest reading the documentation of these functions? – Roland Nov 28 '17 at 12:15
  • I removed any mention of Rstudio as this question has nothing to do with the IDE. It is solely a question about R. (R and Rstudio are independent pieces of software). – lmo Nov 28 '17 at 12:43

1 Answers1

3

The lapply/sapply loops through each element i.e. in this case each element of the vector (c('a', 'b')). If it is a data.frame, the columns will be the looped and a matrix is a vector with dimensions, therefore, each element will be looped and the function is applied. The output returned is a list for lapply and sapply here it is a vector, but it depends on the argument simplify. By default it is simplify = TRUE, so if the lengths of the output elements are same, it returns a vector.

With apply, we needs a MARGIN argument. The MARGINs are there for data.frame or matrix. Suppose, if we convert the vector to a matrix, then with MARGIN it works

apply(t(c("a" , "b")) , 2, pr)

If it is a single column matrix, use the MARGIN=1

apply(matrix(c('a', 'b')), 1, pr)

It could return a list, vector etc depending on the length of the output element. Here, it is just returning the values. So, it is a vector

akrun
  • 874,273
  • 37
  • 540
  • 662
  • sapply returns a vector but why are vector dimensions [1] not returned in response, is this some quirk of rstudio ? – blue-sky Nov 28 '17 at 12:39
  • @blue-sky As mentioned, the default for `sapply` is `simplify = TRUE` – akrun Nov 28 '17 at 12:40
  • thanks but would have thought using sapply(c("a" , "b"), pr, simplify = FALSE) should return [1] "a" "b" . What is significance of [[1]] ? – blue-sky Nov 28 '17 at 12:44
  • @blue-sky As you noted, it is returning a `list` because you don't want to simplify it to a vector. The significance of `[[1]]` is that it subsets the first list element to give 'a' – akrun Nov 28 '17 at 12:45
  • @blue-sky Read the documentation on `?[` It contains very useful information that should help to clarify the differences between lists and vectors. Maybe also [this SO question](https://stackoverflow.com/questions/36777567/is-there-a-logical-way-to-think-about-list-indexing/36815401). – lmo Nov 28 '17 at 12:48