![]() A vector of all the negative elements of yĪ vector of all the positive elements of y. ![]() A vector of all the positive elements of y.Therefore, if we want to create a vector called y that contains all of the non-NA values from x, we can use y 0 will give us a vector of logical values the same length as y, with TRUEs corresponding to values of y that are greater than zero and FALSEs corresponding to values of y that are less than or equal to zero. Recall that ! gives us the negation of a logical expression, so !is.na(x) can be read as ‘is not NA’. x # NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA Recall that is.na(x) yields a vector of logical values the same length as x, with TRUEs corresponding to NA values in x and FALSEs corresponding to non-NA values in x. One common scenario when working with real-world data is that we want to extract all elements of a vector that are not NA (i.e., missing data). Let’s start by indexing with logical vectors. ![]() Index vectors come in four different flavors – logical vectors, vectors of positive integers, vectors of negative integers, and vectors of character strings – each of which we’ll cover in this lesson. The way you tell R that you want to select some particular elements (i.e., a ‘subset’) from a vector is by placing an ‘index vector’ in square brackets immediately following the name of the vector.įor a simple example, try x to view the first ten elements of x. I’ve created for you a vector called x that contains a random ordering of 20 numbers (from a standard normal distribution) and 20 NAs. By the end of this lesson, you’ll know how to handle each of these scenarios. In other words, we want to select some of the numbers in a vector based either on their position in the vector or the value that each number has.įor example, we may only be interested in the first 20 elements of a vector, or only the elements that are not NA, or only those that are positive or correspond to a specific variable of interest. In this lesson, we’ll see how to extract elements from a vector based on some conditions that we specify.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |