Rich Scriven Rich Scriven - 1 year ago 37
R Question

within.list() versus - What's the difference?

Upon investigating

, I noticed it uses methods for lists and data frames.

# function (data, expr, ...)
# UseMethod("within")
# <bytecode: 0x4403f78>
# <environment: namespace:base>
# [1] within.list
# see '?methods' for accessing help and source code

Reading through the code for each
, I noticed they are exactly the same. This is confirmed by

# [1] TRUE

They also seem to act the same, and do exactly the same thing. Take these two examples:

x <- list(a = 1:5, b = 6:10)
within.list(x, { d <- 11:15 }),, { d <- 11:15 })
# [1] TRUE
y <-
within.list(y, { d <- 11:15 }),, { d <- 11:15 })
# [1] TRUE

Is there an actual difference between these two functions? If so, what is it? If not, why do two of exactly the same function exist?

Answer Source

No difference. within.list is an alias for See the source

within.list <-

Both exist so that if the within generic is called on either a list or a data.frame the proper method will be dispatched.

If within.list didn't exist and you tried to call within(myList, ...) you would get an error like

Error in UseMethod("within") : 
  no applicable method for 'within' applied to an object of class "list"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download