How to use sapply() function In R Language

Here we can access the elements from the sapply() using index numbers

Syntax:

sapply(list,"[",c(index))

Example: Subset List

R




# create list
data=list(a=c(1:3),b=c(5:8),"geeks")
  
# get elements of index 1 and 2
sapply(data,"[",c(1,2))


Output:

How to Subset Lists in R?

In this article, we will discuss how to subset lists in R programming language.

Similar Reads

Method 1: Extract One List Item

We can extract using an index number. Indexing starts with 1...

Method 2: Extract Multiple List Items

...

Method 3: Extract Multiple List Items with slice operator

We can also select the items at a time using index numbers with c() function...

Method 4: Using sapply() function

...

Method 5: Using $ operator

Here we are going to use index numbers in slice operator....