问题描述
我最近发现了管道运算符%>%
,它可以使代码更具可读性.这是我的 MWE.
I recently discovered the pipe operator %>%
, which can make code more readable. Here is my MWE.
library(dplyr) # for the pipe operator
library(lsr) # for the cohensD function
set.seed(4) # make it reproducible
dat <- data.frame( # create data frame
subj = c(1:6),
pre = sample(1:6, replace = TRUE),
post = sample(1:6, replace = TRUE)
)
dat %>% select(pre, post) %>% sapply(., mean) # works as expected
但是,在这种特殊情况下,我很难使用管道运算符
However, I struggle using the pipe operator in this particular case
dat %>% select(pre, post) %>% cohensD(.$pre, .$post) # piping returns an error
cohensD(dat$pre, dat$post) # classical way works fine
为什么不能使用占位符 .
与 $
组合对列进行子集化?使用管道运算符 %>%
编写这一行是否值得,或者它是否使语法复杂化?经典的写法似乎更简洁.
Why is it not possible to subset columns using the placeholder .
in combination with $
? Is it worthwhile to write this line using a pipe operator %>%
, or does it complicate syntax? The classical way of writing this seems more concise.
推荐答案
由于您要从一堆数据转换为一个(行)值,因此您需要进行总结.在 dplyr 管道中,您可以使用汇总函数,在汇总函数中您不需要子集,只需调用 pre
和 post
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
像这样:
dat %>% select(pre, post) %>% summarize(CD = cohensD(pre, post))
(在这种情况下实际上不需要 select 语句,但我保留了它以展示它在管道中的工作原理)
(The select statement isn't actually necessary in this case, but I left it in to show how this works in a pipeline)
这篇关于R:使用管道 %>% 和占位符进一步子集选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!