Missing Values: NA
Part of the Fundamentals section of Coddy's R journey — lesson 10 of 78.
In real-world data analysis, you'll often encounter situations where information is missing or unavailable. R provides a special value called NA to handle these cases.
NA stands for "Not Available" and represents a missing or undefined value. It's a special logical constant that serves as a placeholder when data is unknown or absent. Unlike other data types you've learned, NA doesn't represent any actual value - it represents the absence of a value.
You can assign NA to any variable just like you would with other values:
missing_data <- NA
unknown_score <- NAThis is particularly useful when you're working with datasets where some information might be incomplete. For example, if you're collecting user information but someone hasn't provided their age, you can store NA instead of leaving the variable empty or using an arbitrary number like 0, which could be misleading.
Challenge
EasyCreate variables to represent missing information in a student database. First, create a variable called student_age and assign it NA to represent an unknown age. Then create a variable called test_score and assign it NA to represent a missing test score. Finally, create a variable called graduation_year and assign it NA to represent an unknown graduation year.
After creating all three variables, use the print() function to display each variable in this exact order: student_age, test_score, and graduation_year.
Cheat sheet
R uses NA (Not Available) to represent missing or undefined values. NA is a special logical constant that serves as a placeholder when data is unknown or absent.
You can assign NA to variables:
missing_data <- NA
unknown_score <- NAUsing NA is preferable to using arbitrary values like 0 or empty strings when data is genuinely missing, as it clearly indicates the absence of information.
Try it yourself
# TODO: Write your code here
# Create the three variables with NA values
# Then print each variable in the specified orderThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values