Python and R can be seamlessly connected!

Image for post
Image for post

The method introduced here has something in common with the method we use when we learn languages ​​by ourselves. For example, if we want to learn Japanese, we can use the following three key exercises to help me translate Japanese words into English awkwardly and directly. Think and answer in English (Thinking in English) .

Associate new English words with Japanese words that I already know. Comparing English and Japanese words allows me to quickly understand the meaning of this new word.

Repeating this word many times, and using it in many different scenarios, engraved the word deeply in my mind.

Using contextual clues allows me to better understand the usage and reason of this word in synonyms.

Image for post
Image for post

When you learn coding for the first time, repetition and contextualization are essential . Through constant repetition, you begin to memorize vocabulary and grammar. Through project development, you can understand how and why different functions and technologies are used, and start to see how to use the code in different contexts. But there is not necessarily a simple way to connect new ways of thinking with the language you speak. This means that you not only have to remember a word, but have a new understanding of every programming concept. Even the first line of code you write, print (“Hello, world!”) requires you to understand how the print function works, how the editor returns print statements, and when to use quotes. When you learn the second programming language, you can translate the concepts in the language you know into the new language, so you can learn more effectively and faster.

The world of data science is divided between Python advocates and R enthusiasts. However, anyone who has learned one of these languages ​​should make full use of their advantages and go deep into the other language instead of claiming to be a party. There are infinite similarities between Python and R , and both languages ​​are available to you. You can solve the challenge in the best way, instead of limiting yourself to half of the tool library.

Below is a simple guide to connect R and Python to facilitate the conversion between the two . By establishing these connections, repeatedly interacting with new languages, and contextualizing projects, anyone who understands Python or R can quickly start programming in another language.


You can see that the functions and appearances of Python and R are very similar, but the syntax is slightly different.

Type of data


Guide package

Math package: mathematics is the same in all languages

Call functions

Conditional judgment

Lists and vectors: This is a bit difficult, but I found the association method mentioned above to be very useful.

  • In python, a list is a variable collection of ordered items of any data type. List index in Python starts from 0, not including 0
  • In R, a vector is a variable set of ordered items of the same type. The vector in index R starts at 1 and is inclusive


Data manipulation

Both python and R provide simple and streamlined data manipulation packages, making them indispensable tools for data scientists.

Both languages ​​are equipped with packages capable of loading, cleaning and processing data .

Python uses pandas and R uses tidyverse, and their functions are basically the same.

Both languages ​​allow multiple operations to be connected by pipes. Use “.” in python to combine different operations with “%>%” in R.

Read, write and view data

Rename and add columns

Select and filter columns

Filter rows



# R df %>% group_by(col1) %>% summarize(mean = mean(agg_col, na.rm=TRUE)) %>% ungroup #if resetting index

Use filter aggregation

df.groupby( ‘col1’ ).filter( lambda x: x.col2.mean> 10 )

# R df %>% group_by(col1) %>% filter(mean(col2)> 10 )

Merge dataframe

# R merge(df1, df2, by.df1= “df1_col” , by.df2= “df2_col” )

The above example is the starting point for creating psychological similarity between Python and R. Although most data scientists tend to use one language or the other, the tools that best suit your needs can be used in both languages.

Our ultimate goal is not to master another language proficiently and develop with it, but to understand the code written in another language and apply its ideas to our own projects.

Written by

Digital Nomad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store