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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:58:26+00:00 2026-05-20T00:58:26+00:00

Say I have the following R data.frame ZZZ : ( ZZZ <- structure(list(n =

  • 0

Say I have the following R data.frame ZZZ:

( ZZZ <- structure(list(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8, 
8)), .Names = c("n", "m", "o"), row.names = c(NA, -3L), class = "data.frame") )

## not run
   n  m o
1  1  6 7
2  2 NA 8
3 NA NA 8

I want to know, in the form of a vector, how many non-NAs I’ve got. I want the answer available to me as:

2, 1, 3

When I use the command length(ZZZ), I get 3, which of course is the number of vectors in the data.frame, a valuable enough piece of information.

I have other functions that operate on this data.frame and give me answers in the form of vectors, but, dang-it, length doesn’t operate like that.

  • 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-05-20T00:58:27+00:00Added an answer on May 20, 2026 at 12:58 am

    Try this:

    # define "demo" dataset
    ZZZ <- data.frame(n=c(1,2,NA),m=c(6,NA,NA),o=c(7,8,8))
    # apply the counting function per columns
    apply(ZZZ, 2, function(x) length(which(!is.na(x))))
    

    Having run:

    > apply(ZZZ, 2, function(x) length(which(!is.na(x))))
    n m o 
    2 1 3 
    

    If you really insist on returning a vector, you might use as.vector, e.g. by defining this function:

    nonNAs <- function(x) {
        as.vector(apply(x, 2, function(x) length(which(!is.na(x)))))
        }
    

    You could simply run nonNAs(ZZZ):

    > nonNAs(ZZZ)
    [1] 2 1 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a solution with the following structure: Project.DAL - Data access
Let's say I have the following data frame. dat <- data.frame(city=c(Chelsea,Brent,Bremen,Olathe,Lenexa,Shawnee), tag=c(rep(AlabamaCity,3), rep(KansasCity,3))) I
Let just say I have the following data models: public class Account { public
Say we have the following simple data-frame of date-value pairs, where some dates are
I have written the following code: library(ggplot2) data <- structure(list(x = c(1L, 6L, 3L,
Say I have the following data I want to use in my FOR XML
All right, let's say I have the following plot. df = data.frame(date=c(rep(2008:2013, by=1)), value=c(303,407,538,696,881,1094))
Let's say I have the following hypothetical data structure: create table country ( country_id
Let's say I have a table containing following data: | id | t0 |
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is

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.