How to insert values in next couple of rows based on the previous row [duplicate]












1
















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










share|improve this 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.























    1
















    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










    share|improve this 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.





















      1












      1








      1









      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










      share|improve this question

















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.


























          3 Answers
          3






          active

          oldest

          votes


















          1














          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;





          share|improve this answer
























          • 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



















          1














          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)))





          share|improve this answer


























          • 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











          • 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



















          0














          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





          share|improve this answer






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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;





            share|improve this answer
























            • 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
















            1














            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;





            share|improve this answer
























            • 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














            1












            1








            1







            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;





            share|improve this answer













            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;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            1














            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)))





            share|improve this answer


























            • 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











            • 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
















            1














            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)))





            share|improve this answer


























            • 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











            • 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














            1












            1








            1







            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)))





            share|improve this answer















            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)))






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 15:19

























            answered Nov 20 '18 at 15:05









            SotosSotos

            30.5k51640




            30.5k51640













            • 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











            • 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



















            • 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











            • 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

















            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











            0














            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





            share|improve this answer




























              0














              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





              share|improve this answer


























                0












                0








                0







                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





                share|improve this answer













                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 15:07









                jasbnerjasbner

                2,026618




                2,026618















                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word