Table of Contents
The “Is Not NA” function in R is used to check if a value is not missing or “NA” (not available). This function returns a logical value of TRUE or FALSE, indicating whether the value is not missing. It is useful for data cleaning and filtering operations, as it allows for the identification and removal of missing data. This function can be used in conditional statements, data manipulation, and data analysis tasks in R programming. By using “Is Not NA,” users can ensure the accuracy and completeness of their data sets.
Use “Is Not NA” in R
You can use the following syntax to return values in R that are not NA values:
#return only values that are not NA x <- x[!is.na(x)]
The following examples show how to use this syntax with both vectors and data frames in R.
Example 1: Return Values that are Not NA in Vector
The following code shows how to return the values in a vector that are not NA:
#create vector x <- c(1, 24, NA, 6, NA, 9) #return only values that are not NA x <- x[!is.na(x)] [1] 1 24 6 9
Example 2: Return Rows that are Not NA in One Column of Data Frame
The following code shows how to return the rows in a data frame that do not have an NA value in a specific column:
#create data frame
df <- data.frame(x=c(1, 24, NA, 6, NA, 9),
y=c(NA, 3, 4, 8, NA, 12),
z=c(NA, 7, 5, 15, 7, 14))
#view data frame
df
x y z
1 1 NA NA
2 24 3 7
3 NA 4 5
4 6 8 15
5 NA NA 7
6 9 12 14
#remove rows with NA in z column
df <- df[!(is.na(df$z)), ]
#view data frame
df
x y z
2 24 3 7
3 NA 4 5
4 6 8 15
5 NA NA 7
6 9 12 14Example 3: Return Rows that are Not NA in Several Columns
The following code shows how to return the rows in a data frame that do not have an NA value in one of several specific columns:
#create data frame
df <- data.frame(x=c(1, 24, NA, 6, NA, 9),
y=c(NA, 3, 4, 8, NA, 12),
z=c(NA, 7, 5, 15, 7, 14))
#view data frame
df
x y z
1 1 NA NA
2 24 3 7
3 NA 4 5
4 6 8 15
5 NA NA 7
6 9 12 14
#remove rows with NA in x or y column
df <- df[!(is.na(df$x)) & !(is.na(df$y)), ]
#view data frame
df
x y z
2 24 3 7
4 6 8 15
6 9 12 14Example 4: Return Rows that are Not NA in Any Column
The following code shows how to return the rows in a data frame that do not have an NA value in any column:
#create data frame
df <- data.frame(x=c(1, 24, NA, 6, NA, 9),
y=c(NA, 3, 4, 8, NA, 12),
z=c(NA, 7, 5, 15, 7, 14))
#view data frame
df
x y z
1 1 NA NA
2 24 3 7
3 NA 4 5
4 6 8 15
5 NA NA 7
6 9 12 14
#remove rows with NA in any column
df <- na.omit(df)
#view data frame
df
x y z
2 24 3 7
4 6 8 15
6 9 12 14Cite this article
stats writer (2024). How can I use “Is Not NA” in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-is-not-na-in-r/
stats writer. "How can I use “Is Not NA” in R?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-is-not-na-in-r/.
stats writer. "How can I use “Is Not NA” in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-is-not-na-in-r/.
stats writer (2024) 'How can I use “Is Not NA” in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-is-not-na-in-r/.
[1] stats writer, "How can I use “Is Not NA” in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I use “Is Not NA” in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
