获取每组的第一个和最后一个值

编程入门 行业动态 更新时间:2024-10-25 04:18:55
本文介绍了获取每组的第一个和最后一个值 - dplyr group_by with last() 和 first()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的代码应该按年份对数据进行分组,然后使用每年的第一个和最后一个值创建两个新列.

The code below should group the data by year and then create two new columns with the first and last value of each year.

library(dplyr) set.seed(123) d <- data.frame( group = rep(1:3, each = 3), year = rep(seq(2000,2002,1),3), value = sample(1:9, r = T)) d %>% group_by(group) %>% mutate( first = dplyr::first(value), last = dplyr::last(value) )

然而,它不能正常工作.预期的结果是

However, it does not work as it should. The expected result would be

group year value first last <int> <dbl> <int> <int> <int> 1 1 2000 3 3 4 2 1 2001 8 3 4 3 1 2002 4 3 4 4 2 2000 8 8 1 5 2 2001 9 8 1 6 2 2002 1 8 1 7 3 2000 5 5 5 8 3 2001 9 5 5 9 3 2002 5 5 5

然而,我得到了这个(它在整个数据框中取第一个和最后一个值,而不仅仅是组):

Yet, I get this (it takes the first and the last value over the entire data frame, not just the groups):

group year value first last <int> <dbl> <int> <int> <int> 1 1 2000 3 3 5 2 1 2001 8 3 5 3 1 2002 4 3 5 4 2 2000 8 3 5 5 2 2001 9 3 5 6 2 2002 1 3 5 7 3 2000 5 3 5 8 3 2001 9 3 5 9 3 2002 5 3 5

推荐答案

dplyr::mutate() 成功了

d %>% group_by(group) %>% dplyr::mutate( first = dplyr::first(value), last = dplyr::last(value) )

更多推荐

获取每组的第一个和最后一个值

本文发布于:2023-10-24 08:36:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   每组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!