Extract given rows and columns from a given dataframe in R
Last Updated :
26 Mar, 2021
Extraction of given rows and columns has always been one of the most important tasks which are especially required while working on data cleaning activities. In this article, we will be discussing all the sets of commands which are used to extract given rows and columns from a given dataframe in the R programming language.
Extraction of a single element from a dataframe
A single element can be extracted if the exact position of the item is known and is passed.
Syntax:
df[ row , column ]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[3,2]
Output:
[1] 130
Extraction of given Rows and Columns
Method 1: Extraction of all rows and columns
If no row and column number is specified, all rows and columns basically the complete data set is printed.
Syntax:
df[ ,]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[,]
Output:
c1 c2 c3
1 30 110 280
2 40 120 285
3 50 130 290
Method 2: Extraction of All rows of a column
If no row number is specified, but the column number is set to the required column value, all rows of a column can be extracted.
Syntax:
df[,n]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[,1]
Output:
[1] 30 40 50
Method 3: extraction of All columns of a row
If row number is specified, but no column number is given, all columns of a row can be extracted.
Syntax:
df[n,]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[1,]
Output:
c1 c2 c3
1 30 110 280
Method 4: extraction of more than one rows with all columns
c in c() means 'combine'. c function is a generic function which combines it arguments to form vectors. When row numbers to be extracted are specified and column number is not specified, selected rows with all columns are displayed.
Syntax:
df[ c(n,m), ]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[ c(1,3),]
Output:
c1 c2 c3
1 30 110 280
3 50 130 290
Method 5: extraction of more than one column with all rows
For this number of rows to be extracted should be given with no arguments for rows.
Syntax:
df[,c(n,m)]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[,c(2,3)]
Output:
c2 c3
1 110 280
2 120 285
3 130 290
Method 6: extraction of range of rows with all columns
If the range is specified for rows with no value for columns, this functionality can be achieved.
Syntax:
df[n:m,]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[1:2,]
Output:
c1 c2 c3
1 30 110 280
2 40 120 285
Method 7: extraction of a specific row from a range of columns
If the row is provided with the number of the row to be extracted and column with the range our functionality can be achieved.
Syntax:
df[r,n:m]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[2,1:3]
Output:
c1 c2 c3
2 40 120 285
Method 8: extraction of range of rows and columns
Supply rows and columns with ranges.
Syntax:
df[p:q, n:m]
Example:
R
df <- data.frame( c( 30, 40, 50), c( 110, 120, 130 ),c( 280, 285,290))
names(df) <- c("c1", "c2", "c3")
df[1:2, 1:3]
Output:
c1 c2 c3
1 30 110 280
2 40 120 285
Similar Reads
Extract last N rows of dataframe in R The last n rows of the data frame can be accessed by using the in-built tail() method in R. Supposedly, N is the total number of rows in the data frame, then n <=N last rows can be extracted from the structure. Syntax: tail(dataframe, n = ) Parameter : dataframe - The data frame to extract rows f
3 min read
Get Column Index in Data Frame by Variable Name in R R is an open-source programming language that is used as a statistical software and data analysis tool. In R Programming Language we can work on specific columns based on their names. In this article, we will learn different methods to extract the Get Column Index in Data Frame by Variable Name in R
5 min read
Subset Dataframe Rows Based On Factor Levels in R In this article, we will be discussing how to subset a given dataframe rows based on different factor levels with the help of some operators in the R programming language. Method 1: Subset dataframe Rows Based On One Factor Levels In this approach to subset dataframe rows based on one-factor levels,
2 min read
Extract unique columns from a matrix using R A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In R programming Language, matrices are two-dimensional, homogeneous data structures. These are some examples of matrice
5 min read
Unnesting a list of lists in a data frame column in R Working with data that has lists within columns is frequent when using R programming language. These lists may include various kinds of information, including other lists. But, working with these hierarchical lists can be difficult, especially if we wish to analyze or visualize the data. A list of l
6 min read
How to convert excel content into DataFrame in R ? R Programming Language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to convert excel content into DataFrame in R Programming. To read an excel file itself, read.xlsx() function from xlsx is used. Installation This module
2 min read