w3resource

Replace NA Values with 3 in R Data Frame

R Programming: Data frame Exercise-14 with Solution

Write a R program to replace NA values with 3 in a given data frame.

Sample Solution :

R Programming Code :

# Create a data frame named 'exam_data' with columns: 'name', 'score', 'attempts', 'qualify'
exam_data = data.frame(
  # Define the 'name' column with a list of names
  name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
  
  # Define the 'score' column with corresponding scores
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
  
  # Define the 'attempts' column with some NA values
  attempts = c(1, NA, 2, NA, 2, NA, 1, NA, 2, 1),
  
  # Define the 'qualify' column with qualification statuses
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)

# Print the message indicating the original data frame
print("Original dataframe:")

# Print the original data frame
print(exam_data)

# Replace all NA values in the data frame with 3
exam_data[is.na(exam_data)] = 3

# Print the message indicating the data frame after replacing NA values
print("After removing NA with 3, the said dataframe becomes:")

# Print the modified data frame
print(exam_data)

Output:

[1] "Original dataframe:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0       NA      no
3  Katherine  16.5        2     yes
4      James  12.0       NA      no
5      Emily   9.0        2      no
6    Michael  20.0       NA     yes
7    Matthew  14.5        1     yes
8      Laura  13.5       NA      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes
[1] "After removing NA with 3, the said dataframe becomes:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0        3      no
3  Katherine  16.5        2     yes
4      James  12.0        3      no
5      Emily   9.0        2      no
6    Michael  20.0        3     yes
7    Matthew  14.5        1     yes
8      Laura  13.5        3      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes

Explanation:

  • Create Data Frame (exam_data):
    • exam_data is created as a data frame with four columns: name, score, attempts, and qualify.
    • The name column contains names of individuals.
    • The score column contains numeric scores.
    • The attempts column contains numbers of attempts, with some missing values (NA).
    • The qualify column contains qualification status as 'yes' or 'no'.
  • Print Original Data Frame:
    • The print function displays a message "Original dataframe:".
    • The print function then shows the content of the exam_data data frame, showing the initial data including NA values.
  • Replace NA Values:
    • exam_data[is.na(exam_data)] = 3 replaces all NA values in the data frame with the number 3.
  • Print Modified Data Frame:
    • The print function displays a message "After removing NA with 3, the said dataframe becomes:".
    • The print function then shows the modified exam_data data frame, where all NA values have been replaced with 3.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create inner, outer, left, right join(merge) from given two data frames.
Next: Write a R program to change a column name of a given data frame.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/dataframe/r-programming-data-frame-exercise-14.php