Get startedGet started for free

Selecting elements from objects

1. Selecting elements from objects

Now that you have created several types of objects, in this video you will learn how to select elements and subsets of from these objects.

2. Select element from vector

Individual elements in vectors can be selected using single brackets with the index position provided inside. For example, to select the third element from X type 3 between the square brackets, to extract the value 2.

3. Select element from matrix

Individual elements can also be selected from a matrix using single brackets but with 2 numbers for the row and column index positions. The row index is listed first and column index listed second separated by a comma. Let's select the element at the first row and second column, which is the value 4.1.

4. Select column from matrix

Besides individual elements, you can select a whole row or whole column from a matrix. To select an entire column, like column 2 shown here, inside the single brackets, the row index is left blank to select all rows for column 2 listed second.

5. Select row from matrix

Similarly, to select the entire third row from the matrix m, the row index 3 is listed first and the column index is left blank.

6. Select elements from data frame

Selecting individual elements, rows or columns from a data frame can also be done using the same single bracket notation with the row index listed first and the column index is listed second. For example, the value FALSE is displayed from row 2 column 3. The second column for age descriptions can be displayed. Or you can select the third row from the data frame.

7. Select variable from data frame by name

When working with data frames, the name of a variable column can also be used to select that column. The single bracket notation can be used again leaving the row index blank, but instead of column number you put the name of the variable between double quotation marks. Here the test variable is extracted from data frame d.

8. Get one column from data frame

In addition to the bracket notation with data frames, the dplyr package provides the pull, select, and slice functions to extract one or more columns and rows. For example, you can pull out the test variable column from the data frame d. With the pull dplyr function, the variable name test does not need double quotation marks.

9. Get two columns from data frame

Besides using the pull function to extract a single variable, the select function can be used to extract two or more variables. Inside the parentheses for select you can list the columns using index numbers or the names of the variables. The colon notation selects all columns between the first and last variable listed. Here you are selecting the columns from age to test. You can also list the specific columns you want and list them in the order you want them displayed. Here, the code extracts the test variable first followed by the score variable.

10. Get two rows from data frame

The dplyr slice function can be used to get one or more rows from a dataset. Similar to select, you can use the colon notation to select all rows beginning at row 2 through row 3.

11. Get specific rows from data frame

A single row can be extracted using the slice function - for example row 2. You can also use the c combine function inside the parentheses of the slice function to select specific rows in the order you want them. For example, extract and display row 3 first and then row 1 from data frame d.

12. Let's practice selecting data elements

So, let's practice selecting data elements and customizing your output.