Use Gemini for Code Conversion
Here is a useful task for which LLMs like Google Gemini can be of assistance. Let’s say you inherited several R scripts in your workplace and you want to convert them to Python. With a little bit of prompting, you can save yourself some time and get your R code turned into Python code.
Let’s start simple. I have the following basic R code to declare and print a vector. I want to turn it into Python code.
s<-c('apple','red',5,TRUE)(s)
I can prompt Google Gemini to convert it to Python as follows (note that I am pasting the code into a triple-ticks (```) block):
Prompt: Can you convert this R code into Python code?
```s <- c('apple','red',5,TRUE) print(s)```
Gemini: Sure, here is the Python code that converts the R code
s <- c(‘apple’,‘red’,5,TRUE):s=['apple','red',5,True](s)The Python code is equivalent to the R code in terms of functionality. It creates a list called
sthat contains the elements'apple','red',5, andTrue. Then, it prints the contents of the lists.
This is a great start, but let’s say I wanted to use pandas rather than a plain Python list. I can continue to prompt and ask for this change:
Prompt: Awesome, can you use pandas to do that task?
Gemini: Sure, here is the Python code that uses pandas to do the same task:
importpandasaspds=pd.Series(['apple','red',5,True])(s)The Python code first imports the pandas library. Then, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access