Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the name of the first argument of `[`?

Tags:

r

extract

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

What is the name of the first argument so the 2 following expressions would be equivalent ?

lapply(1:3,function(x){letters[x]})
lapply(1:3,`[`,param1 = letters) # param1 to be replaced with solution
like image 528
Moody_Mudskipper Avatar asked Jan 03 '23 12:01

Moody_Mudskipper


2 Answers

For you to be able to define a function similar to the one above, you will have to pass two arguments to your function. The function [ does take various inputs. We can use Map instead of lapply to give it both the data where to extract from and the Indices to indicate the part of the data to be extracted:

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

 [[2]]
 [1] "b"

 [[3]]
 [1] "c"

This is similar to what you have above. Hope this helps

like image 86
KU99 Avatar answered Feb 15 '23 07:02

KU99


You have to be could be more specific than "[", for instance:

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

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

(Not sure [.numeric_version is the most appropriate, though... I'm digging a bit more)

like image 27
Aurèle Avatar answered Feb 15 '23 07:02

Aurèle