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
31
votes
7 answers

Remove columns from dataframe where some of values are NA

I have a dataframe where some of the values are NA. I would like to remove these columns. My data.frame looks like this v1 v2 1 1 NA 2 1 1 3 2 2 4 1 1 5 2 2 6 1 NA I tried to estimate the col mean and…
TTT
  • 3,844
  • 12
  • 64
  • 117
30
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
  • 3,625
  • 7
  • 32
  • 50
30
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,481
  • 2
  • 28
  • 36
29
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
  • 425
  • 2
  • 6
  • 9
28
votes
3 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
  • 1,866
  • 4
  • 22
  • 33
27
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
  • 787
  • 2
  • 9
  • 21
27
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,113
  • 6
  • 41
  • 48
26
votes
2 answers

NA values not being excluded in `cor`

To simplify, I have a data set which is as follows: b <- 1:6 # > b # [1] 1 2 3 4 5 6 jnk <- c(2, 4, 5, NA, 7, 9) # > jnk # [1] 2 4 5 NA 7 9 When I try: cor(b, jnk, na.rm=TRUE) I get: > cor(b, jnk, na.rm=T) Error in cor(b, jnk, na.rm = T) :…
Charlie
  • 387
  • 2
  • 4
  • 7
24
votes
1 answer

Test for NA and select values based on result

My question is rather simple. What I want is if A[i]!=NA, then C[i]=A[i], if A[i]=NA, then C[i]=B[i], however, I always get some error messages. Can somebody help me out? A B C NA 82.6 . NA 127.2 . NA 93.6 . NA 105 . NA 104 . NA …
Dan
  • 415
  • 1
  • 8
  • 14
23
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
  • 13,468
  • 6
  • 48
  • 90
23
votes
2 answers

Select NA in a data.table in R

How do I select all the rows that have a missing value in the primary key in a data table. DT = data.table(x=rep(c("a","b",NA),each=3), y=c(1,3,6), v=1:9) setkey(DT,x) Selecting for a particular value is easy DT["a",] Selecting for the…
Farrel
  • 9,584
  • 19
  • 57
  • 95
21
votes
5 answers

Replace all NA with FALSE in selected columns in R

I have a question similar to this one, but my dataset is a bit bigger: 50 columns with 1 column as UID and other columns carrying either TRUE or NA, I want to change all the NA to FALSE, but I don't want to use explicit loop. Can plyr do the trick?…
lokheart
  • 20,665
  • 32
  • 86
  • 161
21
votes
4 answers

Replace NA with Zero in dplyr without using list()

In dplyr I can replace NA with 0 using the following code. The issue is this inserts a list into my data frame which screws up further analysis down the line. I don't even understand lists or atomic vectors or any of that at this point. I just want…
stackinator
  • 3,909
  • 6
  • 23
  • 62
21
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
  • 5,474
  • 13
  • 63
  • 141
21
votes
4 answers

R: replacing NAs in a data.frame with values in the same position in another dataframe

I have a dataframe with some NA values: dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfa I would like to replace the NAs with values in the same position in another dataframe: dfrepair <-…
adam.888
  • 6,958
  • 16
  • 58
  • 97