Ask explicitly for efficient code from ChatGPT

Ask explicitly for efficient code from ChatGPT


Often times the code generated by ChatGPT is not the most efficient. Here I want to remove the second word in a sentence. The initial code works but involves splitting the string into words and then combine them, which is slow.


Me: Write R code to remove the second word in a string.

string <- "Hello World and Universe
words <- strsplit(string, "\\s")[[1]]
words <- words[-2]
new_string <- paste(words, collapse = " ")"        

I then asked it to avoid splitting. I got a slightly faster but ugly-looking code.

Me: Can you do that using regular expression not strsplit?

first_space <- which(substr(string, 1, 1:length(string)) == " ")[1
second_space <- which(substr(string, 1, 1:length(string)) == " ")[2]
new_string <- paste(substr(string, 1, first_space), substr(string, second_space+1, nchar(string)), sep="")]        

I tried again to ask for efficient code explicitly, ChatGPT pointed me to a function in the stringr package that uses regular expression. It is just one line and should be much faster. But when I tried it. It give me the wrong results -- it removes the first word!

Me: Great. Are there more efficient methods with less computing?

library(stringr)
new_string <- str_replace(string, "^\\S+\\s\\S+\\s", ""))
        


I have to Google around to find the correct regular expression. The final solution is using stringr package but with a correct regular expression.

library(stringr)
new_string <- str_replace(string, '^(\s*(?:\S+\s+){1})\S+', '\\1'))
        

You would think more efficient code should be prioritized, but apparently not. You have to be explicit. The lesson is if the code is important, ask repeatedly to get alternative solutions. And then test them. Don't trust ChatGPT.

#ChatGPT #rstats

John Giorgio

Sr Data Scientist at Great West Casualty

1 年

I appreciate you sharing your findings. Just today I used it to learn how to convert a column of lists to column of string using a lambda function in Python. Probably would not have thought to do that without seeing your posts

要查看或添加评论,请登录

Steven Ge的更多文章

  • ChatGPT goes to school

    ChatGPT goes to school

    Why is ChatGPT so useful and versatile? If it is a kid, how did its parent, OpenAI, raise and educate it? ChatGPT is a…

    3 条评论
  • Analytics Camp for High School Students (Free)

    Analytics Camp for High School Students (Free)

    Time: June 18-21, 2018 Location: Solberg Hall Computer Lab, SDSU Registration due date: 6/1/2018 Contact: Bill Alsaker…

    3 条评论
  • Running a complex web server for $5

    Running a complex web server for $5

    After running the iDEP web server (https://ge-lab.org/idep/ ) for over a year.

  • Interactive plots

    Interactive plots

    Like the photos posted on online dating websites, a conventional figure is a carefully chosen snapshot that can be…

  • iDEP 0.62 released

    iDEP 0.62 released

    iDEP 0.62 released! https://ge-lab.

  • Visualizing enriched gene ontology terms

    Visualizing enriched gene ontology terms

    Many Gene Ontology terms are related and show up redundantly in the results of enrichment analyses. To improve…

  • Analyzing gene expression data using iDEP

    Analyzing gene expression data using iDEP

    iDEP https://ge-lab.org/idep/ can translate RNA-Seq & other data into differentially expressed genes (DEGs) & pathways…

  • An integrated tool for analyzing gene expression

    An integrated tool for analyzing gene expression

    iDEP (integrated Differential Expression and Pathway analysis) is a web-based tool for analyzing RNA-seq data…

  • Gene Enrichment Analysis

    Gene Enrichment Analysis

    Enrichment Analysis that works for most species, most ID types We recently developed a small Shiny app: ShinyGO…

  • "Junk" DNA essential for embryo development

    "Junk" DNA essential for embryo development

    Just uploaded a manuscript over the weekend to BioRxiv.org.

社区洞察

其他会员也浏览了