Categories: Big Data

Learn R – How to Extract Rows & 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. When working on data analytics or data science projects, these commands come very handy in data cleaning activities.  This article is meant for beginners/rookies getting started with R and wanting to know or see examples of extracting information from a data frame. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the key points described later in this article:
  • Commands to extract rows and columns
  • Command to extract a column as data frame
  • Command to extract an element

 

Suppose you have a data frame, df, which is represented such as following. Below is created a data frame consisting of three vectors that consist of information such as height, weight, age.
df <- data.frame( c( 183, 85, 40), c( 175, 76, 35), c( 178, 79, 38 ))
names(df) <- c("Hieght", "Weight", "Age")

 

Commands to Extract Rows and Columns

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

# All Rows and All Columns
df[,]

# First row and all columns
df[1,]

# First two rows and all columns
df[1:2,]

# 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
df[,c(1,3)]

 

Command to Extract a Column as Data Frame

Following represents command which could 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 an 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

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

# Element at 2nd row, third column
df[2,3]

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Recent Posts

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 28th April, 2024 As a data scientist, understanding the nuances of various cost…

1 day ago

Cross Entropy Loss Explained with Python Examples

Last updated: 28th April, 2024 In this post, you will learn the concepts related to…

1 day ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

3 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

4 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

1 week ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

1 week ago