How to insert values in next couple of rows based on the previous row [duplicate]
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
I have a table like this
Id A B
1 11 event1
2 12 event2
3 00 event1
4 00 event2
5 11 event1
6 00 event2
7 00 event3
8 13 event1
I want to change 00 from the previous values. The output will look like this
Id A B
1 11 event1
2 12 event2
3 12 event1
4 12 event2
5 11 event1
6 11 event2
7 11 event3
8 13 event1
Is there a way to do this in R or MySQL.
Thanks
mysql sql r
marked as duplicate by Billal Begueradj, jogo, Community♦ Nov 20 '18 at 15:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
I have a table like this
Id A B
1 11 event1
2 12 event2
3 00 event1
4 00 event2
5 11 event1
6 00 event2
7 00 event3
8 13 event1
I want to change 00 from the previous values. The output will look like this
Id A B
1 11 event1
2 12 event2
3 12 event1
4 12 event2
5 11 event1
6 11 event2
7 11 event3
8 13 event1
Is there a way to do this in R or MySQL.
Thanks
mysql sql r
marked as duplicate by Billal Begueradj, jogo, Community♦ Nov 20 '18 at 15:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
I have a table like this
Id A B
1 11 event1
2 12 event2
3 00 event1
4 00 event2
5 11 event1
6 00 event2
7 00 event3
8 13 event1
I want to change 00 from the previous values. The output will look like this
Id A B
1 11 event1
2 12 event2
3 12 event1
4 12 event2
5 11 event1
6 11 event2
7 11 event3
8 13 event1
Is there a way to do this in R or MySQL.
Thanks
mysql sql r
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
I have a table like this
Id A B
1 11 event1
2 12 event2
3 00 event1
4 00 event2
5 11 event1
6 00 event2
7 00 event3
8 13 event1
I want to change 00 from the previous values. The output will look like this
Id A B
1 11 event1
2 12 event2
3 12 event1
4 12 event2
5 11 event1
6 11 event2
7 11 event3
8 13 event1
Is there a way to do this in R or MySQL.
Thanks
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
mysql sql r
mysql sql r
edited Nov 20 '18 at 15:13
jogo
10k92135
10k92135
asked Nov 20 '18 at 15:01
No_bodyNo_body
329111
329111
marked as duplicate by Billal Begueradj, jogo, Community♦ Nov 20 '18 at 15:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Billal Begueradj, jogo, Community♦ Nov 20 '18 at 15:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use correlated subquery in MySQL :
SELECT t.id,
(CASE WHEN A <> '00'
THEN A
ELSE (SELECT t1.A
FROM table t1
WHERE t1.id < t.id AND t1.A <> '00'
ORDER BY t1.id DESC
LIMIT 1
)
END) AS A, B
FROM table t;
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
add a comment |
Here is a one-liner to do this in R. Basically, we replace 0s with NA and use zoo::na.locf() to fill with the last non-NA value, i.e.
transform(d1, A = zoo::na.locf(replace(A, A == 0, NA)))
which gives,
Id A B
1 1 11 event1
2 2 12 event2
3 3 12 event1
4 4 12 event2
5 5 11 event1
6 6 11 event2
7 7 11 event3
8 8 13 event1
As @G. Grothendieck notes, your A variable seems to be a character. If that's the case then,
transform(d1, A = na.locf0(replace(A, A == "00", NA)))
It's not jiust thatAis character but also thatna.locf0applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it toAand not the entire data.frame.
– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also aboutna.locf0,..., when doesna.locfproduce a result with different length than the input?
– Sotos
Nov 20 '18 at 15:19
If there are leading NAs thenna.locfwill drop them unlessna.rm = FALSEis used.na.locf0hard codes tona.rm = FALSEbut only works on vectors.na.locfwas really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.
– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
add a comment |
In R you can do this with the tidyr function fill. First change all the 0 to NA with mutate, then the fill function will replace it with the last value.
library(dplyr)
library(tidyr)
df %>% mutate(A = ifelse(A==0,NA,A)) %>% fill(A)
# Id A B
#1 1 11 event1
#2 2 12 event2
#3 3 12 event1
#4 4 12 event2
#5 5 11 event1
#6 6 11 event2
#7 7 11 event3
#8 8 13 event1
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use correlated subquery in MySQL :
SELECT t.id,
(CASE WHEN A <> '00'
THEN A
ELSE (SELECT t1.A
FROM table t1
WHERE t1.id < t.id AND t1.A <> '00'
ORDER BY t1.id DESC
LIMIT 1
)
END) AS A, B
FROM table t;
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
add a comment |
You can use correlated subquery in MySQL :
SELECT t.id,
(CASE WHEN A <> '00'
THEN A
ELSE (SELECT t1.A
FROM table t1
WHERE t1.id < t.id AND t1.A <> '00'
ORDER BY t1.id DESC
LIMIT 1
)
END) AS A, B
FROM table t;
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
add a comment |
You can use correlated subquery in MySQL :
SELECT t.id,
(CASE WHEN A <> '00'
THEN A
ELSE (SELECT t1.A
FROM table t1
WHERE t1.id < t.id AND t1.A <> '00'
ORDER BY t1.id DESC
LIMIT 1
)
END) AS A, B
FROM table t;
You can use correlated subquery in MySQL :
SELECT t.id,
(CASE WHEN A <> '00'
THEN A
ELSE (SELECT t1.A
FROM table t1
WHERE t1.id < t.id AND t1.A <> '00'
ORDER BY t1.id DESC
LIMIT 1
)
END) AS A, B
FROM table t;
answered Nov 20 '18 at 15:05
Yogesh SharmaYogesh Sharma
32.5k51437
32.5k51437
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
add a comment |
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
Is there a more optimized way to do it. I am running it on 80 M rows and it's taking forever to run. Thanks
– No_body
Nov 27 '18 at 16:28
add a comment |
Here is a one-liner to do this in R. Basically, we replace 0s with NA and use zoo::na.locf() to fill with the last non-NA value, i.e.
transform(d1, A = zoo::na.locf(replace(A, A == 0, NA)))
which gives,
Id A B
1 1 11 event1
2 2 12 event2
3 3 12 event1
4 4 12 event2
5 5 11 event1
6 6 11 event2
7 7 11 event3
8 8 13 event1
As @G. Grothendieck notes, your A variable seems to be a character. If that's the case then,
transform(d1, A = na.locf0(replace(A, A == "00", NA)))
It's not jiust thatAis character but also thatna.locf0applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it toAand not the entire data.frame.
– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also aboutna.locf0,..., when doesna.locfproduce a result with different length than the input?
– Sotos
Nov 20 '18 at 15:19
If there are leading NAs thenna.locfwill drop them unlessna.rm = FALSEis used.na.locf0hard codes tona.rm = FALSEbut only works on vectors.na.locfwas really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.
– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
add a comment |
Here is a one-liner to do this in R. Basically, we replace 0s with NA and use zoo::na.locf() to fill with the last non-NA value, i.e.
transform(d1, A = zoo::na.locf(replace(A, A == 0, NA)))
which gives,
Id A B
1 1 11 event1
2 2 12 event2
3 3 12 event1
4 4 12 event2
5 5 11 event1
6 6 11 event2
7 7 11 event3
8 8 13 event1
As @G. Grothendieck notes, your A variable seems to be a character. If that's the case then,
transform(d1, A = na.locf0(replace(A, A == "00", NA)))
It's not jiust thatAis character but also thatna.locf0applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it toAand not the entire data.frame.
– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also aboutna.locf0,..., when doesna.locfproduce a result with different length than the input?
– Sotos
Nov 20 '18 at 15:19
If there are leading NAs thenna.locfwill drop them unlessna.rm = FALSEis used.na.locf0hard codes tona.rm = FALSEbut only works on vectors.na.locfwas really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.
– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
add a comment |
Here is a one-liner to do this in R. Basically, we replace 0s with NA and use zoo::na.locf() to fill with the last non-NA value, i.e.
transform(d1, A = zoo::na.locf(replace(A, A == 0, NA)))
which gives,
Id A B
1 1 11 event1
2 2 12 event2
3 3 12 event1
4 4 12 event2
5 5 11 event1
6 6 11 event2
7 7 11 event3
8 8 13 event1
As @G. Grothendieck notes, your A variable seems to be a character. If that's the case then,
transform(d1, A = na.locf0(replace(A, A == "00", NA)))
Here is a one-liner to do this in R. Basically, we replace 0s with NA and use zoo::na.locf() to fill with the last non-NA value, i.e.
transform(d1, A = zoo::na.locf(replace(A, A == 0, NA)))
which gives,
Id A B
1 1 11 event1
2 2 12 event2
3 3 12 event1
4 4 12 event2
5 5 11 event1
6 6 11 event2
7 7 11 event3
8 8 13 event1
As @G. Grothendieck notes, your A variable seems to be a character. If that's the case then,
transform(d1, A = na.locf0(replace(A, A == "00", NA)))
edited Nov 20 '18 at 15:19
answered Nov 20 '18 at 15:05
SotosSotos
30.5k51640
30.5k51640
It's not jiust thatAis character but also thatna.locf0applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it toAand not the entire data.frame.
– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also aboutna.locf0,..., when doesna.locfproduce a result with different length than the input?
– Sotos
Nov 20 '18 at 15:19
If there are leading NAs thenna.locfwill drop them unlessna.rm = FALSEis used.na.locf0hard codes tona.rm = FALSEbut only works on vectors.na.locfwas really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.
– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
add a comment |
It's not jiust thatAis character but also thatna.locf0applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it toAand not the entire data.frame.
– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also aboutna.locf0,..., when doesna.locfproduce a result with different length than the input?
– Sotos
Nov 20 '18 at 15:19
If there are leading NAs thenna.locfwill drop them unlessna.rm = FALSEis used.na.locf0hard codes tona.rm = FALSEbut only works on vectors.na.locfwas really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.
– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
It's not jiust that
A is character but also that na.locf0 applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it to A and not the entire data.frame.– G. Grothendieck
Nov 20 '18 at 15:14
It's not jiust that
A is character but also that na.locf0 applied to a vector will ensure that the result is the same length as the input. Also we only need to apply it to A and not the entire data.frame.– G. Grothendieck
Nov 20 '18 at 15:14
Ahh...right. I did not even notice that I apply it on the whole data frame. Also about
na.locf0 ,..., when does na.locf produce a result with different length than the input?– Sotos
Nov 20 '18 at 15:19
Ahh...right. I did not even notice that I apply it on the whole data frame. Also about
na.locf0 ,..., when does na.locf produce a result with different length than the input?– Sotos
Nov 20 '18 at 15:19
If there are leading NAs then
na.locf will drop them unless na.rm = FALSE is used. na.locf0 hard codes to na.rm = FALSE but only works on vectors. na.locf was really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.– G. Grothendieck
Nov 20 '18 at 15:21
If there are leading NAs then
na.locf will drop them unless na.rm = FALSE is used. na.locf0 hard codes to na.rm = FALSE but only works on vectors. na.locf was really developed for zoo objects where you usually don't need filling but if you want to apply it to a data.frame where filling is normally needed then you have to worry about such things.– G. Grothendieck
Nov 20 '18 at 15:21
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
Oh, I get it. Great. Thank you for the info @G.Grothendieck
– Sotos
Nov 20 '18 at 15:28
add a comment |
In R you can do this with the tidyr function fill. First change all the 0 to NA with mutate, then the fill function will replace it with the last value.
library(dplyr)
library(tidyr)
df %>% mutate(A = ifelse(A==0,NA,A)) %>% fill(A)
# Id A B
#1 1 11 event1
#2 2 12 event2
#3 3 12 event1
#4 4 12 event2
#5 5 11 event1
#6 6 11 event2
#7 7 11 event3
#8 8 13 event1
add a comment |
In R you can do this with the tidyr function fill. First change all the 0 to NA with mutate, then the fill function will replace it with the last value.
library(dplyr)
library(tidyr)
df %>% mutate(A = ifelse(A==0,NA,A)) %>% fill(A)
# Id A B
#1 1 11 event1
#2 2 12 event2
#3 3 12 event1
#4 4 12 event2
#5 5 11 event1
#6 6 11 event2
#7 7 11 event3
#8 8 13 event1
add a comment |
In R you can do this with the tidyr function fill. First change all the 0 to NA with mutate, then the fill function will replace it with the last value.
library(dplyr)
library(tidyr)
df %>% mutate(A = ifelse(A==0,NA,A)) %>% fill(A)
# Id A B
#1 1 11 event1
#2 2 12 event2
#3 3 12 event1
#4 4 12 event2
#5 5 11 event1
#6 6 11 event2
#7 7 11 event3
#8 8 13 event1
In R you can do this with the tidyr function fill. First change all the 0 to NA with mutate, then the fill function will replace it with the last value.
library(dplyr)
library(tidyr)
df %>% mutate(A = ifelse(A==0,NA,A)) %>% fill(A)
# Id A B
#1 1 11 event1
#2 2 12 event2
#3 3 12 event1
#4 4 12 event2
#5 5 11 event1
#6 6 11 event2
#7 7 11 event3
#8 8 13 event1
answered Nov 20 '18 at 15:07
jasbnerjasbner
2,026618
2,026618
add a comment |
add a comment |