Get startedGet started for free

Working with lists

1. Working with lists

In this video you will continue learning how to save output from R functions and tests that are list objects. You will also learn how to extract specific elements from these list objects.

2. Create list by combining other objects

First, though, let's learn about list objects. Lists are even more flexible than data frames since lists combine objects of different sizes and class types. Let's create a list object. First, we'll create a logical scalar logic_scalar with one value TRUE.

3. Create list by combining other objects

Next, we'll create a character vector age with three words; child, young, and old.

4. Create list by combining other objects

And we'll also create a numeric matrix m from two numeric vectors. The matrix has 3 rows and 2 columns.

5. Create list by combining objects of different types

Now, you will use the list function to combine the three objects logic_scalar, age_charvec, and num_matrix into a list named list_logicagemtx.

6. Named list elements

Let's also name the elements of our list using the names function. Run the names function again to view these names. These names will be used later to select elements. The str structure function output confirms the class of the object list_logicagemtx is a list with 3 elements named logicalValue, ageDescription,and numericMatrix. This output also shows the class type of each element in the list.

7. Select elements from list by name

The dollar sign operator can be used to select elements out of a list object by the name of the element. Here the age vector is selected by its name ageDescription. You can confirm that the extracted age vector is of character vector class using the class and is.vector functions.

8. Hmisc output

Now that you learned about list objects, let's work with the the output from the Hmisc::describe function which is a list. The names of the list elements in Hmisc output are sex and bmi. They are both list objects.

9. Hmisc output

You can use the dollar sign to selectively display the output for the sex variable. It is worth noting that the sex element of the output is itself also a list. Lists can be nested inside of lists, which is why the list class is so versatile and used by many developers to produce output.

10. Hmisc output

You can also view the names of the elements inside the davisHmisc$sex list. You can use the dollar sign selector again to further select the counts element from the sex summary statistics.

11. Correlation tests output

In an earlier video you used the psych::corr.test function to get Pearson's r correlations plus the statistical tests and p-values. However, the standard output did not provide the p-values with many digits. As you can see from the names function, the corr.test output is a list of 11 elements.

12. Correlation tests output

By using the dollar sign to select the p-values, they are displayed with much greater precision. The CI element displays the correlations r, the lower and upper bounds of their confidence intervals and their p-values.

13. T-tests output

You can also save the output from the t-tests you did in an earlier video. The output object is htest class which is also a list. The htest class for a t-test contains many elements including the test statistic, degrees of freedom parameter, p.value, confidence intervals and more.

14. Chi-square tests output

You can also save the output from the chisq.test function. This output is also htest class, with elements similar to those from t.test.

15. Output of htest list class

From the t.test output list, you can display the t-test statistic, degrees of freedom parameter, and p-value. From the chisq.test output you can display the observed counts and expected values.

16. Let's work with list output objects to customize your abalone analysis results.

Let's work with list output objects to customize your abalone analysis results.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.