How do we replace NAs?
Replace NAs indicates, assigning the NAs with specific data such as integers, strings, float, and other types of data. So, these NAs can have the ability to accept data of any type. Replacing NAs with specific values is a crucial step in data preprocessing. Some of the methods to replace NAs are:
- is.na()
- replace()
Replacing the NAs by using is.na()
The function ‘is.na()’ is an in-built function which was provided by the R for replacing the NAs with specific values efficiently. In the function is.na(), “is” and “an” are the reserved words. The function ‘is.na()’ can able to works with dataframes, vectors, lists and matrices.
is.na(data)
In the below example, we created a dataframe by using the ‘data.frame()’. After, By using the function ‘df[is.na()]’, we replaced NAs with a character ‘g’.
print("The dataframe is")
#vector1
a=c("a",NA,"c","b")
#vector2
b=c(NA,"c",NA,"p")
#vector3
c=c("s","d","t",NA)
#creating dataframe
df=data.frame(a,b,c)
print(df)
print("Replacing the NAs")
df[is.na(df)]="g"
print(df)
Output:
a b c
1 a <NA> s
2 <NA> c d
3 c <NA> t
4 b p <NA>
[1] "Replacing the NAs"
a b c
1 a g s
2 g c d
3 c g t
4 b p g
In the below example, we created a matrix using the function ‘matrix()’. After we are replaced the NAs with a interger 5.
#declaring the matrix
matrix=matrix(c(9,8,NA,6,NA,4,NA,2,1),nrow=3)
print(matrix)
#replacement value
replacement_value=5
#Assigning the NAs with 5
matrix[is.na(matrix)]=replacement_value
print(matrix)
Output:
[,1] [,2] [,3]
[1,] 9 6 NA
[2,] 8 NA 2
[3,] NA 4 1
Assigning the NAs with 5
[,1] [,2] [,3]
[1,] 9 6 5
[2,] 8 5 2
[3,] 5 4 1
In the below example, we created a vector. After, by using the function ‘c[is.na()]’, we are replaced the NAs with a integer 2 .
print("Before changing the vector")
#vector
c=c(NA,4,6,NA)
print(c)
#printing the vector
print("After changing the vector")
c[is.na(c)]=2
print(c)
Output:
[1] "Before changing the vector"
[1] NA 4 6 NA
[1] "After changing the vector"
[1] 2 4 6 2
Using the function replace()
The function ‘replace()’ is an in-built function which was provided by the R for replacing the NAs with specific values efficiently. It is similar to the function ‘is.na()’ and can able to works with dataframes, vectors, lists and matrices.
replace( data,condition,value)
In the below example, we created a matrix by using the function ‘matrix()’. After, using the function ‘replace()’, we are replaced the NAs with a interger 8.
#declaring the matrix
matrix=matrix(c(9,8,NA,6,NA,4,NA,2,1),nrow=3)
print(matrix)
#replacement value
replacement_value=8
#Assigning the NAs with 8
matrix <- replace(matrix, is.na(matrix), replacement_value)
print(matrix)
Output:
[,1] [,2] [,3]
[1,] 9 6 NA
[2,] 8 NA 2
[3,] NA 4 1
[,1] [,2] [,3]
[1,] 9 6 8
[2,] 8 8 2
[3,] 8 4 1
In the below example, we created a dataframe by using the ‘data.frame()’ . After, using the function ‘replace()’, we replaced the NAs with a character ‘g’.
print("The dataframe is")
#vector1
a1=c("a","b",NA,"c")
#vector2
b1=c(NA,9,NA,1)
#vector3
c1=c("g","f","g",NA)
#creating dataframe
df=data.frame(a1,b1,c1)
#printing
print(df)
print("Replacing the NAs")
res=replace(df,is.na(df),8)
print(res)
Output:
[1] "The dataframe is"
a1 b1 c1
1 a NA g
2 b 9 f
3 <NA> NA g
4 c 1 <NA>
[1] "Replacing the NAs"
a1 b1 c1
1 a 8 g
2 b 9 f
3 8 8 g
4 c 1 8
In the below example, we created a vector. Using the function ‘replace()’, we replaced the NAs with a interger 8.
print("Before changing the vector")
#vector
c=c(NA,4,6,NA)
print(c)
#printing the vector
print("After replacing the NAs in vector ")
res=replace(c,is.na(c),8)
print(res)
Output:
[1] "Before changing the vector"
[1] NA 4 6 NA
[1] "After replacing the NAs in vector "
[1] 8 4 6 8
Replace NAs with specified values
In this article, we will examine various methods to replace NAs with specified values in the R programming language.