Over a million developers have joined DZone.

Learn R: How to Extract Rows and Columns From Data Frame

DZone's Guide to

Learn R: How to Extract Rows and Columns From Data Frame

This article represents command set in R programming language, which could be used to extract rows and columns from a given data frame.

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

This article represents a command set in the R programming language, which could be used to extract rows and columns from a given data frame. When working on data analytics or data science projects, these commands come in handy in data cleaning activities.  This article is meant for beginners/rookies getting started with R who want examples of extracting information from a data frame. Please feel free to comment/suggest if I missed mentioning one or more important points.

Following are the key points described later in this article:

  • Commands to extract rows and columns.
  • Command to extract a column as a data frame.
  • Command to extract an element.

Suppose you have a data frame, df, which is represented as follows. Below we've created a data frame consisting of three vectors that include information such as height, weight, and age.

df <- data.frame( c( 183, 85, 40), c( 175, 76, 35), c( 178, 79, 38 ))
names(df) <- c("Height", "Weight", "Age")

Commands to Extract Rows and Columns

The following represents different commands which could be used to extract one or more rows with one or more columns. Note that the output is extracted as a data frame. This could be checked using the class command.

# All Rows and All Columns

# First row and all columns

# First two rows and all columns

# First and third row and all columns
df[ c(1,3), ]

# First Row and 2nd and third column
df[1, 2:3]

# First, Second Row and Second and Third COlumn
df[1:2, 2:3]

# Just First Column with All rows
df[, 1]

# First and Third Column with All rows

Command to Extract a Column as Data Frame

The following represents a command which can be used to extract a column as a data frame. If you use command such as df[,1], the output will be a numeric vector (in this case). To get the output as a data frame, you would need to use something like below.

# First Column as data frame
as.data.frame( df[,1], drop=false)

Command to Extract an Element

The following represents command which could be used to extract an element in a particular row and column. It is as simple as writing a row and a column number, such as the following:

# Element at 2nd row, third column

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

big data ,analytics ,rlang ,r ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}