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
53
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
  • 5,057
  • 12
  • 40
  • 57
52
votes
5 answers

Correct syntax for mutate_if

I would like to replace NA values with zeros via mutate_if in dplyr. The syntax below: set.seed(1) mtcars[sample(1:dim(mtcars)[1], 5), sample(1:dim(mtcars)[2], 5)] <- NA require(dplyr) mtcars %>% mutate_if(is.na,0) mtcars %>% …
Konrad
  • 14,406
  • 15
  • 86
  • 141
51
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,710
  • 2
  • 18
  • 27
49
votes
1 answer

One function to detect NaN, NA, Inf, -Inf, etc.?

Is there a single function in R that determines if a value is NA, NaN, Inf, -Inf, or otherwise not a well-formed number?
SFun28
  • 32,209
  • 43
  • 123
  • 233
47
votes
2 answers

aggregate methods treat missing values (NA) differently

Here's a simple data frame with a missing value: M = data.frame( Name = c('name', 'name'), Col1 = c(NA, 1) , Col2 = c(1, 1)) # Name Col1 Col2 # 1 name NA 1 # 2 name 1 1 When I use aggregate to sum variables by group ('Name') using the…
Ryan Walker
  • 2,928
  • 1
  • 20
  • 26
45
votes
11 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
  • 7,222
  • 5
  • 61
  • 118
41
votes
2 answers

Hiding NA's when printing a dataframe in knitr

I'm trying to print a table in knitr from a data frame using xtable . The table in the example below has the dimensions 3x7 but the third row only has one value, in the second column. The rest of the cells in the third row are 'NA'. When I compile…
Matt
  • 463
  • 1
  • 4
  • 8
41
votes
3 answers

Find names of columns which contain missing values

I want to find all the names of columns with NA or missing data and store these column names in a vector. # create matrix a <- c(1,2,3,4,5,NA,7,8,9,10,NA,12,13,14,NA,16,17,18,19,20) cnames <- c("aa", "bb", "cc", "dd", "ee") mymatrix <- matrix(a,…
lever
  • 604
  • 1
  • 5
  • 10
40
votes
3 answers

Dealing with missing values for correlations calculation

I have huge matrix with a lot of missing values. I want to get the correlation between variables. 1. Is the solution cor(na.omit(matrix)) better than below? cor(matrix, use = "pairwise.complete.obs") I already have selected only variables…
Delphine
  • 1,023
  • 5
  • 15
  • 22
40
votes
4 answers

model.matrix() with na.action=NULL?

I have a formula and a data frame, and I want to extract the model.matrix(). However, I need the resulting matrix to include the NAs that were found in the original dataset. If I were to use model.frame() to do this, I would simply pass it…
Vincent
  • 10,769
  • 6
  • 31
  • 32
37
votes
2 answers

Replace NA with 0 in a data frame column

Possible Duplicate: Set NA to 0 in R I have a data.frame with a column having NA values. I want to replace NA with 0 or any other value. I have tried a lot of threads and methods but it did not give me the result. I have tried the below…
Kunal Batra
  • 801
  • 3
  • 13
  • 22
35
votes
2 answers

Replace missing values (NA) with blank (empty string)

I have a dataframe with an NA row: df = data.frame(c("classA", NA, "classB"), t(data.frame(rep("A", 5), rep(NA, 5), rep("B", 5)))) rownames(df) <- c(1,2,3) colnames(df) <- c("class", paste("Year", 1:5, sep = "")) > df class Year1 Year2 Year3…
Mayou
  • 7,628
  • 15
  • 51
  • 91
33
votes
5 answers

Dealing with TRUE, FALSE, NA and NaN

Here is a vector a <- c(TRUE, FALSE, FALSE, NA, FALSE, TRUE, NA, FALSE, TRUE) I'd like a simple function that returns TRUE everytime there is a TRUE in "a", and FALSE everytime there is a FALSE or a NA in "a". The three following things do not…
Remi.b
  • 15,061
  • 23
  • 64
  • 143
31
votes
6 answers

Replace in a factor column

I want to replace values in a factors column with a valid value. But I can not find a way. This example is only for demonstration. The original data comes from a foreign csv file I have to deal with. df <- data.frame(a=sample(0:10, size=10,…
31
votes
2 answers

Count NAs per row in dataframe

I've got dataframe that has batch ID and the results of six tests performed on each batch. The data looks like this: batch_id test1 test2 test3 test4 test5 test6 001 0.121 NA 0.340 0.877 0.417 0.662 002 0.229 0.108 NA …
Shark7
  • 379
  • 1
  • 5
  • 8