Data Selection - Basics
Once your dataset is loaded into R, you need to learn how to work with specific parts of it. This means selecting particular rows and columns that you want to focus on. Whether you're cleaning data or analyzing specific trends, being able to subset your data efficiently is essential.
Loading Your Dataset
Before working with any data, it needs to be loaded and viewed:
library(tidyverse) # load the tidyverse package
df <- read_csv("car_details.csv") # read the dataset
View(df) # open the dataset in a spreadsheet-style viewer
Selecting Rows
In R, you can select rows by their numeric position. Since indexing starts from 1, writing df[3, ]
will return the third row from the dataset.
df[3, ]
Selecting a Column by Position
Similarly to rows, you can select a column using its numeric position. By leaving the row index blank and specifying the column index, df[, 5]
returns the fifth column of the dataset.
df[, 5]
Selecting a Column by Name
You can also access a column directly by its name using the $
operator. This provides a quick and readable way to extract a single column. For example, df$km_driven
selects the column named km_driven.
view(df$km_driven)
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Data Selection - Basics
Swipe to show menu
Once your dataset is loaded into R, you need to learn how to work with specific parts of it. This means selecting particular rows and columns that you want to focus on. Whether you're cleaning data or analyzing specific trends, being able to subset your data efficiently is essential.
Loading Your Dataset
Before working with any data, it needs to be loaded and viewed:
library(tidyverse) # load the tidyverse package
df <- read_csv("car_details.csv") # read the dataset
View(df) # open the dataset in a spreadsheet-style viewer
Selecting Rows
In R, you can select rows by their numeric position. Since indexing starts from 1, writing df[3, ]
will return the third row from the dataset.
df[3, ]
Selecting a Column by Position
Similarly to rows, you can select a column using its numeric position. By leaving the row index blank and specifying the column index, df[, 5]
returns the fifth column of the dataset.
df[, 5]
Selecting a Column by Name
You can also access a column directly by its name using the $
operator. This provides a quick and readable way to extract a single column. For example, df$km_driven
selects the column named km_driven.
view(df$km_driven)
Thanks for your feedback!