Forward and backward fill data frame in R [duplicate]












7
















This question already has an answer here:




  • Replacing NAs with latest non-NA value

    13 answers




I have a data frame in with data as follows



Col1    Col2 
20 NA
25 NA
15 NA
NA 10
NA 15


and so on... I am looking to reshape it as follows



Col1     Col2
20 10
25 10
15 10
15 10
15 15


Basically to forward or backward fill NA values with the first occurring non NA value. I tried a variation of Carry last Factor observation forward and backward in group of rows in R, but was unable to get it to work...
Thanks in advance!










share|improve this question















marked as duplicate by 42- r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 26 '17 at 23:42


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.














  • 7





    tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

    – alistaire
    Mar 20 '17 at 23:23











  • @alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

    – FlyingPickle
    Mar 20 '17 at 23:42











  • everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

    – alistaire
    Mar 20 '17 at 23:44
















7
















This question already has an answer here:




  • Replacing NAs with latest non-NA value

    13 answers




I have a data frame in with data as follows



Col1    Col2 
20 NA
25 NA
15 NA
NA 10
NA 15


and so on... I am looking to reshape it as follows



Col1     Col2
20 10
25 10
15 10
15 10
15 15


Basically to forward or backward fill NA values with the first occurring non NA value. I tried a variation of Carry last Factor observation forward and backward in group of rows in R, but was unable to get it to work...
Thanks in advance!










share|improve this question















marked as duplicate by 42- r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 26 '17 at 23:42


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.














  • 7





    tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

    – alistaire
    Mar 20 '17 at 23:23











  • @alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

    – FlyingPickle
    Mar 20 '17 at 23:42











  • everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

    – alistaire
    Mar 20 '17 at 23:44














7












7








7









This question already has an answer here:




  • Replacing NAs with latest non-NA value

    13 answers




I have a data frame in with data as follows



Col1    Col2 
20 NA
25 NA
15 NA
NA 10
NA 15


and so on... I am looking to reshape it as follows



Col1     Col2
20 10
25 10
15 10
15 10
15 15


Basically to forward or backward fill NA values with the first occurring non NA value. I tried a variation of Carry last Factor observation forward and backward in group of rows in R, but was unable to get it to work...
Thanks in advance!










share|improve this question

















This question already has an answer here:




  • Replacing NAs with latest non-NA value

    13 answers




I have a data frame in with data as follows



Col1    Col2 
20 NA
25 NA
15 NA
NA 10
NA 15


and so on... I am looking to reshape it as follows



Col1     Col2
20 10
25 10
15 10
15 10
15 15


Basically to forward or backward fill NA values with the first occurring non NA value. I tried a variation of Carry last Factor observation forward and backward in group of rows in R, but was unable to get it to work...
Thanks in advance!





This question already has an answer here:




  • Replacing NAs with latest non-NA value

    13 answers








r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 11:46









Community

11




11










asked Mar 20 '17 at 23:11









FlyingPickleFlyingPickle

159210




159210




marked as duplicate by 42- r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 26 '17 at 23:42


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 42- r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 26 '17 at 23:42


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.










  • 7





    tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

    – alistaire
    Mar 20 '17 at 23:23











  • @alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

    – FlyingPickle
    Mar 20 '17 at 23:42











  • everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

    – alistaire
    Mar 20 '17 at 23:44














  • 7





    tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

    – alistaire
    Mar 20 '17 at 23:23











  • @alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

    – FlyingPickle
    Mar 20 '17 at 23:42











  • everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

    – alistaire
    Mar 20 '17 at 23:44








7




7





tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

– alistaire
Mar 20 '17 at 23:23





tidyr::fill works, e.g. df %>% fill(everything()) %>% fill(everything(), .direction = 'up')

– alistaire
Mar 20 '17 at 23:23













@alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

– FlyingPickle
Mar 20 '17 at 23:42





@alistaire: Thanks! I believe when you mention everything() its ncol(df) to account for doing this action over all columns in the DF?

– FlyingPickle
Mar 20 '17 at 23:42













everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

– alistaire
Mar 20 '17 at 23:44





everything() is a helper function from dplyr that just tells the function to act on all the columns. Specify the columns any way you could in dplyr::select.

– alistaire
Mar 20 '17 at 23:44












1 Answer
1






active

oldest

votes


















4














We can do this with na.locf from zoo



library(zoo)
na.locf(na.locf(df1), fromLast = TRUE)
# Col1 Col2
#1 20 10
#2 25 10
#3 15 10
#4 15 10
#5 15 15





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    We can do this with na.locf from zoo



    library(zoo)
    na.locf(na.locf(df1), fromLast = TRUE)
    # Col1 Col2
    #1 20 10
    #2 25 10
    #3 15 10
    #4 15 10
    #5 15 15





    share|improve this answer




























      4














      We can do this with na.locf from zoo



      library(zoo)
      na.locf(na.locf(df1), fromLast = TRUE)
      # Col1 Col2
      #1 20 10
      #2 25 10
      #3 15 10
      #4 15 10
      #5 15 15





      share|improve this answer


























        4












        4








        4







        We can do this with na.locf from zoo



        library(zoo)
        na.locf(na.locf(df1), fromLast = TRUE)
        # Col1 Col2
        #1 20 10
        #2 25 10
        #3 15 10
        #4 15 10
        #5 15 15





        share|improve this answer













        We can do this with na.locf from zoo



        library(zoo)
        na.locf(na.locf(df1), fromLast = TRUE)
        # Col1 Col2
        #1 20 10
        #2 25 10
        #3 15 10
        #4 15 10
        #5 15 15






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 '17 at 2:55









        akrunakrun

        413k13200275




        413k13200275

















            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas