Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Accessing Columns in Data Frames | Data Frames
R Introduction

bookAccessing Columns in Data Frames

Data frames always have named columns, and you can use these names to extract specific columns. There are two main approaches.

Using Square Brackets

You can specify the column name inside square brackets.

Example

1234567
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Extracting the name with square brackets test[, "name"]
copy

Using the Dollar Sign

Data frames also support a shorthand operator, $, which allows you to access a column by name without quotes.

Example

1234567
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Extracting the name with dollar sign test$name
copy
Task

Swipe to start coding

You have the mtcars dataset.

Your tasks are:

  1. Extract the cyl column values using square brackets.
  2. Extract the disp column values using the dollar $ sign.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 2.27

bookAccessing Columns in Data Frames

Swipe to show menu

Data frames always have named columns, and you can use these names to extract specific columns. There are two main approaches.

Using Square Brackets

You can specify the column name inside square brackets.

Example

1234567
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Extracting the name with square brackets test[, "name"]
copy

Using the Dollar Sign

Data frames also support a shorthand operator, $, which allows you to access a column by name without quotes.

Example

1234567
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Extracting the name with dollar sign test$name
copy
Task

Swipe to start coding

You have the mtcars dataset.

Your tasks are:

  1. Extract the cyl column values using square brackets.
  2. Extract the disp column values using the dollar $ sign.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
single

single

some-alt