Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for NA and select values based on result

Tags:

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  90.6    . NA  95.8    . NA  103 . NA  85.4    . NA  81.5    . NA  142.8   . NA  102.3   . NA  104 . NA  103 . NA  94.6    . NA  113.8   . NA  113.5   . NA  74.5    . NA  123.8   . NA  94  . NA  89.8    . NA  74  . NA  104 . NA  100.5   . NA  102.9   . NA  132.5   . NA  91  . NA  92.5    . NA  97  . NA  90  . 54.6    51.7    . NA  61  . NA  80  . NA  77.5    . NA  NA  . NA  80.6    . NA  44.6    . NA  37.6    . NA  27  . NA  NA  . NA  NA  . NA  NA  . 
like image 305
Dan Avatar asked Sep 20 '11 15:09

Dan


People also ask

How do I check if a value is na in R?

To test if a value is NA, use is.na(). The function is.na(x) returns a logical vector of the same size as x with value TRUE if and only if the corresponding element in x is NA.

How do you check for NA values in a data set in R?

To check which value in NA in an R data frame, we can use apply function along with is.na function. This will return the data frame in logical form with TRUE and FALSE.

How do you know if a value is not na?

The is.na function returns TRUE if the value is NA, and returns FALSE if not as the name explains. Now you have a new column with TRUE/FALSE data. If you want to have opposite values, like FALSE for NA, and TRUE for non NA values, you can add an exclamation mark (“!”) at the beginning of the formula.

How do I select missing values in R?

In R, the easiest way to find columns that contain missing values is by combining the power of the functions is.na() and colSums(). First, you check and count the number of NA's per column. Then, you use a function such as names() or colnames() to return the names of the columns with at least one missing value.


1 Answers

use is.na :

DF <- within(DF,    C <- ifelse(!is.na(A),A,B) ) 

with DF being your dataframe.

like image 164
Joris Meys Avatar answered Apr 27 '23 01:04

Joris Meys