R中的数据帧累积游程长度编码 [英] data frame cumulative run length encoding in R

查看:67
本文介绍了R中的数据帧累积游程长度编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含与观察值相关的值1或0的数据框。我想对连续出现的1进行计数,将其重置为0。游程编码功能( rle )似乎可以完成工作,但我无法将数据转换为所需的格式。我想尝试执行此操作而不编写自定义函数。在下面的数据中,我在一个数据框中进行了观察,然后我想派生连续列并写回到该数据框中。此链接是良好的开端

I've got a data frame containing values relating to observations, 1 or 0. I want to count the continual occurrences of 1, resetting at 0. The run length encoding function (rle) seems like it would do the work but I can't work out getting the data into the desired format. I want to try doing this without writing a custom function. In the data below, I have observation in a data frame, then I want to derive the "continual" column and write back to the dataframe. This link was a good start.

observation continual 
          0         0
          0         0
          0         0
          1         1
          1         2
          1         3
          1         4
          1         5
          1         6
          1         7
          1         8
          1         9
          1        10
          1        11
          1        12
          0         0
          0         0


推荐答案

您可以通过几个步骤轻松完成此操作:

You can do this pretty easily in a couple of steps:

x <- rle(mydf$observation)       ## run rle on the relevant column
new <- sequence(x$lengths)       ## create a sequence of the lengths values
new[mydf$observation == 0] <- 0  ## replace relevant values with zero
new
#  [1]  0  0  0  1  2  3  4  5  6  7  8  9 10 11 12  0  0

这篇关于R中的数据帧累积游程长度编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆