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 8397839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:53:23+00:00 2026-06-09T20:53:23+00:00

With the help of sebastian-c, I figured out my problem with daily data. Please

  • 0

With the help of sebastian-c, I figured out my problem with daily data. Please see: R ifelse condition: frequency of continuously NA

And now I have a data set with hourly data:

set.seed(1234)  
day <- c(rep(1:2, each=24))  
hr <- c(rep(0:23, 2))  
v <- c(rep(NA, 48))   
A <- data.frame(cbind(day, hr, v))  
A$v <- sample(c(NA, rnorm(100)), nrow(A), prob=c(0.5, rep(0.5/100, 100)), replace=TRUE)  

What I need to do is: If there are more(>=) 4 continuously missing day-hours(7AM-7PM) or >= 3 continuously missing night-hours(7PM-7AM), I will delete the entire day from the data frame, otherwise just run linear interpolation. Thus, the second day should be entirely deleted from the data frame since there are 4 continuously NA during day-time (7AM-10AM). The result is preferably remain data frame. Please help, thank you!

  • 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-09T20:53:24+00:00Added an answer on June 9, 2026 at 8:53 pm

    If I modify the NA_run function from the question you linked to take a variable named v instead of value and return the boolean rather than the data.frame:

    NA_run <- function(x, maxlen){
      runs <- rle(is.na(x$v))
      any(runs$lengths[runs$values] >= maxlen)
    }
    

    I can then write a wrapper function to call it twice for daytime and nighttime:

    dropfun <- function(x) {
      dt <- x$hr > 7 & x$hr < 19
      daytime <- NA_run(x[dt,], 4)
      nighttime <- NA_run(x[!dt,], 3)
    
      any(daytime, nighttime)
    }
    

    Which gives me a data.frame of days to drop.

    > ddply(A, .(day), dropfun)
      day    V1
    1   1  TRUE
    2   2 FALSE
    > 
    

    We can alter the dropfun to return the dataframe instead though:

    dropfun <- function(x) {
      dt <- x$hr > 7 & x$hr < 19
      daytime <- NA_run(x[dt,], 4)
      nighttime <- NA_run(x[!dt,], 3)
    
      if(any(daytime, nighttime)) NULL else x
    }
    
    > ddply(A, .(day), dropfun)
       day hr           v
    1    2  0          NA
    2    2  1          NA
    3    2  2  2.54899107
    4    2  3          NA
    5    2  4 -0.03476039
    6    2  5          NA
    7    2  6  0.65658846
    8    2  7  0.95949406
    9    2  8          NA
    10   2  9  1.08444118
    11   2 10  0.95949406
    12   2 11          NA
    13   2 12 -1.80603126
    14   2 13          NA
    15   2 14          NA
    16   2 15  0.97291675
    17   2 16          NA
    18   2 17          NA
    19   2 18          NA
    20   2 19 -0.29429386
    21   2 20  0.87820363
    22   2 21          NA
    23   2 22  0.56305582
    24   2 23 -0.11028549
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Help me please. I have problem with encoding response string after GET request: var
Help me, please, solve one problem. I have project, which uses Nhibernate and Fluent
Help please on how to extract data like 15,Teaching Periods 1 and 2.,Max 150.
Help me please to find out what's the difference between these two code snippets:
help out a noob with a simple web development question?? I want to create
help me figure out how to set an event on the parent element (
HELP! I need help setting up tipsy I had it working earlier but now
Help! I have a clickonce app built and published using .Net 2.0. This app
Help please. How can I format the DateTime column in SQLServer 2005 to store
I have a problem when trying to parse an XML from a string. I'm

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.