Cornell College
DSC 223 - Spring 2024 Block 7
How are you using Chat GPT for this class?
Each CSI franchise includes a number of series, each with the character string "CSI"
(in capital letters) in their title. Using this information, create a new data frame called csi
that only has titles from the CSI-verse and all of the columns from tv
for these titles. Then, display the first 10 rows of csi
. Note: The titles and seasons included in the dataset may not be all CSI titles and titles that have ever aired.
Each CSI franchise includes a number of series, each with the character string
"CSI"
(in capital letters) in their title. Using this information, create a new data frame calledcsi
that only has titles from the CSI-verse and all of the columns fromtv
for these titles. Then, display the first 10 rows ofcsi
. Note: The titles and seasons included in the dataset may not be all CSI titles and titles that have ever aired.
Certainly! To filter the
tv
data frame to include only titles from the CSI-verse, you can use thefilter
function from thedplyr
package. Hereās the code:library(dplyr) # Assuming your dataset is named 'tv' csi <- tv %>% filter(str_detect(title, "CSI", case_insensitive = TRUE)) # Display the first 10 rows of the 'csi' data frame head(csi, 10)
This code uses the
filter
function to select rows where thetitle
column contains the string āCSIā (case-insensitive). The resulting data frame,csi
, will include only titles from the CSI-verse along with all the columns from the originaltv
data frame. Make sure to replace ātvā with the actual name of your dataset.
What is wrong with the following code?
library(dplyr)
# Assuming your dataset is named 'tv'
csi <- tv %>%
filter(str_detect(title, "CSI", case_insensitive = TRUE))
Error in `filter()`:
ā¹ In argument: `str_detect(title, "CSI", case_insensitive = TRUE)`.
Caused by error in `str_detect()`:
! unused argument (case_insensitive = TRUE)
Error in eval(expr, envir, enclos): object 'csi' not found
str_detect doesnāt have a case_insensitive argument, can you try again?
You are correct, and I appreciate your understanding. The
str_detect
function itself doesnāt have acase_insensitive
argument. Instead, you can use thestr_which
function along with thecase_insensitive
argument in thefilter
function. Hereās the corrected code:library(dplyr) # Assuming your dataset is named 'tv' csi <- tv %>% filter(str_detect(title, "CSI", ignore_case = TRUE)) # Display the first 10 rows of the 'csi' data frame head(csi, 10)
This code uses
str_detect
within thefilter
function and includes theignore_case = TRUE
argument to perform a case-insensitive search. The resultingcsi
data frame will contain only titles from the CSI-verse along with all the columns from the originaltv
data frame. Make sure to replace ātvā with the actual name of your dataset.
library(dplyr)
# Assuming your dataset is named 'tv'
csi <- tv %>%
filter(str_detect(title, "CSI", ignore_case = TRUE))
Error in `filter()`:
ā¹ In argument: `str_detect(title, "CSI", ignore_case = TRUE)`.
Caused by error in `str_detect()`:
! unused argument (ignore_case = TRUE)
Error in eval(expr, envir, enclos): object 'csi' not found
str_detect doesnāt have an ignore_case argument either, can you try again?
I apologize for the oversight. It seems there was a mistake in my responses. To perform a case-insensitive search, you can use the
stringi
package along withstri_detect_regex
. Hereās the corrected code:library(dplyr) library(stringi) # Assuming your dataset is named 'tv' csi <- tv %>% filter(stri_detect_regex(title, "CSI", case_insensitive = TRUE)) # Display the first 10 rows of the 'csi' data frame head(csi, 10)
This code uses
stri_detect_regex
from thestringi
package within thefilter
function, and it includes thecase_insensitive = TRUE
argument for a case-insensitive search. The resultingcsi
data frame will contain only titles from the CSI-verse along with all the columns from the originaltv
data frame. Make sure to replace ātvā with the actual name of your dataset.
and edit it a bit:
# A tibble: 36 Ć 9
season title year month day av_rating genre_1 genre_2 genre_3
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 1 CSI: Crime Scene Investigatā¦ 2001 1 20 8.32 Crime Drama Mystery
2 2 CSI: Crime Scene Investigatā¦ 2002 1 10 8.26 Crime Drama Mystery
3 3 CSI: Crime Scene Investigatā¦ 2003 1 15 8.30 Crime Drama Mystery
4 4 CSI: Crime Scene Investigatā¦ 2004 1 18 8.33 Crime Drama Mystery
5 5 CSI: Crime Scene Investigatā¦ 2005 1 24 8.38 Crime Drama Mystery
6 6 CSI: Crime Scene Investigatā¦ 2006 1 16 8.21 Crime Drama Mystery
7 7 CSI: Crime Scene Investigatā¦ 2007 1 14 8.43 Crime Drama Mystery
8 8 CSI: Crime Scene Investigatā¦ 2008 1 7 8.07 Crime Drama Mystery
9 9 CSI: Crime Scene Investigatā¦ 2009 1 27 7.80 Crime Drama Mystery
10 10 CSI: Crime Scene Investigatā¦ 2010 1 23 7.69 Crime Drama Mystery
# ā¹ 26 more rows
# A tibble: 36 Ć 9
season title year month day av_rating genre_1 genre_2 genre_3
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 1 CSI: Crime Scene Investigatā¦ 2001 1 20 8.32 Crime Drama Mystery
2 2 CSI: Crime Scene Investigatā¦ 2002 1 10 8.26 Crime Drama Mystery
3 3 CSI: Crime Scene Investigatā¦ 2003 1 15 8.30 Crime Drama Mystery
4 4 CSI: Crime Scene Investigatā¦ 2004 1 18 8.33 Crime Drama Mystery
5 5 CSI: Crime Scene Investigatā¦ 2005 1 24 8.38 Crime Drama Mystery
6 6 CSI: Crime Scene Investigatā¦ 2006 1 16 8.21 Crime Drama Mystery
7 7 CSI: Crime Scene Investigatā¦ 2007 1 14 8.43 Crime Drama Mystery
8 8 CSI: Crime Scene Investigatā¦ 2008 1 7 8.07 Crime Drama Mystery
9 9 CSI: Crime Scene Investigatā¦ 2009 1 27 7.80 Crime Drama Mystery
10 10 CSI: Crime Scene Investigatā¦ 2010 1 23 7.69 Crime Drama Mystery
# ā¹ 26 more rows
|>
, use tidyverse-based code, etc.