Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:39:38+00:00 2026-06-12T05:39:38+00:00

I need to reduce the consecutive NA’s in a vector to a single NA,

  • 0

I need to reduce the consecutive NA’s in a vector to a single NA, without touching the other values.
So, for example, given a vector like this:

NA NA  8  7 NA NA NA NA NA  3  3 NA -1  4

what I need to get, is the following result:

NA  8  7 NA  3  3 NA -1  4

Currently, I’m using the following function:

reduceConsecutiveNA2One <- function(vect){
  enc <- rle(is.na(vect))

  # helper func
  tmpFun <- function(i){
    if(enc$values[i]){
      data.frame(L=c(enc$lengths[i]-1, 1), V=c(TRUE,FALSE))
    }else{
      data.frame(L=enc$lengths[i], V=enc$values[i])
    }
  }

  Df <- do.call(rbind.data.frame,lapply(1:length(enc$lengths),FUN=tmpFun))

  return(vect[rep.int(!Df$V,Df$L)])
}

and it seems to work fine, but probably there’s a simpler/faster way to accomplish this task.

Any suggestions ?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:39:39+00:00Added an answer on June 12, 2026 at 5:39 am

    Here’s one idea:

    x <- c(NA, NA,8,7,NA, NA, NA, NA, NA, 3, 3, NA, -1,  4)
    
    x[!(is.na(x) & diff(c(FALSE, is.na(x)))==0)]
    # [1] NA  8  7 NA  3  3 NA -1  4
    
    ## It also works for length-one vectors
    x <- NA
    x[!(is.na(x) & diff(c(FALSE, is.na(x)))==0)]
    # [1] NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to reduce this boolean expression to its simplest form. Its given that
Need a map reduce function by mongo in php This my mongo structure [_id]
I will build a system where I want to reduce single-point-of-failures, and I need
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I need to reduce the time to get an error when connecting to an
I need to reduce the size of apk file which currently is 40M. This
i am getting double values,i need to reduce to approximative value after . for
I need to reduce that space, ... How can I do it? Planificación, prioridad
I need to compress a string to reduce the size of a web service
If I don't do any map/reduce jobs, still JobTracker/TaskTrackers need to be running for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.