library(tidyverse) #install tinyverse library
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
  1. Load pre-installed ‘iris’ dataset:
iris #load iris dataset
  1. Choose and display columns ‘Sepal.Length’, ‘Species’, and ‘Petal.Width’:
iris |> #load iris dataset
  select(Sepal.Length, Species, Petal.Width) #then select specified columns
  1. Subset the data to include only rows where ‘Sepal.Length’ is greater than 5:
iris |> #load iris dataset
  filter(Sepal.Length > 5) #return rows where sepal length is greater than 5
  1. Sort the dataset based on ‘Petal.Length’ column in ascending order:
iris |> #load iris dataset
  arrange(Petal.Length) #then sort petal length column in ascending order
  1. Create a new variable ‘sepal_ratio” by dividing ’Sepal.Width’ by ‘Sepal.Length’ and display the updated dataset:
iris |> #load iris dataset, then
  mutate( #create new column
    sepal_ratio = Sepal.Width/Sepal.Length #new column named 'sepal_ratio' defined as sepal width / sepal length
  )
  1. Calculate the average ‘Petal.Length’ for the entire dataset:
iris |> #load iris dataset, then
  summarize( #summarize a column
    Average.Petal.Length = mean(Petal.Length) #return average petal length
  )
  1. Group the data by the ‘Species’ variable, then calculate the average ’Petal.Length” within each species group:
iris |> #load iris dataset, then
  group_by(Species) |> #group by Species
  summarize( #summarize a column
    Average.Petal.Length = mean (Petal.Length) #return average petal length
  )
  1. In one code bloack using pipes, subset to include only rows where ‘Petal.Length’ is greater than 3, then create new variable ‘sepal_difference’ by subtracting ‘Sepal.Width’ by ‘Sepal.Length’. Then group data by the ‘Species’ variable and calculate the average ‘sepal_difference’ within each species group:
iris |> #load iris dataset, then
  filter(Petal.Length > 3) |> #select rows where petal length is greater than 3
  mutate( #create a new variable 
    sepal_difference = Sepal.Width - Sepal.Length #new variable called sepal_difference defined as differenced between sepal width and sepal length
  )|>
  group_by(Species) |> #group by species variable
  summarize(#summarize a column
    avg_sepal_difference = mean(sepal_difference) #return average sepal_difference
  )