DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Why Is SQL Knowledge Vital for Data Scientists? A Sneak Peek
  • Setup ActiveMQ Artemis on Windows
  • Integrating Google BigQuery With Amazon SageMaker
  • AI, ML, and Data Science: Shaping the Future of Automation

Trending

  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  1. DZone
  2. Data Engineering
  3. Databases
  4. Learn R: How to Extract Rows and Columns From Data Frame

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.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Dec. 08, 14 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
1.1M Views

Join the DZone community and get the full member experience.

Join For Free

This article represents a command set in the R programming language, which can 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.

The 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
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 a Data Frame

The following represents a command which can be used to extract a column as a data frame. If you use a 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 a 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
df[2,3]
Data science R (programming language) Extract Column (database) Frame (networking) Row (database) Command (computing)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Is SQL Knowledge Vital for Data Scientists? A Sneak Peek
  • Setup ActiveMQ Artemis on Windows
  • Integrating Google BigQuery With Amazon SageMaker
  • AI, ML, and Data Science: Shaping the Future of Automation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!