Replace Character Value with NA in R
Last Updated :
29 Sep, 2021
In this article, we are going to see how to replace character value with NA in R Programming Language.
We can replace a character value with NA in a vector and in a dataframe.
Example 1: Replace Character Value with NA in vector
In a vector, we can replace it by using the indexing operation.
Syntax:
vector[vector == “character”] <- NA
R
vector = c ( "a" , "d" , "A" , "g" , "S" ,
"S" , "t" , "S" , "e" , "S" )
final = vector
final[final == "S" ] <- NA
print (final)
|
Output:
[1] "a" "d" "A" "g" NA NA "t" NA "e" NA
Example 2: Replace Character Value with NA in Dataframe
Replace character value with NA in dataframe.
Syntax:
dataframe[dataframe== “character”] <- NA
R
data = data.frame ( "a" , "d" , "A" , "g" , "S" ,
"S" , "t" , "S" , "e" , "S" )
final = data
final[final == "A" ] <- NA
print (final)
|
Output:
X.a. X.d. X.A. X.g. X.S. X.S..1 X.t. X.S..2 X.e. X.S..3
1 a d <NA> g S S t S e S
Share your thoughts in the comments
Please Login to comment...