Questions tagged [rle]

RLE is run-length encoding, it is a form of lossless data compression based on sequences in which the same data value occurs in many consecutive data elements (typical usage e.g. in simple graphic images such as icons, line drawings, animations). Use this tag for questions about this type of encoding/compression. Do not use this tag for common issues about encoding/compression.

rle() computes the lengths and values of runs of equal values in a vector -- or the reverse operation with inverse.rle().

rle() returns an object of class "rle" which is a list with components:

  • lengths: an integer vector containing the length of each run.

  • values: a vector of the same length as lengths with the corresponding values.

inverse.rle() returns an atomic vector.

70 questions
1
vote
2 answers

R - Count consecutive occurrences of a specific number based on a specific group

In R say I had the dataframe: frame object positive 1 6 0 2 6 1 3 6 1 4 6 1 5 6 1 6 6 0 7 6 0 8 6 1 9 6 1 10 6 …
iamsaad
  • 11
  • 2
1
vote
1 answer

Filter a list of vectors using different run-length conditions

I have the following list of vectors: vec1 <- c(rep(0, 10), rep(1, 4), rep(0,5), rep(-1,5)) vec2 <- c(rep(-1, 7), rep(0,99), rep(1, 6)) vec3 <- c(rep(1,2), rep(-1,2), rep(0,10), rep(-1,4), rep(0,8)) vec4 <- rep(0, 100) dummy_list <- list(vec1,…
ramen
  • 691
  • 4
  • 20
1
vote
1 answer

Add the results of RLE to the original data frame in R

I have a data frame containing dates and for each date the number of events that took place. From this I add a field telling me if the number of events was above average or not. Date Events Above…
1
vote
1 answer

assigning group ID for pivoting, based on recurrent values in two columns

I have a long data frame in which a start and end day are assigned to an action. Some actions might only have a start day, and one action type can start and end multiple times. I would now like to pivot this wide, so that there is one row for each…
tjebo
  • 21,977
  • 7
  • 58
  • 94
1
vote
1 answer

Can you explain me the RLE algorithm code in python

I I've finally found how to make a RLE algorithm by watching a tutorial but This tutorial didn' t explain something in that code I didn't get why we write j = i instead of j = 0 (Knowing that I = 0) it's the same no ? I didn't get why i = j + 1…
1
vote
1 answer

Run Length Encoding within SQL Query

I have time-series data that I am summarizing by using run-length encoding with some additional summary statistics. The problem is that the data is a minimum of 40 million rows and I only have 16GB of RAM. At the moment I am having to perform the…
Melissa Salazar
  • 517
  • 5
  • 16
1
vote
2 answers

Find if certain value appears more than n-times subsequently [R]

I have a list of vectors, for instance: vec1 <- c(rep(0,5), 1, rep(0,11), rep(1,4), rep(0,6)) vec2 <- c(rep(0,11), 1, rep(0,18)) vec3 <- c(rep(0,3), rep(1,5), rep(0,21)) vec4 <- c(rep(0,23)) test_list <- list(vec1, vec2, vec3, vec4) I would like…
ramen
  • 691
  • 4
  • 20
1
vote
1 answer

Count events with two specific conditions in R

I need to count events with two specific conditions and aggregate by year. My data example is below: year <- c(rep(1981,20)) k1 <- c(rep(NA,5),rep("COLD",4),rep(NA,4),"COLD",NA,"COLD",rep(NA,4)) k2 <- c(rep(NA,10),rep("COLD",2),rep(NA,8)) k3 <-…
Indrute
  • 115
  • 10
1
vote
1 answer

Calculating median in each consecutive run

I have a data.frame as below; df <- data.frame(ID = c(2,3,5,8,9,10,12,13,14,15,16), value = c(1,2,3,4,5,6,7,8,9,10,11)) > df ID value 1 2 1 2 3 2 3 5 3 4 8 4 5 9 5 6 10 6 7 12 7 8 14 8 9 …
imtaiky
  • 191
  • 1
  • 12
1
vote
1 answer

Setting fixed value when counting observations

Goal: To create a variable named 'duration'--to count the number of months the 'previous month's value (0 or 1)' was consistent, (a) only when there are at least 3 consecutive observations in the past for a given month and (b) counting as '0' when…
user14250906
  • 197
  • 8
1
vote
3 answers

Rows sequence by group using two columns

Suppose I have the following df data <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2,3,3,3), Value = c(1,1,0,1,0,1,1,1,0,0,1,0,0,0), Result = c(1,1,2,3,4,5,5,1,2,2,3,1,1,1)) How can I obtain column Result from the first two…
torakxkz
  • 483
  • 5
  • 17
1
vote
2 answers

Replace consecutive repeat values based on different run lengths in R

Consider the following dataset: dat<-data.frame(id = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3), var1 = c("A","NA","B","A","NA","NA","B","A","NA","NA","NA","C","A","NA","B","A","NA","NA","D","A","NA","NA","B")) dat First, I…
el88
  • 49
  • 2
1
vote
2 answers

Count first occurrence of consecutive days from rle() in R

I have the following data: dat <- structure(list(Year = c(1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,…
Lyndz
  • 347
  • 1
  • 13
  • 30
1
vote
2 answers

Identify first occurence of vector where 12 of 15 values are 1

I have a vector like so: test = c(NA, 1, 1, 1, NA, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, 1, 1, 1, 1, NA, NA, 1) and within this vector I want to identify the first time that 12 of 15 values is equal to one. I have started by using rle to count the…
Stefano Potter
  • 3,467
  • 10
  • 45
  • 82
1
vote
2 answers

How to label groups in df based on specific sequence of values in a column

I have a dataframe with the id and value columns indicated below, but want to determine the Status column based on values in the value column, by id groups. x <- data.frame(id = c(rep(1,10), rep(2,10), rep(3,10)), serial =…
Shu
  • 57
  • 6