如何为R中由给定类型的行拆分的连续行分配ID?

编程入门 行业动态 更新时间:2024-10-10 01:24:34
本文介绍了如何为R中由给定类型的行拆分的连续行分配ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数据框,其行代表人。对于给定的家庭,第一行在 A 列中的值为 1 ,随后的所有行均包含相同的族,直到 A 列中的另一行具有值 1 。然后,开始一个新的家庭。

I have a dataframe whose rows represent people. For a given family, the first row has the value 1 in the column A, and all following rows contain members of the same family until another row in in column A has the value 1. Then, a new family starts.

我想为数据集中的所有家庭分配ID。换句话说,我想参加:

I would like to assign IDs to all families in my dataset. In other words, I would like to take:

A 1 2 3 1 3 3 1 4

并将其变成:

A family_id 1 1 2 1 3 1 1 2 3 2 3 2 1 3 4 3

我正在使用一个300万行的数据框,因此我想出的一个简单的 for 循环解决方案不足必要的效率。此外, family_id 不必是连续的。

I'm playing with a dataframe of 3 million rows, so a simple for-loop solution I came up with falls short of necessary efficiency. Also, the family_id need not be sequential.

我将采用dplyr解决方案。

I'll take a dplyr solution.

推荐答案

数据:

df <- data.frame(A = c(1:3,1,3,3,1,4))

代码:

df$familiy_id <- cumsum(c(-1,diff(df$A)) < 0)

结果:

# A familiy_id #1 1 1 #2 2 1 #3 3 1 #4 1 2 #5 3 2 #6 3 2 #7 1 3 #8 4 3

请注意:

please note:

当出现的数字小于前一个数字时,此解决方案将启动一个新组。

This solution starts a new group when a number occurs that is smaller than the previous one.

如果100%确保一个新组始终以 1 开头,那么ronak的解决方案就是完美的。

When its 100% sure that a new group always begins with a 1 consistently, then ronak's solution is perfect.

更多推荐

如何为R中由给定类型的行拆分的连续行分配ID?

本文发布于:2023-11-22 00:29:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   分配   类型   ID

发布评论

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

>www.elefans.com

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