1

I was wondering if anybody knew or could help me find the formal argument names for all of the magrittr alias functions. For example, I know that the argument for 'set_colnames' is 'value'.

df <- data.frame(1:3, 4:6, 7:9) %>%
  set_colnames(value = c('a', 'b', 'c')

Normally, I just pass the arguments in unnamed but lately I've been trying to make my code as robust as possible and it's also helpful when you're trying to use these aliases inside of an apply function (or llply in my case). The problem that I'm having is that I have a list of similar df's and I want to extract the same column from each but still retain the list format.

df_list <- list(data.frame('a' = 1:3, 'b' = 4:6), 
                data.frame('a' = 7:9, 'b' = 10:12))

What I would like to do is something like

df_b <- df_list %>%
  llply(.fun = use_series, b)

But this doesn't work because I don't know the formal name to pass to 'use_series'.

zero323
  • 322,348
  • 103
  • 959
  • 935
stat_student
  • 787
  • 10
  • 17

1 Answers1

2

use_series is just an alias for $. You can see that by typing the function name without the parenthesis

use_series
# .Primitive("$")

The $ primitive function does not have formal argument names in the same way a user-defined function does. It would be easier to use extract2 in this case

df_b <- df_list %>%
  llply(.fun = extract2, "b")

Note in this case you pass the column name as a character values rather than a symbol. I've learned before that the $ is tricky to use the the apply family of functions.

Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you! It worked perfectly! Do you by chance know if there is documentation somewhere that lists all of the formal arguments names for the magrittr aliases that have them? – stat_student Jun 01 '15 at 18:45
  • Most of them appear to be aliases for primitives which do not have formal argument names. For those that do have formal arguments, you can use the `formals()` function to extract them: ie `formals(set_colnames)` – MrFlick Jun 01 '15 at 19:23