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
39
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
  • 17,389
  • 28
  • 87
  • 168
37
votes
5 answers

Replace a value NA with the value from another column in R

I want to replace the NA value in dfABy from the column A, with the value from the column B, based on the year of column year. For example, my df is: >dfABy A B Year 56 75 1921 …
Diego
  • 633
  • 1
  • 9
  • 16
35
votes
4 answers

dplyr join define NA values

Can I define a "fill" value for NA in dplyr join? For example in the join define that all NA values should be 1? require(dplyr) lookup <- data.frame(cbind(c("USD","MYR"),c(0.9,1.1))) names(lookup) <- c("rate","value") fx <-…
Triamus
  • 2,415
  • 5
  • 27
  • 37
35
votes
9 answers

Find columns with all missing values

I am writing a function, which needs a check on whether (and which!) column (variable) has all missing values (NA, ). The following is fragment of the function: test1 <- data.frame (matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3)) test2 <- data.frame…
SHRram
  • 4,127
  • 7
  • 35
  • 53
34
votes
3 answers

Count non-NA values by group

Here is my example mydf<-data.frame('col_1' = c('A','A','B','B'), 'col_2' = c(100,NA, 90,30)) I would like to group by col_1 and count non-NA elements in col_2 I would like to do it with dplyr. Here is what I tried: mydf %>% group_by(col_1) %>%…
user1700890
  • 7,144
  • 18
  • 87
  • 183
33
votes
4 answers

Counting non NAs in a data frame; getting answer as a vector

Say I have the following R data.frame ZZZ: ( ZZZ <- structure(list(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8, 8)), .Names = c("n", "m", "o"), row.names = c(NA, -3L), class = "data.frame") ) ## not run n m o 1 1 6 7 2 2 NA 8 3 NA NA 8 I…
Plsvn
  • 465
  • 2
  • 6
  • 9
32
votes
2 answers

How to replace empty string with NA in R dataframe?

My first approach was to use na.strings="" when I read the data in from a csv. This doesn't work for some reason. I also tried: df[df==''] <- NA Which gave me an error: Can't use matrix or array for column indexing. I tried just the column:…
mp3242
  • 489
  • 1
  • 5
  • 12
32
votes
2 answers

R function prcomp fails with NA's values even though NA's are allowed

I am using the function prcomp to calculate the first two principal components. However, my data has some NA values and therefore the function throws an error. The na.action defined seems not to work even though it is mentioned in the help file…
user969113
  • 2,349
  • 10
  • 44
  • 51
31
votes
6 answers

Replacing NAs in R with nearest value

I'm looking for something similar to na.locf() in the zoo package, but instead of always using the previous non-NA value I'd like to use the nearest non-NA value. Some example data: dat <- c(1, 3, NA, NA, 5, 7) Replacing NA with na.locf (3 is…
geoffjentry
  • 4,674
  • 3
  • 31
  • 37
29
votes
4 answers

How to remove row if it has a NA value in one certain column

My data called "dat": A B C NA 2 NA 1 2 3 1 NA 3 1 2 3 I want to be all rows to be removed if it has an NA in column B: A B C NA 2 NA 1 2 3 1 2 3 na.omit(dat) removes all rows with an NA not just the ones where the…
user9259005
  • 465
  • 1
  • 4
  • 12
29
votes
4 answers

Is it possible to set na.rm to TRUE globally?

For commands like max the option na.rm is set by default to FALSE. I understand why this is a good idea in general, but I'd like to turn it off reversibly for a while -- i.e. during a session. How can I require R to set na.rm = TRUE whenever it is…
Hugh
  • 15,521
  • 12
  • 57
  • 100
28
votes
3 answers

Replace NA in column with value in adjacent column

This question is related to a post with a similar title (replace NA in an R vector with adjacent values). I would like to scan a column in a data frame and replace NA's with the value in the adjacent cell. In the aforementioned post, the solution…
hubert_farnsworth
  • 797
  • 2
  • 9
  • 21
26
votes
4 answers

Locate index of rows in a dataframe that have the value of NA

Suppose that we have the following data frame: > dataset1 x 1 1 2 2 3 3 4 NA 5 5 I want to come up with a R command that computes the row index of the 1-column data frame that contains the value of 'NA'. More…
Jin-Dominique
  • 3,043
  • 6
  • 19
  • 28
25
votes
4 answers

logical(0) in if statement

This line: which(!is.na(c(NA,NA,NA))) == 0 produces logical(0) While this line if(which(!is.na(c(NA,NA,NA))) == 0){print('TRUE')} generates: Error in if (which(!is.na(c(NA, NA, NA))) == 0) { : argument is of length zero Why there is an error?…
user1700890
  • 7,144
  • 18
  • 87
  • 183
25
votes
2 answers

specifying "skip NA" when calculating mean of the column in a data frame created by Pandas

I am learning Pandas package by replicating the outing from some of the R vignettes. Now I am using the dplyr package from R as an example: http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html R script planes <-…
lokheart
  • 23,743
  • 39
  • 98
  • 169