lag

Replace 0's with previous non-zero value per ID (lag)

白昼怎懂夜的黑 提交于 2020-01-15 09:41:48
问题 How can I replace all 0's with the last non-zero value per ID in R? Example: Input: df <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2), Var1 = c(0,10, 30, 0, 0,50,80,0, 0, 57, 0)) Output: df <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2), Var1 = c(0,10, 30, 0, 0,50,80,0, 0, 57, 0), res = c(0,10,30,30,30,50,80,0,0,57,57)) Is there an easy way with lag function? 回答1: Here's a tidyverse approach: library(tidyverse) df %>% group_by(ID) %>% mutate(x = replace(Var1, cumsum(Var1 !=0) > 0 & Var1 == 0, NA))

Replace 0's with previous non-zero value per ID (lag)

廉价感情. 提交于 2020-01-15 09:41:09
问题 How can I replace all 0's with the last non-zero value per ID in R? Example: Input: df <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2), Var1 = c(0,10, 30, 0, 0,50,80,0, 0, 57, 0)) Output: df <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2), Var1 = c(0,10, 30, 0, 0,50,80,0, 0, 57, 0), res = c(0,10,30,30,30,50,80,0,0,57,57)) Is there an easy way with lag function? 回答1: Here's a tidyverse approach: library(tidyverse) df %>% group_by(ID) %>% mutate(x = replace(Var1, cumsum(Var1 !=0) > 0 & Var1 == 0, NA))

Test app performance by making it lag

自作多情 提交于 2020-01-06 15:17:17
问题 Is there a way or an application to test performance by making the app execute slower? I want to be sure that my app will perform well on older hardware. 回答1: Just adding stalls in SW won't necessarily imitate any older HW, it would just show you how the stalled code behaves on the new HW (and if the stalls aren't properly serializing - they may actually get avoided altogether). If you just want to see how the code behaves without some specific ISA features you can disable them on compilation

Calculating time lag between sequential events after grouping for subsets

蓝咒 提交于 2020-01-05 15:36:40
问题 I am trying to calculate the time between sequential observations for different combinations of my columns. I have attached a sample of my data here. A subset of my data looks like: head(d1) #visualize the first few lines of the data date time year km sps pp datetime prev timedif seque <fct> <fct> <int> <dbl> <fct> <dbl> <chr> <dbl> <dbl> <chr> 2012/06/09 2:22 2012 110 MICRO 0 2012-06-09 02:22 0 260. 00 2012/06/19 2:19 2012 80 MICRO 0 2012-06-19 02:19 1 4144 01 2012/06/19 22:15 2012 110 MICRO

Calculating time lag between sequential events after grouping for subsets

◇◆丶佛笑我妖孽 提交于 2020-01-05 15:36:09
问题 I am trying to calculate the time between sequential observations for different combinations of my columns. I have attached a sample of my data here. A subset of my data looks like: head(d1) #visualize the first few lines of the data date time year km sps pp datetime prev timedif seque <fct> <fct> <int> <dbl> <fct> <dbl> <chr> <dbl> <dbl> <chr> 2012/06/09 2:22 2012 110 MICRO 0 2012-06-09 02:22 0 260. 00 2012/06/19 2:19 2012 80 MICRO 0 2012-06-19 02:19 1 4144 01 2012/06/19 22:15 2012 110 MICRO

ActionScript 3 AIR — Video make blink jump

邮差的信 提交于 2020-01-05 04:17:05
问题 I make an application for iOS and Android using ActionScript 3 and Adobe AIR ( 3.7 ) to build the ipa and apk. In this application, I load a Video from an FLV and add it in the scene. The problem is, on the emulator or the Flash view, all is ok, but, on the iPad ( test on iPad 1, 2 and 3 with same results ) the video makes shorts jumps ( like a sudden freeze follow by a short jump into the time-line ) every 2 secondes, approximately. Of course, I make sure that the video wasn't under other

Cause of Lag in Painting a Line Set by Dragging the Mouse

我的梦境 提交于 2020-01-05 03:58:24
问题 I have written an applet in Java that allows the user to set background color, and "pen" color then click and draw in the window frame to drawn a line. I set this line by filling ovals at each x and y point of the dragging mouse. I also allows user to use the - and + keys to change the size of the line by increasing or decreasing the radius of the ovals. My issue is that there is some cause of lag in the drawing of the line. I believe it is in the mouseDrag method and the speed at which that

Clustering rows by group based on column value with conditions

你说的曾经没有我的故事 提交于 2020-01-04 13:46:28
问题 A few days ago I opened this thread: Clustering rows by group based on column value In which we obtained this result: df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,1, 1, 1,1,1,1,1), Obs1 = c(1,1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,1), Control = c(0,3,3,1,12,1,1,1,36,13,1,1,2,24,2,2,48), ClusterObs1 = c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5)) With: df <- df %>% group_by(ID) %>% mutate_at(vars(Obs1), funs(ClusterObs1= with(rle(.), rep(cumsum(values == 1), lengths)))) Now I have to make some

R: Is it possible to use mutate+lag with the same column?

匆匆过客 提交于 2020-01-04 08:39:19
问题 I'm trying to replicate the following formula in R: Xt = Xt-1 * b + Zt * (1-b) I'm using the following code t %>% mutate(x= ifelse(week == 1, z, NaN)) %>% # Initial value for the first lag mutate(x= ifelse(week != 1, lag(x,1 ,default = 0) * b + z, z) But I get all NaN except from the second element. z b x (dbl) (dbl) (dbl) 1 168.895 0.9 168.8950 2 20.304 0.9 131.7472 3 14.943 0.9 NA 4 11.028 0.9 NA 5 8.295 0.9 NA 6 8.024 0.9 NA 7 6.872 0.9 NA 8 7.035 0.9 NA 9 4.399 0.9 NA 10 4.158 0.9 NA This

R: Is it possible to use mutate+lag with the same column?

纵然是瞬间 提交于 2020-01-04 08:39:12
问题 I'm trying to replicate the following formula in R: Xt = Xt-1 * b + Zt * (1-b) I'm using the following code t %>% mutate(x= ifelse(week == 1, z, NaN)) %>% # Initial value for the first lag mutate(x= ifelse(week != 1, lag(x,1 ,default = 0) * b + z, z) But I get all NaN except from the second element. z b x (dbl) (dbl) (dbl) 1 168.895 0.9 168.8950 2 20.304 0.9 131.7472 3 14.943 0.9 NA 4 11.028 0.9 NA 5 8.295 0.9 NA 6 8.024 0.9 NA 7 6.872 0.9 NA 8 7.035 0.9 NA 9 4.399 0.9 NA 10 4.158 0.9 NA This