# R Program to replace NA values with 5 in a given data frame

March 3, 2022, Learn eTutorial
1101

## How to replace NA values with 5 in a given data frame

Here we are explaining how to write an R program to replace NA values with 5 in a given data frame. Here we are using a built-in function data.frame() for this. A data frame is used for storing data tables which has a list of vectors with equal length. The data frames are created by function data.frame(), which has tightly coupled collections of variables. The syntax of this function is,

``````

data.frame(…, row.names = NULL, check.rows = FALSE,check.names = TRUE, fix.empty.names = TRUE,stringsAsFactors = default.stringsAsFactors())
```
```

Where dots(...) indicates the arguments are of either the form value or tag = value and row. name is a NULL or a single integer or character string.

## How to replace NA values with 5 in a given data frame in the R program

Below are the steps used in the R program to replace NA values with 5 in a given data frame. In this R program, we directly give the data frame to a built-in function. Here we are using variables E, N, S, A, Q for holding different types of vectors. Call the function data.frame() for creating dataframe. Finally, replace NA values with 5 from a given data frame by calling like E[is.na(E)] = 5

## ALGORITHM

STEP 1: Assign variables E,N,S,A,Q with vector values

STEP 2: First print original data frame

STEP 3: Replace NA values with 5 from a given data frame as E[is.na(E)] = 5

STEP 5: print the final data frame

## R Source Code

```                                          ```E = data.frame(
N = c('Jhon', 'Hialy', 'Albert', 'James', 'Delma'),
S = c(10, 9.5, 12.2, 11, 8),
A = c(2, NA, 2, NA, 1),
Q = c('yes', 'no', 'yes', 'no', 'no')
)
print("Original dataframe:")
print(E)
E[is.na(E)] = 5
print("After removing NA with 5, the dataframe is:")
print(E)```
```

## OUTPUT

```[1] "Original dataframe:"
name    score attempts qualify
1  Jhon       10        2     yes
2  Hialy      9.5       NA     no
3  Albert     12.2      2     yes
4  James      11        NA    no
5  Delma      8         1     no
[1] "After removing NA with 5, the dataframe is:"
name    score attempts qualify
1  Jhon       10        2     yes
2  Hialy      9.5       5     no
3  Albert     12.2      2     yes
4  James      11        5    no
5  Delma      8         1     no```

VIEW ALL
VIEW ALL