What is the name of the first argument `[`?

letter[2]equivalent '['(letters,i=2), second argument i.

What is the name of the first argument, so the next two expressions will be equivalent?

lapply(1:3,function(x){letters[x]})
lapply(1:3,`[`,param1 = letters) # param1 to be replaced with solution
+4
source share
3 answers

You should be more specific than "[", for example:

lapply(1:3, `[.numeric_version`, x = letters)

# [[1]]
# [1] "a"
# 
# [[2]]
# [1] "b"
# 
# [[3]]
# [1] "c"

(Not sure which [.numeric_versionis the most suitable, though ... I dig a little more)

+3
source

, , . [ . Map lapply, , , Indices, , :

  Map("[",list(letters),1:3)
 [[1]]
 [1] "a"

 [[2]]
 [1] "b"

 [[3]]
 [1] "c"

, . ,

+5

rlang::as_closureand purrr::as_mapper, as based on rlang::as_function(see doc) both convert [to a function with named parameters:

lapply(1:3, purrr::as_mapper(`[`), .x = letters)
lapply(1:3, rlang::as_closure(`[`), .x = letters)
# [[1]]
# [1] "a"
# 
# [[2]]
# [1] "b"
# 
# [[3]]
# [1] "c"
+1
source

Source: https://habr.com/ru/post/1685126/


All Articles