Merge Two Unequal DataFrames and Replace NA with 0 in R
In this article we are going to merge two unequal dataframes and then replace NA values in the resultant dataframe with 0 using R programming language.
Dataframes in use:
Merging the dataframes
We can merge the two dataframes by using merge() function.
merge(dataframe1, dataframe2, by = “column”,all = TRUE)
- dataframe1 is the first dataframe
- dataframe2 is the second dataframe
- column is the column name to be merged based on this column
Example: R program to merge the dataframes by id column
Replacing NA with 0 in dataframe
We can replace NA by using is.na() function. By setting data to 0 inside the index the job can be easily done.
data[is.na(data)] = 0
Where, data is the merged dataframe with NA values
Example: R program to replace NA with 0