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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:21:21+00:00 2026-05-30T17:21:21+00:00

I have a data.frame like this: (t=structure(list(count = c(NA, 2, NA, NA, NA, 8,

  • 0

I have a data.frame like this:

(t=structure(list(count = c(NA, 2, NA, NA, NA, 8, NA, NA, NA)), .Names = "count", row.names = c(NA,-9L), class = "data.frame"))
  count
1    NA
2     2
3    NA
4    NA
5    NA
6     8
7    NA
8    NA
9    NA

It is great that R has the NA value but sometimes it bites me. I often forget about it and try to do subsetting like this

> t[t$count>=1,]
[1] NA  2 NA NA NA  8 NA NA NA

And the output includes all NA rows. (which I don’t like)

After an hour of bug searching I change the code to this and that is what I want (imagine large dataframe a lots of non-NA resuls and only few “well-hidden” NAs):

> t[t$count>=1&!is.na(t$count),]
[1] 2 8

1.
Is there a feature of the “as.integer” function so that I could do something like this:

t[as.integer.EXCLUDE.NA(t$count)>=1,]

I would want to use such feature in other as.xxxx functions as well. Basically force R to stop think like a statistician and treat NA differently (e.g., like NULL (I am not sure NULL would solve my issue) (this did not work: t$count[3]<-NULL for some reason)

2.
or how would I run

transform(t, replace all NAs from count columns with 0)

or even better

transform(t, replace all NA from all numeric columns with 0 in t)

3.
any generic comments on making R forget about NAs are welcomed?

  • 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-30T17:21:22+00:00Added an answer on May 30, 2026 at 5:21 pm

    I do not like the choices that were made at the time of designing how “[” handles NA values either. The approach I take when I want to extract values using a logical test is to wrap the logical expression in which. This converts the result to a set of numbers and indexing succeeds without dragging along the unwanted NA’s:

    > t[ which(t$count >= 1), ]
    [1] 2 8
    # Or if you still want a dataframe result
    > t[ which(t$count >= 1), , drop=FALSE]
      count
    2     2
    6     8
    

    I also use subset since it handles NA’s in the same manner as which(logical). The one gotcha is when which is used with a “-” sign to retrieve the complement set. If there are no elements in the set satisfying the logical-condition, there will also be no elements in the -which(logical)-form. So I just do not use the -which combo:

    > t[ -which(t$count < 1), , drop=FALSE]
    [1] count
    <0 rows> (or 0-length row.names)
    > t[ which(t$count < 1), , drop=FALSE]
    [1] count
    <0 rows> (or 0-length row.names)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
i have a time series data like this: x <- structure(list(date = structure(c(1264572000, 1266202800,
i have a data frame like this A B value 1 1 0.123 2
i have a data frame that contains a data like this : V1 V2
I have a data set like this: structure(list(var1 = c(APE, APE, APE, APE, APE,
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a dataframe with numeric entries like this one test <- data.frame(x =
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a data frame that has dates, disk reads and disk writes data.

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.