Basic question, missing data vectors

I am very new to Studio and programming languages at large, so please bear with me!

I am working through the Cambio training for R as a complete beginner, and I am working on a section explaining how to handle data sets with missing values, represented by NA.

The tutorial gives three examples of how these data sets can be manipulated: is.na(), na.omit(), and complete.cases()

These are the examples used:

heights <- c(2, 4, 4, NA, 6)

heights[!is.na(heights)]

na.omit(heights)

heights[complete.cases(heights)]

Each of these functions return the answer
[1] 2 4 4 6
and I don't quite understand how these three functions are different from one another. In the explanation provided by the tutorial, they reference the removal or inclusion of "incomplete cases" or "complete cases", and I don't know if these are the same as missing values, which is the language that was used to reference NA. If someone could explain simply it would be much appreciated!

Both is.na() and complete.cases() return return logical values, TRUE or FALSE, depending on whether they encounter an NA. In you example, their output is used inside of the square brackets used to subset a vector, i.e. heights[logical vector]. That results in choosing the elements of heights that match with a TRUE value in the logical vector. To keep the non-NA elements of heights, the result of is.na is inverted with !.
A difference between is.na() and coplete.cases() is that complete.cases can conveniently handle a two dimensional object like a data frame. Used with a vector, as in your example, the only difference is that is.na() it TRUE for NA and complete.cases() is FALSE for NA.

1 Like

is.na() returns vector of TRUE / FALSE for NA values.

na.omit() removes NA values

complete.cases() returns vector of TRUE/FALSE for non-NA values.

If you use result of is.na() or complete.cases() inside an index, you can return the same result as na.omit().

I think it will be clear to you if you execute

is.na(heights)
na.omit(heights)
complete.cases(heights)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.