Questions tagged [na]

NA is a missing value indicator (Not Available) in the Most languages like R language, spreadsheets like Excel and Google sheets.

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.

3057 questions
169
votes
10 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,841
  • 2
  • 14
  • 8
142
votes
5 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
  • 7,548
  • 9
  • 42
  • 68
128
votes
7 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,401
  • 2
  • 11
  • 12
125
votes
7 answers

Filter for complete cases in data.frame using dplyr (case-wise deletion)

Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known…
user2503795
  • 4,035
  • 2
  • 34
  • 49
107
votes
9 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,705
  • 8
  • 25
  • 36
101
votes
12 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,367
  • 2
  • 12
  • 16
90
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,800
  • 6
  • 29
  • 28
90
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
  • 903
  • 1
  • 6
  • 5
77
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
  • 801
  • 2
  • 8
  • 5
76
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.
Suraj
  • 35,905
  • 47
  • 139
  • 250
76
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
  • 761
  • 1
  • 5
  • 3
70
votes
10 answers

Combine column to remove NA's

I have some columns in R and for each row there will only ever be a value in one of them, the rest will be NA's. I want to combine these into one column with the non-NA value. Does anyone know of an easy way of doing this. For example I could have…
user1165199
  • 6,351
  • 13
  • 44
  • 60
67
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
  • 58,647
  • 14
  • 142
  • 184
63
votes
14 answers

suppress NAs in paste()

Regarding the bounty Ben Bolker's paste2-solution produces a "" when the strings that are pasted contains NA's in the same position. Like this, > paste2(c("a","b", "c", NA), c("A","B", NA, NA)) [1] "a, A" "b, B" "c" "" The fourth element is an…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
60
votes
3 answers

What is the difference between and NA?

I have a factor named SMOKE with levels "Y" and "N". Missing values were replaced with NA (from the initial level "NULL"). However when I view the factor I get something like this: head(SMOKE) # N N Y Y N # Levels: Y N Why is R displaying NA…
oort
  • 1,840
  • 2
  • 20
  • 29
1
2 3
99 100