![]() |
VOOZH | about |
In this article, we will discuss how to Replace NAs with strings in R Programming Language. NA stands for Not a Number, we can replace NA with strings in the dataframe.
Output:
name subjects address 1 sravan <NA> <NA> 2 ojaswi java hyd 3 <NA> jsp tenali 4 ramesh <NA> guntur
we can replace NA's with strings in particular column using replace_na() function, we have to import tidyr package
Syntax: dataframe$column_name%>% replace_na('string')
where
- dataframe is the input dataframe
- column_name is the column replace with the string
Output:
name subjects address 1 sravan <NA> <NA> 2 ojaswi java hyd 3 <NA> jsp tenali 4 ramesh <NA> guntur name subjects address 1 sravan <NA> <NA> 2 ojaswi java hyd 3 <NA> jsp tenali 4 ramesh <NA> guntur
Here we are using the same method as above but, to replace in multiple columns we have to specify multiple columns in a list function
Syntax: dataframe %>% replace_na(list(column1 = 'string', column2 = 'string',.,columnn = 'string',))
Output:
name subjects address 1 sravan <NA> <NA> 2 ojaswi java hyd 3 <NA> jsp tenali 4 ramesh <NA> guntur name subjects address 1 sravan <NA> <NA> 2 ojaswi java hyd 3 <NA> jsp tenali 4 ramesh <NA> guntur