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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:13:00+00:00 2026-05-24T11:13:00+00:00

I was using sum(is.na(my.df)) to check whether my data frame contained any NAs, which

  • 0

I was using sum(is.na(my.df)) to check whether my data frame contained any NAs, which worked as I expected, but sum(is.nan(my.df)) did not work as I expected.

> my.df <- data.frame(a=c(1, 2, 3), b=c(5, NA, NaN))
> my.df
  a   b
1 1   5
2 2  NA
3 3 NaN
> is.na(my.df)
         a     b
[1,] FALSE FALSE
[2,] FALSE  TRUE
[3,] FALSE  TRUE
> is.nan(my.df)
    a     b 
FALSE FALSE 
> sum(is.na(my.df))
[1] 2
> sum(is.nan(my.df))
[1] 0

Oh dear.
Is there a reason for the inconsistency in behaviour? Is it for a lack of implementation, or is it intentional? What does the return value of is.nan(my.df) signify? Is there a good reason not to use is.nan() on a whole data frame?

In the documentation for is.na( ) and is.nan( ), the argument types seem the same (although they don’t specifically list data frames):

is.na(): x R object to be tested: the default methods handle atomic vectors, lists and pairlists.
is.nan(): x R object to be tested: the default methods handle atomic vectors, lists and pairlists.

  • 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-24T11:13:00+00:00Added an answer on May 24, 2026 at 11:13 am

    From ?is.nan:

    All elements of logical,integer and raw vectors are considered not to be NaN, and
    elements of lists and pairlists are also unless the element is a length-one numeric
    or complex vector whose single element is NaN.
    

    The columns of a data frame are technically “elements of a list”, so is.nan(df) returns a vector with length equal to the number of columns of the data frame, which is TRUE only if the column consists of a single NaN element:

    > is.nan(data.frame(a=NaN,b=NA,c=1))
        a     b     c 
     TRUE FALSE FALSE 
    

    If you want behavior matching that of is.na, use apply:

    sum(apply(my.df,2,is.nan))
    

    The answer is 1 rather than 2 because is.nan(NA) is FALSE …

    edit: alternatively, you can just turn the data frame into a matrix:

     sum(is.nan(as.matrix(my.df)))
    

    update: this behaviour changed shortly (two months) after the question was asked, in R version 2.14 (October 2011): from the NEWS file,

    o The default methods for is.finite(), is.infinite() and is.nan() now signal an error if their argument is not an atomic vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was using python to create a dictionary but as my data got larger
I'm looking for help using sum() in my SQL query: SELECT links.id, count(DISTINCT stats.id)
I am using this code to sum values from multiple radio buttons : $(document).ready(function(){
I am calculating percentages in Analysis Services 2008 using MDX by dividing the sum
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I am using Compute for summing up a datatable which has a condition. Sometimes,
How would one test whether data structure built properly? I'm implementing kind of modified
I'm writing a performance-critical .NET application which makes heavy use of multithreading. Using the
I'm trying to build up the proper data structure using a pseudo tcp header,
I have a List[Option[Int]] and want to sum over it using applicative functors. From

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.