1

I'm new to R. Reading Book Of R by Tilman Davies. An example is provided for how to use an externally defined helper function which incidentally utilizes double square brackets [[]]. Please explain what helper.call[[1]] and helper.call[[2]] are doing and use of double brackets here.

multiples_helper_ext <- function(x=foo,matrix.flags,mat=diag(2){
  indexes <- which(matrix.flags)
  counter <- 0
  result <- list()
  for(i in indexes){
    temp <- x[[i]]
    if(ncol(temp)==nrow(mat)){
      counter <- counter+1
      result[[counter]] <- temp%*%mat
    }
  }
  return(list(result,counter))
}

multiples4 <- function(x,mat=diag(2),str1="no valid matrices",str2=str1){
  matrix.flags <- sapply(x,FUN=is.matrix)

  if(!any(matrix.flags)){
    return(str1)
  }

  helper.call <- multiples_helper_ext(x,matrix.flags,mat=diag(2)
  result <- helper.call[[1]] #I dont understand this use of double bracket
  counter <- helper.call[[2]] #and here either

  if(counter==0){
    return(str2)
  } else {
    return(result)
  }
}
foo <- list(matrix(1:4,2,2),"not a matrix","definitely not a matrix",matrix(1:8,2,4),matrix(1:8,4,2))
Aitch
  • 297
  • 1
  • 3
  • 10
  • 2
    Suggested dupe: [Difference between \[\] and \[\[\]\]?](https://stackoverflow.com/q/1169456/903061), though [How to correctly use lists in R?](https://stackoverflow.com/q/2050790/90306) is also relevant. In short, if `x` is a list then `x[[1]]` selects the first element of `x`, whereas `x[1]` a sublist (still a list!) containing the first element of `x`. Use `help("[[")` for the built-in help. – Gregor Thomas Oct 05 '17 at 21:12
  • Which list are the double brackets referencing? – Aitch Oct 05 '17 at 21:20
  • The function returns `list(result,counter)`. So `helper.call[[1]]` refers to `result`. – Rui Barradas Oct 05 '17 at 21:27
  • `helper.call[[1]]` indexes the list `helper.call`. – Gregor Thomas Oct 05 '17 at 23:00
  • @RuiBarradas Oh now I see that helper function actually returns a list and multiples4 references elements in it with helper.call[[1]] and helper.call[[2]]. If you mark yours as answer, I will check it. Thanks. – Aitch Oct 06 '17 at 02:28
  • Don't worry about upvotes, but thanks. – Rui Barradas Oct 06 '17 at 05:50

2 Answers2

1

In R there are two basic types of objects: lists and vectors. The items of lists can be other objects, the items of of vectors are usually numbers, strings, etc.

To access items in a list, you use the double bracket [[]]. This gives back the object at that place of the list. So

x <- 1:10

x is now a vector of integers

L <- list( x, x, "hello" )

L is a list whose first item is the vector x, its second item is the vector x, and its third item is the string "hello".

L[[2]]

This give back a vector, 1:10, which is stored in the 2nd place in L.

L[2]

This is a bit confusing, but this gives back a list whose only item is 1:10, i.e. it only contains L[[2]].

In R, when you want to return multiple values, you usually do this with a list. So, you might end you function with

f <- function() {
  return( list( result1="hello", result2=1:10) )
}
x = f()

Now you can access the two results with

print( x[["result1"]] )
print( x[["result2"]] )

You can also access items of a list with ''$, so instead you can write

print( x$result1 )
print( x$result2 )
0

The syntax [[]] is used for list in python. Your helper.call is a list (of result and counter), so helper.cal[[1]] returns the first element of this list (result). Have a look here: Understanding list indexing and bracket conventions in R

ZiGaelle
  • 744
  • 1
  • 9
  • 21