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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:12:52+00:00 2026-06-17T01:12:52+00:00

A New Year’s quandary for the stackoverflow community which has been quite the help

  • 0

A New Year’s quandary for the stackoverflow community which has been quite the help by reading posts and answers in the past (this is my first question). I’ve found a work around, but I’m wondering if other approaches/solutions might be suggested.

I am attempting to remove trailing NA’s from a large data.frame, but those NA’s are only found in a few of the columns of the data.frame and I would like to retain all columns in the output. Here is a representative data subset.

df=data.frame(var1=rep("A", 8), var2=c("a","b","c","d","e","f","g","h"), var3=c(0,1,NA,2,3,NA,NA,NA), var4=c(0,0,NA,4,5,NA,NA,NA), var5=c(0,0,NA,0,2,4,NA,NA))

Goals of the process:

  1. Trim trailing NAs based on NA presence in var3,var4 and var5
  2. Retain all columns in final output
  3. Only remove trailing NAs (i.e. row 3 remains in record as a placeholder)
  4. Only trim if all columns have an NA (i.e. row 7 and 8, but not row 6)

Based on these goals, the solution should remove the last two rows of df:

df.output = df[-c(7,8),]

The behaviour of na.trim (in the zoo package) is ideal (as it limits removal to those NA’s at the end of the data.frame, with sides=”right”), and my work-around involved altering the na.trim.default function to include a subset term.

Any suggestions? Many thanks for any help.

EDIT: Just to complete this question, below is the function I created from the na.trim.default code which also works, but as noted, does require loading the zoo package.

na.trim.multiplecols <-  function (object, colrange, sides = c("both", "left", "right"),     is.na = c("any","all"),...) 
{
is.na <- match.arg(is.na)
nisna <- if (is.na == "any" || length(dim(object[,colrange])) < 1) {
complete.cases(object[,colrange])
}
else rowSums(!is.na(object[,colrange])) > 0
idx <- switch(match.arg(sides), left = cumsum(nisna) > 0, 
            right = rev(cumsum(rev(nisna) > 0) > 0), both = (cumsum(nisna) > 
                                                               0) &       rev(cumsum(rev(nisna)) > 0))
if (length(dim(object)) < 2) 
object[idx]
else object[idx, , drop = FALSE]

}
  • 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-17T01:12:54+00:00Added an answer on June 17, 2026 at 1:12 am

    Edit: First solution using base rle and apply

    t <- rle(apply(as.matrix(df[,3:5]), 1, function(x) all(is.na(x))))
    r <- ifelse(t$values[length(t$values)] == TRUE, t$lengths[length(t$lengths)], 0)
    head(df, -r)
    

    Second solution using Rle from package IRanges:

    require(IRanges)
    t <- min(sapply(df[,3:5], function(x) {
        o <- Rle(x)
        val <- runValue(o)
        if (is.na(val[length(val)])) {
            len <- runLength(o)
            out <- len[length(len)]
        } else {
            out <- 0
        }
    }))
    head(df, -t)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

and happy new year! I have a model which will hold some hundreds of
Happy new year :) I have a Chrome extension which is a popup that
First of all, Happy New Year to anybody out there reading this! I have
I've been re-writing my resume from scratch (good way to start the new year,
I have a countdown here which counts down to New Year's fine. But, for
Happy New Year SO users! I have an issue that I hope someone can
I have these 2 routes : routes.MapRoute(Agenda, ConfigurationManager.AppSettings[eventsUrl] + /{year}/{month}, MVC.Events.Index(), new { year
function GetDaysInMonth(month, year) { return 32 - new Date(year, month, 32).getDate(); } Ok, I
I'm relatively new to C++ (about one year of experience, on and off). I'm
as Microsoft are releasing a tablet later this year, with a new operating system

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.