Working with Chat GPT

Tyler George

Cornell College
DSC 223 - Spring 2024 Block 7

Warm up

The šŸ˜ in the room: Chat GPT

Using Chat GPT

How are you using Chat GPT for this class?

Example

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.

Chat GPT - Take 1 prompt

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.

Chat GPT - Take 1 response

Certainly! To filter the tv data frame to include only titles from the CSI-verse, you can use the filter function from the dplyr 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 the title 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 original tv data frame. Make sure to replace ā€˜tvā€™ with the actual name of your dataset.

Chat GPT - Take 1 code

What is wrong with the following code?

tv <- read_csv("data/tv.csv")
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)
# Display the first 10 rows of the 'csi' data frame
head(csi, 10)
Error in eval(expr, envir, enclos): object 'csi' not found

Chat GPT - Take 2 prompt

str_detect doesnā€™t have a case_insensitive argument, can you try again?

Chat GPT - Take 2 response

You are correct, and I appreciate your understanding. The str_detect function itself doesnā€™t have a case_insensitive argument. Instead, you can use the str_which function along with the case_insensitive argument in the filter 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 the filter function and includes the ignore_case = TRUE argument to perform a case-insensitive search. The resulting csi data frame will contain only titles from the CSI-verse along with all the columns from the original tv data frame. Make sure to replace ā€˜tvā€™ with the actual name of your dataset.

Chat GPT - Take 2 code

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)
# Display the first 10 rows of the 'csi' data frame
head(csi, 10)
Error in eval(expr, envir, enclos): object 'csi' not found

Chat GPT - Take 3 prompt

str_detect doesnā€™t have an ignore_case argument either, can you try again?

Chat GPT - Take 3 response

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 with stri_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 the stringi package within the filter function, and it includes the case_insensitive = TRUE argument for a case-insensitive search. The resulting csi data frame will contain only titles from the CSI-verse along with all the columns from the original tv data frame. Make sure to replace ā€˜tvā€™ with the actual name of your dataset.

Letā€™s go back to Take 1

and edit it a bit:

tv |>
  filter(str_detect(title, "CSI"))
# 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

Letā€™s go back to Take 1

csi <- tv |>
  filter(str_detect(title, "CSI"))

csi
# 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

Guidelines and best practices for using Chat GPT

  • Do not just copy-paste the prompt ā€“ for appropriate academic conduct, for your own learning, and for getting to better results faster
  • Engineer the prompt until the response starts to look like code youā€™re learning in the course
  • If the response is not correct, ask for a correction
  • If the response doesnā€™t follow the guidelines, ask for a correction
  • Do not just copy-paste code from Chat GPT responses, run it line-by-line and edit as needed
  • Watch out for clear mistakes in the response: do not keep loading packages that are already loaded, use the base pipe |>, use tidyverse-based code, etc.