How To Replace na Values with Zeros In R Dataframe
Let us create a random data for this example.
Below will create a dataframe with 5 columns and 5 rows.
df <- data.frame(matrix(NA, nrow = 5, ncol = 5))
head(df,1)
To replace na values, we can use is.na() function as shown below.
df[is.na(df)] <- 0
Let check our dataframe now.
head(df)
Ok now we have all the values replaced with zeros.
Replace na with zeros in a column of Dataframe in R
Let us recreate our dataframe with na values.
df <- data.frame(matrix(NA, nrow = 5, ncol = 5))
For example- replace na in 3rd column with all zeros.
df[,3] <- 0
df
Replace na with zeros in a row of Dataframe in R
For example - replace na in ist row with all zeros.
df[1,] <-0
df
Related Notebooks
- PySpark Replace Values In DataFrames
- How To Write DataFrame To CSV In R
- How to Create DataFrame in R Using Examples
- How To Analyze Yahoo Finance Data With R
- How To Plot Histogram In R
- How To Use Grep In R
- How To Run Logistic Regression In R
- Introduction To R DataFrames
- How to Sort Pandas DataFrame with Examples