Extract column from list in R - GeeksforGeeks

26 Mar, 2021
In this article, we are going to create a list of elements and access those columns in R. We are first creating a list with matrix and vectors and access those columns using R.
Approach
Create a list
Syntax:
list_name=list(var1, var2, varn..)
Assign names to list elements as columns names. We can give names using names() function
Syntax:
names(list_name)=c(var1, var2, varn)
Access those columns and elements.
Method 1: Using indices
In this method, we simply have to pass the index of the column with the name of the list to extract that specific column.
Example:
R
# Create a list that can hold a vector, and a matrixlist1 <- list(c("sravan", "sudheer", "vani", "radha"),               matrix(c(98, 87, 78, 87)))  # assign names to the elements in the list.names(list1) <- c("names", "percentage")  # access the column 1print(list1[1])  # access the column 2print(list1[2])
Output:
Method2: Using $ operator.
In this method, the name of the column to be retrieved has to be passed with its name and name of the list separated by the dollar sign($).
Syntax:
list_name$column_name
Example:
R
# Create a list that can hold a vector, and a matrixlist1 <- list(c("sravan", "sudheer", "vani", "radha"),               matrix(c(98, 87, 78, 87)))  # assign names to the elements in the list.names(list1) <- c("names", "percentage")  # access the column 1print(list1$names)  # access the column 2print(list1$percentage)
Output:
A list can contain a matrix, vector, and a list as arguments to a list but to access them the method remains the same and it has been discussed in the code below.
Example: 
R
# Create a list that can hold a vector, and a matrix and a listlist1 <- list(c("sravan", "sudheer", "vani", "radha"),               matrix(c(98, 87, 78, 87)),              list('vignan', 'vit', 'vvit', 'rvrjc'))  # assign names to the elements in the list.names(list1) <- c("names", "percentage", "college")  print("Method 1")  # access the column 1print(list1[1])  # access the column 2print(list1[2])  # access the column 3print(list1[3])  print("Method 2")  # access the column 1print(list1$names)  # access the column 2print(list1$percentage)  # access the column 3print(list1$college)
Output:
It is possible to access nested elements using [[]] operator.
Syntax:
list_name[[value]][[value]]...
Example:
R
# Create a list that can hold a vector, and a # matrix and a listlist1 <- list(c("sravan", "sudheer", "vani", "radha"),               matrix(c(98, 87, 78, 87)),              list('vignan', 'vit', 'vvit', 'rvrjc'))  # access 2nd column first elementprint(list1[[2]][[1]])  # access 2nd column print(list1[[2]])  # access 3rd column third elementprint(list1[[3]][[3]])
Output:
Picked
R-List
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R