[英]Dplyr::last - rows dropped if one variable can't be computed in summarise
I have a data frame I want to summarise, for some of the groups, some variables should return NA, but instead the whole row is removed.我有一个要总结的数据框,对于某些组,某些变量应该返回 NA,但整行都被删除了。 Toy example Df:
玩具示例 Df:
df=data.frame(button=c(1,2,3,3,3,2),group=c(1,1,1,2,2,2),RT=c(100,110,120,130,140,150))
When I summarise without using "last" I get as expected:当我在不使用“last”的情况下进行总结时,我得到了预期的结果:
df%>%dplyr::group_by(group) %>%dplyr::summarize(RT=mean(RT), RT.button1=mean(RT[button==1]))
# A tibble: 2 x 3
group RT RT.button1
* <dbl> <dbl> <dbl>
1 1 110 110
2 2 140 NaN
But when I use last, instead the row is removed但是当我最后使用时,该行被删除了
df%>%dplyr::group_by(group) %>%dplyr::summarize(RT=mean(RT), RT.button1=mean(RT[button==1]),RT.last.button1=last(RT[button==1]))
# A tibble: 1 x 4
# Groups: group [1]
group RT RT.button1 RT.last.button1
<dbl> <dbl> <dbl> <dbl>
1 1 110 110 110
Is there any way to get "last" to return NA instead of removing the row?有没有办法让“最后”返回 NA 而不是删除该行? I'd be very grateful for any pointers!
如果有任何指点,我将不胜感激!
This is certainly because you are using data.table::last
instead of dplyr::last
.这当然是因为您使用的是
data.table::last
而不是dplyr::last
。
With data.table::last
:使用
data.table::last
:
df %>%
dplyr::group_by(group) %>%
dplyr::summarize(RT = mean(RT),
RT.button1 = mean(RT[button == 1]),
RT.last.button1 = data.table::last(RT[button == 1]))
# Groups: group [1]
group RT RT.button1 RT.last.button1
<dbl> <dbl> <dbl> <dbl>
1 1 110 110 110
With dplyr::last
:使用
dplyr::last
:
df %>%
dplyr::group_by(group) %>%
dplyr::summarize(RT = mean(RT),
RT.button1 = mean(RT[button == 1]),
RT.last.button1 = dplyr::last(RT[button == 1]))
# A tibble: 2 x 4
group RT RT.button1 RT.last.button1
<dbl> <dbl> <dbl> <dbl>
1 1 110 110 110
2 2 140 NaN NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.