[英]Repeating first observation within group with dplyr
我正在嘗試在該組的所有行中重復該組中的第一個值。 我以為他會做到的:
library(dplyr)
data.frame(g=c(1,1,2,2), v=c(1,2,3,4)) %>%
group_by(g) %>%
mutate(f=first(v))
# Source: local data frame [4 x 3]
# Groups: g [2]
#
# g v f
# (dbl) (dbl) (dbl)
# 1 1 1 1
# 2 1 2 1
# 3 2 3 1
# 4 2 4 1
但是我不明白為什么f
不返回1,1,3,3。
這應該工作
data.frame(g=c(1,1,2,2), v=c(1,2,3,4)) %>%
group_by(g) %>%
mutate(f = dplyr::first(v))
你可能會拿起first
從不同的包。 它確實存在於xts
軟件包中,並且可能存在於其他軟件包中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.