根据组中的第一值的条件,在DF中替换后续值

发布于 2025-01-21 12:42:20 字数 1435 浏览 2 评论 0原文

我在有序的R数据框架中具有这种类型的数据。

set.seed(25)

date <- sort(as.Date(sample( as.numeric(as.Date("2019-01-01")): as.numeric(as.Date("2021-03-31")), 10, 
                             replace = T), 
                     origin = '1970-01-01'))

type <- c("Football", "Football", "Rugby", "Football", "Hockey", "Tennis", "Hockey", "Basketball", "Basketball", "Rugby")

id <- c("1","1","1","1","2","2","3","4","4","5")

df <- data.frame(date,id, type)



      date id       type
  2019-04-09  1   Football
  2019-04-13  1   Football
  2019-04-20  1      Rugby
  2019-04-21  1   Football
  2019-05-31  2     Hockey
  2020-02-09  2     Tennis
  2020-03-08  3     Hockey
  2020-03-24  4 Basketball
  2020-08-18  4   Football
  2020-11-01  5      Rugby

我要得到的结果是:

    date id       type     type_2
  2019-04-09  1   Football   Football
  2019-04-13  1   Football   Football
  2019-04-20  1      Rugby      Multi
  2019-04-21  1   Football      Multi
  2019-05-31  2     Hockey     Hockey
  2020-02-09  2     Tennis      Multi
  2020-03-08  3     Hockey     Hockey
  2020-03-24  4 Basketball Basketball
  2020-08-18  4 Basketball Basketball
  2020-11-01  5      Rugby      Rugby

基本上,如果他的下一项运动与上一项相同的运动,那么ID练习就会保持不变,type_2保持不变,但是稍后他更改运动一方面,他以后更改了其余的价值观。

我尝试使用lag()lead()if_else()dplyr中执行此操作,但是结果永远不会出来我想要的方式。

I have this type of data in an ordered R dataframe.

set.seed(25)

date <- sort(as.Date(sample( as.numeric(as.Date("2019-01-01")): as.numeric(as.Date("2021-03-31")), 10, 
                             replace = T), 
                     origin = '1970-01-01'))

type <- c("Football", "Football", "Rugby", "Football", "Hockey", "Tennis", "Hockey", "Basketball", "Basketball", "Rugby")

id <- c("1","1","1","1","2","2","3","4","4","5")

df <- data.frame(date,id, type)



      date id       type
  2019-04-09  1   Football
  2019-04-13  1   Football
  2019-04-20  1      Rugby
  2019-04-21  1   Football
  2019-05-31  2     Hockey
  2020-02-09  2     Tennis
  2020-03-08  3     Hockey
  2020-03-24  4 Basketball
  2020-08-18  4   Football
  2020-11-01  5      Rugby

The result I'm trying to get at is this:

    date id       type     type_2
  2019-04-09  1   Football   Football
  2019-04-13  1   Football   Football
  2019-04-20  1      Rugby      Multi
  2019-04-21  1   Football      Multi
  2019-05-31  2     Hockey     Hockey
  2020-02-09  2     Tennis      Multi
  2020-03-08  3     Hockey     Hockey
  2020-03-24  4 Basketball Basketball
  2020-08-18  4 Basketball Basketball
  2020-11-01  5      Rugby      Rugby

Basically, the first sport in time an id practices stays if the next sport he practices is the same as the previous one, type_2 remains the same, but as soon as he changes sport later on, he changes to multi for the rest of his values later on.

I tried do this with lag(), lead() and if_else() in dplyr but the results never come out the way I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-28 12:42:20

您可以从data.table中使用rleid来生成每个type变量的运行长度ID id 。第一个更改后的所有内容变为“ Multi”

library(data.table)

setDT(df)[, type2 := replace(type, rleid(type) > 1, 'Multi'), id]
df

#          date id       type      type2
# 1: 2019-02-18  1   Football   Football
# 2: 2019-02-28  1   Football   Football
# 3: 2019-03-13  1      Rugby      Multi
# 4: 2019-09-29  1   Football      Multi
# 5: 2019-10-09  2     Hockey     Hockey
# 6: 2020-03-19  2     Tennis      Multi
# 7: 2020-04-21  3     Hockey     Hockey
# 8: 2020-06-19  4 Basketball Basketball
# 9: 2020-09-08  4 Basketball Basketball
#10: 2020-10-08  5      Rugby      Rugby

如果您希望将其写入dplyr -

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(type2 = replace(type, rleid(type) > 1, 'Multi')) %>%
  ungroup

You may use rleid from data.table to generate the running length id for type variable in each id. Everything after the first change becomes "Multi".

library(data.table)

setDT(df)[, type2 := replace(type, rleid(type) > 1, 'Multi'), id]
df

#          date id       type      type2
# 1: 2019-02-18  1   Football   Football
# 2: 2019-02-28  1   Football   Football
# 3: 2019-03-13  1      Rugby      Multi
# 4: 2019-09-29  1   Football      Multi
# 5: 2019-10-09  2     Hockey     Hockey
# 6: 2020-03-19  2     Tennis      Multi
# 7: 2020-04-21  3     Hockey     Hockey
# 8: 2020-06-19  4 Basketball Basketball
# 9: 2020-09-08  4 Basketball Basketball
#10: 2020-10-08  5      Rugby      Rugby

If you prefer to write it in dplyr -

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(type2 = replace(type, rleid(type) > 1, 'Multi')) %>%
  ungroup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文