Say I have the following code:
# Create data frame
df <- data.frame(a = 1:5, b = 6:10)
# a b
# 1 1 6
# 2 2 7
# 3 3 8
# 4 4 9
# 5 5 10
Now, I print each row individually using apply
apply(df, MARGIN = 1, print)
If I wanted to reference a particular element of each row passed to FUN
in apply
, I'd do it by defining an anonymous function, like this:
apply(df, MARGIN = 1, function(x)print(x[1]))
This code just prints the first element in each row.
In the tidyverse
, an object passed to a function via a pipe is by default referred to by .
. If this was also the case for apply
, I could write something along the lines of...
apply(df, MARGIN = 1, print(.[1]))
My question: can I refer to the object passed to FUN
by a default name thus avoiding the need for a function definition (e.g., function(x)
)?