October 2017
Beginner to intermediate
236 pages
7h 38m
English
Inside the if() statement, the test scenario any(is.na(x)) produces TRUE or FALSE depending on the input vector x. If the test scenario is TRUE, then the output will be a line of text There are missing values in x. Now, print the location index of the missing values.
The which() function searches for the position index where is.na(x) is TRUE and returns the value for that position. The cat() function prints the text in the R console. Similar code has been used for the character vector y too. In fact, to check missing values for any input, either numeric or character is.na() works fine. The output will look as follows:
There are missing values in x [1] 4 8 There are missing values in x [1] 3