本文介绍了是否有等效于 data.table::rleid 的 dplyr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data.table 提供了一个很好的便利功能,rleid 用于运行长度编码:

库(data.table)DT = data.table(grp=rep(c("A", "B", "C", "A", "B"), c(2, 2, 3, 1, 2)), value=1:10)rleid(DT$grp)# [1] 1 1 2 2 3 3 3 4 5 5

我可以在基本 R 中模仿这个:

df 

有谁知道 dplyr 等价物 (?) 或者是使用 dplyr 创建 rleid 行为的最佳"方法是执行以下操作

库(dplyr)my_rleid = rep(seq_along(rle(df$grp)$values), times = rle(df$grp)$lengths)df %>%变异(rleid = my_rleid)
解决方案

你可以这样做(当你同时拥有 data.tabledplyr 已加载):

DT <- DT %>% mutate(rlid = rleid(grp))

这给出了:

>DTgrp 值 rlid1:一个 1 12:A 2 13:乙 3 24:乙 4 25:C 5 36:C 6 37:C 7 38:一个 8 49:乙 9 510:乙 10 5

当你不想加载 data.table 你也可以单独使用(正如@DavidArenburg 在评论中提到的那样):

DT <- DT %>% mutate(rlid = data.table::rleid(grp))

正如@RichardScriven 在他的评论中所说,你可以复制/窃取它:

myrleid <- data.table::rleid

data.table offers a nice convenience function, rleid for run-length encoding:

library(data.table)
DT = data.table(grp=rep(c("A", "B", "C", "A", "B"), c(2, 2, 3, 1, 2)), value=1:10)
rleid(DT$grp)
# [1] 1 1 2 2 3 3 3 4 5 5

I can mimic this in base R with:

df <- data.frame(DT)
rep(seq_along(rle(df$grp)$values), times = rle(df$grp)$lengths)
# [1] 1 1 2 2 3 3 3 4 5 5

Does anyone know of a dplyr equivalent (?) or is the "best" way to create the rleid behavior with dplyr is to do something like the following

library(dplyr)

my_rleid = rep(seq_along(rle(df$grp)$values), times = rle(df$grp)$lengths)

df %>%
  mutate(rleid = my_rleid)
解决方案

You can just do (when you have both data.table and dplyr loaded):

DT <- DT %>% mutate(rlid = rleid(grp))

this gives:

When you don't want to load data.table separately you can also use (as mentioned by @DavidArenburg in the comments):

DT <- DT %>% mutate(rlid = data.table::rleid(grp))

And as @RichardScriven said in his comment you can just copy/steal it:

myrleid <- data.table::rleid

这篇关于是否有等效于 data.table::rleid 的 dplyr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:41