ChatGPT can Create Datasets, Program in R… and when it makes an Error it can Fix that too!

[This article was first published on R-Bloggers – Learning Machines, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


ChatGPT from OpenAI leaves me speechless over and over again. I have been in the AI industry for many decades now and it has been a long time since I last had this feeling of utter fascination mixed with disbelief mixed with anxiety.

This is only a quick post in the context of R programming which I wanted to share with you, so read on!

So, I asked ChatGPT to create a sample dataset and write some R code to analyze it:

As you can see the code comes fully documented already!

The table looks nice but I wanted to have it in csv format:

When I ran the code, I encountered an error, so I asked ChatGPT to fix it:

After that the code ran without any problems:

# Load the data into R
crm_data <- read.csv("data/crm_data.csv", header = TRUE)

# View the first few rows of the data
head(crm_data)
##   Customer_ID  Name       Address          City State Zip_Code Phone_Number
## 1           1  John  123 Main St.      New York    NY    10001 555-555-5555
## 2           2  Jane 456 Park Ave.   Los Angeles    CA    90001 555-555-5556
## 3           3   Sam   789 Elm St.       Chicago    IL    60601 555-555-5557
## 4           4 Sarah  987 Main St. San Francisco    CA    94102 555-555-5558
## 5           5  Mike 321 Park Ave.      New York    NY    10001 555-555-5559
##             Email Purchased_Product Purchase_Date
## 1  [email protected]       Apple Watch    01/01/2022
## 2  [email protected]         iPhone 12    01/02/2022
## 3   [email protected]       AirPods Pro    01/03/2022
## 4 [email protected]       MacBook Pro    01/04/2022
## 5  [email protected]          iPad Pro    01/05/2022

# View the number of rows and columns in the data
dim(crm_data)
## [1]  5 10

# View the summary statistics of the data
summary(crm_data)
##   Customer_ID     Name             Address              City          
##  Min.   :1    Length:5           Length:5           Length:5          
##  1st Qu.:2    Class :character   Class :character   Class :character  
##  Median :3    Mode  :character   Mode  :character   Mode  :character  
##  Mean   :3                                                            
##  3rd Qu.:4                                                            
##  Max.   :5                                                            
##     State              Zip_Code     Phone_Number          Email          
##  Length:5           Min.   :10001   Length:5           Length:5          
##  Class :character   1st Qu.:10001   Class :character   Class :character  
##  Mode  :character   Median :60601   Mode  :character   Mode  :character  
##                     Mean   :52941                                        
##                     3rd Qu.:90001                                        
##                     Max.   :94102                                        
##  Purchased_Product  Purchase_Date     
##  Length:5           Length:5          
##  Class :character   Class :character  
##  Mode  :character   Mode  :character  
##                                       
##                                       
## 

# Create a bar plot showing the number of purchases by state
barplot(table(crm_data$State))

# Create a pie chart showing the percentage of purchases by product
pie(table(crm_data$Purchased_Product), main = "Percentage of Purchases by Product")

# Convert the Purchase_Date column to a date format
crm_data$Purchase_Date <- as.Date(crm_data$Purchase_Date, format = "%m/%d/%Y")

# Create a scatterplot showing the relationship between purchase date and zip code
plot(crm_data$Purchase_Date, crm_data$Zip_Code, xlab = "Purchase Date", ylab = "Zip Code")

Ok, that’s it for today… this is just unbelievable, isn’t it? Please share your thoughts and experience with this tool in the comments below!

To leave a comment for the author, please follow the link and comment on their blog: R-Bloggers – Learning Machines.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)