Questions tagged [na]

NA is a missing value indicator (Not Available) in the R language.

NA is a logical constant of length 1 which contains a missing value indicator in the R language. NA can be coerced to any other vector type except raw.

There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values; all of these are reserved words in R.

2442 questions
819
votes
22 answers

How do I replace NA values with zeros in an R dataframe?

I have a data frame and some columns have NA values. How do I replace these NA values with zeroes?
Renato Dinhani
  • 30,005
  • 49
  • 125
  • 194
244
votes
21 answers

Convert Pandas column containing NaNs to dtype `int`

I read data from a .csv file to a Pandas dataframe as below. For one of the columns, namely id, I want to specify the column type as int. The problem is the id series has missing/empty values. When I try to cast the id column to integer while…
Zhubarb
  • 8,409
  • 17
  • 65
  • 100
204
votes
7 answers

Remove NA values from a vector

I have a huge vector which has a couple of NA values, and I'm trying to find the max value in that vector (the vector is all numbers), but I can't do this because of the NA values. How can I remove the NA values so that I can compute the max?
CodeGuy
  • 26,751
  • 71
  • 191
  • 310
149
votes
8 answers

Omit rows containing specific column of NA

I want to know how to omit NA values in a data frame, but only in some columns I am interested in. For example, DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22)) but I only want to omit the data where y is NA, therefore the result…
user1489975
  • 1,641
  • 2
  • 12
  • 8
133
votes
4 answers

How to avoid warning when introducing NAs by coercion

I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using as.numeric to convert a character vector. For example: x <- as.numeric(c("1", "2", "X")) Will give me a warning because it…
Corvus
  • 6,840
  • 7
  • 37
  • 64
100
votes
6 answers

Subset of rows containing NA (missing) values in a chosen column of a data frame

We have a data frame from a CSV file. The data frame DF has columns that contain observed values and a column (VaR2) that contains the date at which a measurement has been taken. If the date was not recorded, the CSV file contains the value NA, for…
John
  • 1,121
  • 2
  • 9
  • 12
87
votes
7 answers

How to delete columns that contain ONLY NAs?

I have a data.frame containing some columns with all NA values, how can I delete them from the data.frame. Can I use the function na.omit(...) specifying some additional arguments?
Lorenzo Rigamonti
  • 1,435
  • 6
  • 24
  • 34
84
votes
1 answer

Removing NA in dplyr pipe

I tried to remove NA's from the subset using dplyr piping. Is my answer an indication of a missed step. I'm trying to learn how to write functions using dplyr: > outcome.df%>% + group_by(Hospital,State)%>% +…
ITCoderWhiz
  • 843
  • 1
  • 6
  • 5
83
votes
14 answers

Change the Blank Cells to "NA"

Here's the link of my data. My target is to assign "NA" to all blank cells irrespective of categorical or numerical values. I am using na.strings="". But it's not assigning NA to all blank cells. ## reading the data dat <-…
S Das
  • 2,623
  • 5
  • 21
  • 40
83
votes
10 answers

How to replace NA values in a table for selected columns

There are a lot of posts about replacing NA values. I am aware that one could replace NAs in the following table/frame with the following: x[is.na(x)]<-0 But, what if I want to restrict it to only certain columns? Let's me show you an…
jnam27
  • 1,187
  • 1
  • 11
  • 16
74
votes
7 answers

Replacing character values with NA in a data frame

I have a data frame containing (in random places) a character value (say "foo") that I want to replace with a NA. What's the best way to do so across the whole data frame?
Roberto
  • 2,380
  • 5
  • 26
  • 28
72
votes
6 answers

Fastest way to detect if vector has at least 1 NA?

What is the fastest way to detect if a vector has at least 1 NA in R? I've been using: sum( is.na( data ) ) > 0 But that requires examining each element, coercion, and the sum function.
SFun28
  • 32,209
  • 43
  • 123
  • 233
71
votes
4 answers

How to create an empty matrix in R?

I am new to R. I want to fill in an empty matrix with the results of my for loop using cbind. My question is, how can I eliminate the NAs in the first column of my matrix. I include my code below: output<-matrix(,15,) ##generate an empty matrix…
user3276582
  • 711
  • 1
  • 5
  • 3
65
votes
6 answers

Why does NaN^0 == 1

Prompted by a spot of earlier code golfing why would: >NaN^0 [1] 1 It makes perfect sense for NA^0 to be 1 because NA is missing data, and any number raised to 0 will give 1, including -Inf and Inf. However NaN is supposed to represent…
Simon O'Hanlon
  • 54,383
  • 9
  • 127
  • 173
62
votes
7 answers

Subsetting R data frame results in mysterious NA rows

I've been encountering what I think is a bug. It's not a big deal, but I'm curious if anyone else has seen this. Unfortunately, my data is confidential, so I have to make up an example, and it's not going to be very helpful. When subsetting my…
chrisg
  • 631
  • 2
  • 6
  • 5
1
2 3
99 100