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 calledcsithat only has titles from the CSI-verse and all of the columns fromtvfor 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
tvdata frame to include only titles from the CSI-verse, you can use thefilterfunction from thedplyrpackage. 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
filterfunction to select rows where thetitlecolumn 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 originaltvdata 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_detectfunction itself doesnāt have acase_insensitiveargument. Instead, you can use thestr_whichfunction along with thecase_insensitiveargument in thefilterfunction. 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_detectwithin thefilterfunction and includes theignore_case = TRUEargument to perform a case-insensitive search. The resultingcsidata frame will contain only titles from the CSI-verse along with all the columns from the originaltvdata 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
stringipackage 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_regexfrom thestringipackage within thefilterfunction, and it includes thecase_insensitive = TRUEargument for a case-insensitive search. The resultingcsidata frame will contain only titles from the CSI-verse along with all the columns from the originaltvdata 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.