R: Further subset a selection using the pipe %>% and placeholder

左心房为你撑大大i 提交于 2019-11-30 21:20:19

Since you're going from a bunch of data into one (row of) value(s), you're summarizing. in a dplyr pipeline you can then use the summarize function, within the summarize function you don't need to subset and can just call pre and post

Like so:

dat %>% select(pre, post) %>% summarize(CD = cohensD(pre, post)) 

(The select statement isn't actually necessary in this case, but I left it in to show how this works in a pipeline)

This would work:

dat %>% select(pre, post) %>% {cohensD(.$pre, .$post)}

Wrapping the last call into curly braces makes it be treated like an expression and not a function call. When you pipe something into an expression, the . gets replaced as expected. I often use this trick to call a function which does not interface well with piping.

What is inside the braces happens to be a function call but could really be any expression of . .

It doesn't work because the . operator has to be used directly as an argument, and not inside a nested function (like $...) in your call.

If you really want to use piping, you can do it with the formula interface, but with a little reshaping before (melt is from reshape2 package):

dat %>% select(pre, post) %>% melt %>% cohensD(value~variable, .)
#### [1] 0.8115027
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!