<- list(
asc_list Facility = 1:10,
State = 11:20,
National = 21:30
)
Introduction
In R, lists are a fundamental data structure that allows us to store multiple objects of different data types under a single name. Often times, we want to extract certain elements of a list based on their names, and this can be accomplished through the use of the subset function. In this blog post, we will take a look at how to use the grep function to subset named lists in R.
First, we will create a list object as follows:
We now have a list with three elements, each with a different name. Next, we want to make sure that our list does not contain any 0 length items. This can be achieved by using the lapply function and the length function:
<- asc_list[lapply(asc_list, length) > 0] asc_list
The lapply
function applies the length function to each element of the list, and returns a logical vector indicating whether each element is of length greater than 0. By using the square bracket operator, we can extract only those elements for which the logical value is TRUE.
Next, we create a character vector of possible items that we want to match on:
<- c("state","faci") patterns
We can now pass this vector of patterns to the grep function, along with the names of our list and the ignore.case argument set to TRUE. The grep function returns the indices of the elements in our list that match the given pattern:
grep(
asc_list[paste(patterns, collapse = "|"),
names(asc_list),
ignore.case = TRUE
)]
$Facility
[1] 1 2 3 4 5 6 7 8 9 10
$State
[1] 11 12 13 14 15 16 17 18 19 20
The result of this code is a new list that contains only the elements of our original list whose names match either “state” or “faci”. The paste function is used to join the patterns in the vector into a single string, with the | character separating each pattern. This allows us to search for multiple patterns at once.
In conclusion, the grep function is a powerful tool for sub-setting named lists in R, especially when we have multiple patterns that we want to match on. By combining the grep function with other R functions such as lapply
and length, we can extract specific elements from our lists with ease.