Extract given rows and columns from a given dataframe in R - GeeksforGeeks

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.
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
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
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Replace specific values in column in R DataFrame ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
How to change Row Names of DataFrame in R ?
Change Color of Bars in Barchart using ggplot2 in R
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
Remove rows with NA in one column of R DataFrame