Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does x[NA] yield an NA vector the same length as x?

Tags:

r

The code is like this

x <- 1:5
x[NA]

Why does it produce 5 NAs?

like image 397
Moksh Avatar asked Jul 07 '16 07:07

Moksh


People also ask

What is the magnitude of the cross product of a vector?

However, the cross product is closely connected to the determinant, which is evident if you look at the formulas for each. As someone else said, the magnitude of the cross product is equal to this area. This is similar to how the dot product gives the magnitude of the projection of vector a unto vector b.

What is the relationship between the cross product and the determinant?

However, the cross product is closely connected to the determinant, which is evident if you look at the formulas for each. As someone else said, the magnitude of the cross product is equal to this area.

Is the cross product equal to the area of the area?

No, that's the determinant. However, the cross product is closely connected to the determinant, which is evident if you look at the formulas for each. As someone else said, the magnitude of the cross product is equal to this area.

Can x[0]==0 be used in a vector?

For instance when you make a table of values and say there are zero of that cell you need to hold that that cell made from a string in a vector has no values. it would not be a appropriate to have x[0]==0 as it's not the numeric value of zero but the absence of any value.


1 Answers

The answer to this question has two sides:

How is NA interpreted when indexing matrices?

In one of the links provided by @alexis_laz, I found a very well structured explanation of how TRUE, FALSE and NA are interpreted when indexing matrices:

Logical indices tell R which elements to include or exclude.

You have three options: TRUE, FALSE and NA

They serve to indicate whether or not the index represented in that position should be included. In other words:

TRUE  == "Include the elment at this index"
FALSE == "Do not include the element at this index"
NA    == "Return NA instead of this index" #  loosely speaking

For example:

x <- 1:6
x[ c(TRUE, FALSE, TRUE, NA, TRUE, FALSE)]
# [1]  1  3 NA  5

An important detail is that the default storage mode for an isolated NA value is logical (try typeof(NA)). You can choose the storage mode of the NA by using NA_integer_, NA_real_ (for double), NA_complex_ or NA_character_.

Why 5 NA and not just 1?

When the length of the indices is smaller than the length of vector x, the indexing will start over to also index the values in x that have not been indexed yet. In other words, R will automatically "recycle" the indices:

(...) However, standard recycling rules apply. So in the previous example, if we drop the last FALSE, the index vector is recycled, the first element of the index is TRUE, and hence the 6th element of x is now included

x <- 1:6
x[c(TRUE, FALSE, TRUE, NA, TRUE)]
#  [1]  1  3 NA  5  6

Recall the detail about the storage mode from the previous section. If you type x[NA_integer_], then you will find a different result.

like image 105
KenHBS Avatar answered Oct 12 '22 23:10

KenHBS