Many languages have a way such as null or none to indicate a non-existent value.
Because R is designed to handle large volumes of (often messy) data, it has multiple forms of nothingness.
The overall aim is to flag missing or suspect values as they are encountered, then continue without raising an exception.
If a value really doesn't exist, it is represented by NULL. This is probably closest to what C or Python might do.
v <- c() # zero-length vector
v
#> NULL
is.null(v)
#> [1] TRUE
In many contexts, NULL values are simply ignored:
c(2, 3, NULL, 5)
#> [1] 2 3 5
For situations where a value exists but we don't know what it is, NA is used. For example, when counting vehicles traveling on a road, human observers might go off sick or automatic sensors break down, but the traffic continues to flow.
v <- c(1, 2, NA, 4, 5)
v
#> [1] 1 2 NA 4 5
is.na(v) # test for data gaps
#> [1] FALSE FALSE TRUE FALSE FALSE
Thus NA is a placeholder, warning humans that they need to make a decision about how to handle this.
Perhaps surprisingly, R has no standard way to check for emptiness, though some third-party packages implement their own functions such as is.empty().
Even generating an empty vector is not entirely obvious.
We saw in an earlier section that c() with no arguments returns NULL, not a vector.
is.vector(c())
#> [1] FALSE
Functions such as logical() or integer() take an argument specifying the desired length, which can be zero.
z <- logical(0)
is.vector(z)
#> [1] TRUE
length(z)
#> [1] 0
We can also generate a vector then remove every element, as described in the Vectors Concept. This results in a length-0 vector of an appropriate type.
v <- 5
v[-1] # delete the first element
#> numeric(0)
In this exercise you'll be writing code to print name badges for factory employees.
Employees have an ID, name, and department name.
Employee badge labels are formatted as follows: "[id] - name - DEPARTMENT".
Implement the print_name_badge function. It should take an ID, name, and a department.
It should return the badge label, with the department name in uppercase.
print_name_badge(67, "Katherine Williams", "Strategic Communication")
# => "[67] - Katherine Williams - STRATEGIC COMMUNICATION"
Due to a quirk in the computer system, new employees occasionally don't yet have an ID when they start working at the factory. As badges are required, they will receive a temporary badge without the ID prefix.
Extend the print_name_badge function. When the ID is missing, it should print a badge without it.
print_name_badge(NA, "Robert Johnson", "Procurement")
# => "Robert Johnson - PROCUREMENT"
Even the factory's owner has to wear a badge at all times.
However, an owner does not have a department and never will: they are above all the departments.
In this case, the label should print "OWNER" instead of the department name.
Extend the print_name_badge function. When the department is NULL, assume the badge belongs to the company owner.
print_name_badge(204, "Rachel Miller", NULL)
# => "[204] - Rachel Miller - OWNER"
Note that it is possible for the owner to also be a new employee.
print_name_badge(NA, "Rachel Miller", NULL)
# => "Rachel Miller - OWNER"
As a rough metric of how well the IDs are being issued, you want to see the combined salary of employees with no ID. A high value means lots are waiting, or the problem is affecting senior people: both are bad.
Implement the salaries_no_id function that takes a vector of IDs and a corresponding vector of salaries, and returns the sum of salaries for people with no ID yet.
Both vectors are the same length.
ids <- c(204, NA, 210, 352, NA, 263)
salaries <- c(23, 21, 47, 35, 17, 101) * 1000
salaries_no_id(ids, salaries)
# => 38,000
As an untested extra learning: If there are no employees without ID, what is the "empty vector" that you are summing?
Sign up to Exercism to learn and master R with 21 concepts111 exercises, and real human mentoring, all for free.