Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

r - Using anonymous functions with summarize_each or mutate_each

I want to use anonymous functions in calls to summarize_each:

# how to use an anonymous function in dplyr
df_foo = data_frame(x = rnorm(100),
                    y = rnorm(100))

df_foo %>% 
  summarize_each(funs(function(bar) sum(bar/10)))

How would I go about achieving this? Obviously, naming the function before using it works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a matter of using a lot of parentheses so everything gets evaluated:

df_foo %>% 
  summarize_each(funs(((function(bar){sum(bar/10)})(.))))
#
# Source: local data frame [1 x 2]
# 
#          x          y
#      (dbl)      (dbl)
# 1 1.113599 -0.4766853

where you need

  • parentheses around the function definition so it gets defined,
  • a set of parentheses with a . to tell funs which parameter to stick the data passed to it in (seemingly redundant with single-parameter functions, but not so with multi-parameter ones; see ?funs for more examples), and
  • parentheses around the whole thing to actually evaluate it,

which is kind of ridiculous, but that seems to be the most concise funs can handle. It makes some sense if you look at what you'd have to write to evaluate a similar anonymous function on its own line, e.g.

(function(bar){sum(bar/10)})(df_foo$x)

though the pair wrapping the whole thing are extra for funs. You can use braces {} instead for the outer pair if you prefer, which might make more syntactic sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...