# How to Extract Values from a Matrix in R - dummies.

An R tutorial on retrieving a collection of row vectors in a data frame. Discussion on the usage of numeric, name and logical indexing.

Matrix indexing (x(i) with a logical or a 2-column integer matrix i) using ( is not recommended. For extraction, x is first coerced to a matrix. Thus, something like as.matrix(tmp) happens converting every column to character.

Logical Index Vector A new vector can be sliced from a given vector with a logical index vector, which has the same length as the original vector. Its members are TRUE if the corresponding members in the original vector are to be included in the slice, and FALSE if otherwise. For example, consider the following vector s of length 5.

Logical indexing is a compact and expressive notation that's very useful for many image processing operations. Let's talk about the basic rules of logical indexing, and then we'll reexamine the expression B (isnan (B)). If C and D are matrices, then C (D) is a logical indexing expression if C and D are the same size, and D is a logical matrix.

Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations.

In a data frame the columns contain different types of data, but in a matrix all the elements are the same type of data. A matrix in R is like a mathematical matrix, containing all the same type of thing (usually numbers). R often but not always lets these be used interchangably.

Latent semantic indexing (LSI) is an indexing and retrieval method that uses a mathematical technique called singular value decomposition (SVD) to identify patterns in the relationships between the terms and concepts contained in an unstructured collection of text. LSI is based on the principle that words that are used in the same contexts tend to have similar meanings.

When you index a vector with a logical vector, R will return values of the vector for which the indexing vector is TRUE. If that was confusing, think about it this way: a logical vector, combined with the brackets ( ), acts as a filter for the vector it is indexing.

Logical indexing on the left assigns these values to the elements that were singled out in column major order. So it scans the first column up here, looking for a negative number. It finds one here on the third row, and it assigns the first value from the right, which was 101.