R fill vector with same value until a different one is found [duplicate]










0
















This question already has an answer here:



  • Replacing NAs with latest non-NA value

    13 answers



In R I have two vectors such as



2 NA NA 1 NA NA
1 NA 2 NA NA NA


And I would like them to be like



2 2 2 1 1 1
1 1 2 2 2 2


Any ideas?










share|improve this question













marked as duplicate by Sotos 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();

);
);
);
Nov 15 '18 at 14:37


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.













  • 2





    See zoo::na.locf from the zoo package.

    – nicola
    Nov 15 '18 at 14:21
















0
















This question already has an answer here:



  • Replacing NAs with latest non-NA value

    13 answers



In R I have two vectors such as



2 NA NA 1 NA NA
1 NA 2 NA NA NA


And I would like them to be like



2 2 2 1 1 1
1 1 2 2 2 2


Any ideas?










share|improve this question













marked as duplicate by Sotos 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();

);
);
);
Nov 15 '18 at 14:37


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.













  • 2





    See zoo::na.locf from the zoo package.

    – nicola
    Nov 15 '18 at 14:21














0












0








0









This question already has an answer here:



  • Replacing NAs with latest non-NA value

    13 answers



In R I have two vectors such as



2 NA NA 1 NA NA
1 NA 2 NA NA NA


And I would like them to be like



2 2 2 1 1 1
1 1 2 2 2 2


Any ideas?










share|improve this question















This question already has an answer here:



  • Replacing NAs with latest non-NA value

    13 answers



In R I have two vectors such as



2 NA NA 1 NA NA
1 NA 2 NA NA NA


And I would like them to be like



2 2 2 1 1 1
1 1 2 2 2 2


Any ideas?





This question already has an answer here:



  • Replacing NAs with latest non-NA value

    13 answers







r vector fill






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 14:20









OscarOscar

9019




9019




marked as duplicate by Sotos 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();

);
);
);
Nov 15 '18 at 14:37


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 Sotos 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();

);
);
);
Nov 15 '18 at 14:37


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.









  • 2





    See zoo::na.locf from the zoo package.

    – nicola
    Nov 15 '18 at 14:21













  • 2





    See zoo::na.locf from the zoo package.

    – nicola
    Nov 15 '18 at 14:21








2




2





See zoo::na.locf from the zoo package.

– nicola
Nov 15 '18 at 14:21






See zoo::na.locf from the zoo package.

– nicola
Nov 15 '18 at 14:21













2 Answers
2






active

oldest

votes


















0














With tidyverse:



 read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t

[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2





share|improve this answer






























    0














    vectors:



    a <- c(2, NA, NA, 1, NA, NA)
    b <- c(1, NA, 2, NA, NA, NA)


    As anonymous function:



    (function(x) 
    nai <- which(is.na(x))
    i <- which(!is.na(x))

    x[nai] <- x[i][findInterval(nai,i)]
    return(x)
    )


    Call:



     (function(x) 
    nai <- which(is.na(x))
    i <- which(!is.na(x))

    x[nai] <- x[i][findInterval(nai,i)]
    return(x)
    )(a)
    #[1] 2 2 2 1 1 1

    (function(x)
    nai <- which(is.na(x))
    i <- which(!is.na(x))

    x[nai] <- x[i][findInterval(nai,i)]
    return(x)
    )(b)
    #[1] 1 1 2 2 2 2





    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      With tidyverse:



       read.table(text="2 NA NA 1 NA NA
      1 NA 2 NA NA NA")%>%
      t()%>%
      data.frame()%>%
      mutate_all(funs(as.numeric(.)))%>%
      fill(c(X1,X2))%>%t

      [,1] [,2] [,3] [,4] [,5] [,6]
      X1 2 2 2 1 1 1
      X2 1 1 2 2 2 2





      share|improve this answer



























        0














        With tidyverse:



         read.table(text="2 NA NA 1 NA NA
        1 NA 2 NA NA NA")%>%
        t()%>%
        data.frame()%>%
        mutate_all(funs(as.numeric(.)))%>%
        fill(c(X1,X2))%>%t

        [,1] [,2] [,3] [,4] [,5] [,6]
        X1 2 2 2 1 1 1
        X2 1 1 2 2 2 2





        share|improve this answer

























          0












          0








          0







          With tidyverse:



           read.table(text="2 NA NA 1 NA NA
          1 NA 2 NA NA NA")%>%
          t()%>%
          data.frame()%>%
          mutate_all(funs(as.numeric(.)))%>%
          fill(c(X1,X2))%>%t

          [,1] [,2] [,3] [,4] [,5] [,6]
          X1 2 2 2 1 1 1
          X2 1 1 2 2 2 2





          share|improve this answer













          With tidyverse:



           read.table(text="2 NA NA 1 NA NA
          1 NA 2 NA NA NA")%>%
          t()%>%
          data.frame()%>%
          mutate_all(funs(as.numeric(.)))%>%
          fill(c(X1,X2))%>%t

          [,1] [,2] [,3] [,4] [,5] [,6]
          X1 2 2 2 1 1 1
          X2 1 1 2 2 2 2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 14:29









          jyjekjyjek

          1,504312




          1,504312























              0














              vectors:



              a <- c(2, NA, NA, 1, NA, NA)
              b <- c(1, NA, 2, NA, NA, NA)


              As anonymous function:



              (function(x) 
              nai <- which(is.na(x))
              i <- which(!is.na(x))

              x[nai] <- x[i][findInterval(nai,i)]
              return(x)
              )


              Call:



               (function(x) 
              nai <- which(is.na(x))
              i <- which(!is.na(x))

              x[nai] <- x[i][findInterval(nai,i)]
              return(x)
              )(a)
              #[1] 2 2 2 1 1 1

              (function(x)
              nai <- which(is.na(x))
              i <- which(!is.na(x))

              x[nai] <- x[i][findInterval(nai,i)]
              return(x)
              )(b)
              #[1] 1 1 2 2 2 2





              share|improve this answer



























                0














                vectors:



                a <- c(2, NA, NA, 1, NA, NA)
                b <- c(1, NA, 2, NA, NA, NA)


                As anonymous function:



                (function(x) 
                nai <- which(is.na(x))
                i <- which(!is.na(x))

                x[nai] <- x[i][findInterval(nai,i)]
                return(x)
                )


                Call:



                 (function(x) 
                nai <- which(is.na(x))
                i <- which(!is.na(x))

                x[nai] <- x[i][findInterval(nai,i)]
                return(x)
                )(a)
                #[1] 2 2 2 1 1 1

                (function(x)
                nai <- which(is.na(x))
                i <- which(!is.na(x))

                x[nai] <- x[i][findInterval(nai,i)]
                return(x)
                )(b)
                #[1] 1 1 2 2 2 2





                share|improve this answer

























                  0












                  0








                  0







                  vectors:



                  a <- c(2, NA, NA, 1, NA, NA)
                  b <- c(1, NA, 2, NA, NA, NA)


                  As anonymous function:



                  (function(x) 
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )


                  Call:



                   (function(x) 
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )(a)
                  #[1] 2 2 2 1 1 1

                  (function(x)
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )(b)
                  #[1] 1 1 2 2 2 2





                  share|improve this answer













                  vectors:



                  a <- c(2, NA, NA, 1, NA, NA)
                  b <- c(1, NA, 2, NA, NA, NA)


                  As anonymous function:



                  (function(x) 
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )


                  Call:



                   (function(x) 
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )(a)
                  #[1] 2 2 2 1 1 1

                  (function(x)
                  nai <- which(is.na(x))
                  i <- which(!is.na(x))

                  x[nai] <- x[i][findInterval(nai,i)]
                  return(x)
                  )(b)
                  #[1] 1 1 2 2 2 2






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 14:35









                  Andre ElricoAndre Elrico

                  5,73311229




                  5,73311229













                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線