Memo for Rewriting Code in Python and R (Basic Edition)
This is a basic guide for converting memo between Python and R.
In recent years, Python and R have become extremely popular programming languages in the fields of data analysis and machine learning. Each language has its own strengths and differences in usability and syntax. However, to achieve the same goal, you can write similar code in both languages. Additionally, since R is specialized for programming in statistical analysis, there are abundant books available in that field. I believe that rewriting programs written in R into Python can also contribute to learning the language. This compilation was created with the purpose of serving as a reference table and does not provide detailed explanations of the languages.
Variable
| Python | R |
|---|---|
| >>> x=3 >>> y=2 >>> x+y 5 |
> x <- 3 > y <- 2 > x+y [1] 5 |
String
| Python | R |
|---|---|
| >>> x="Hello" >>> x 'Hello’ |
> x <- “Hello" > x [1] “Hello" |
Arithmetic operation
Addition
| Python | R |
|---|---|
| >>> 3+2 5 |
> 3+2 [1] 5 |
Subtraction
| Python | R |
|---|---|
| >>> 3-2 1 |
> 3-2 [1] 1 |
Multiplication
| Python | R |
|---|---|
| >>> 3*2 6 |
> 3*2 [1] 6 |
Division
| Python | R |
|---|---|
| >>> 3/2 1.5 |
> 3/2 [1] 1.5 |
Exponentiation
| Python | R |
|---|---|
| >>> 3**2 9 |
> 3**2 [1] 9 |
Modulo
| Python | R |
|---|---|
| >>> 5%3 2 |
> 5%%3 [1] 2 |
Factorial
| Python | R |
|---|---|
| >>> import math >>> math.factorial(5) 120 |
> factorial(5) [1] 120 |
※Handling exponential notation in R.
In R, numbers with a large number of digits are displayed in exponential notation by default. To disable this, you can use the options(scipen=**) function to set the desired value.
> 100000
[1] 1e+05
options(scipen=10)
> 100000
[1] 100000
Comparison operators
| Python | R |
|---|---|
| >>> 3 == 3 True |
> 3 == 3 [1] TRUE |
| >>> 3 != 2 True |
> 3 != 2 [1] TRUE |
| >>> 3 > 2 True |
> 3 > 2 [1] TRUE |
| >>> 3 < 2 False |
> 3 < 2 [1] FALSE |
| >>> 3 >= 3 True |
> 3 >= 3 [1] TRUE |
| >>> 3 <= 2 False |
> 3 <= 2 [1] FALSE |
| >>> x = [1,2,3,4,5] >>> 3 in x True >>> 6 not in x True |
> x <- c(1,2,3,4,5) > 3 %in% x [1] TRUE # not in N/A |
Boolean operators
| Python | R |
|---|---|
| >>> x = 10 >>> bool(x) True >>> x = 0 >>> bool(x) False |
N/A |
Lists (vectors in R)
| Python | R |
|---|---|
| >>> list1 = (1,2,3,4,5) >>> list1 (1, 2, 3, 4, 5) >>> list1[0] 1 |
> list1 <- c(1,2,3,4,5) > list1 [1] 1 2 3 4 5 > list1[1] [1] 1 |
| >>> import numpy as np >>> list1 = np.array([1,2,3,4,5]) >>> list1 array([1, 2, 3, 4, 5]) >>> list1[0] 1 |
Dictionary (list in R)
| Python | R |
|---|---|
| >>> fruit = {“Apple":250, “Orange":200, “Melon":500} >>> fruit {'Apple’: 250, 'Orange’: 200, 'Melon’: 500} >>> fruit[“Apple"] 250 |
> fruit <- list(Apple=250, Orange=200, Melon=500) > fruit $Apple [1] 250 $Orange [1] 200 $Melon [1] 500 > fruit[“Apple"] $Apple [1] 250 |
Functions
| Python | R |
|---|---|
| >>> def myfunc(x, y): … return x + y … >>> myfunc(3,2) 5 |
> myfunc <- function(x,y){ return (x + y) } > myfunc(3,2) [1] 5 |
If statement
Python
total =100
if total >= 100:
print("100 or more")
elif total >= 50:
print("Less than 100 More than 50")
else:
print("Less than 50")
R
total <- 100
if(total >= 100){
print("100 or more")
}else if(total >= 50){
print("Less than 100 More than 50")
}else{
print("Less than 50")
}
Switch statement
R
fruit <- c("Apple","Orange","Melon","Grape","Pineapple")
val <- sample(fruit, size=1)
switch(val,
"Apple" = print("It's an apple"),
"Orange" = print("It's an orange"),
"Melon" = print("It's a melon"),
print("Other fruit")
)
Python
From Python 3.10 onwards, the match~case construct, which is equivalent to a switch statement, has been introduced.
import random
fruit=["Apple","Orange","Melon","Grape","Pineapple"]
val = (random.choice(fruit))
match val:
case("Apple"):
print("It's an apple")
case("Orange"):
print("It's an orange")
case("Melon"):
print("It's a melon")
case _:
print(“Other fruit")
Python
Until Python 3.9, you can use if-elif-else as an alternative.
import random
fruit=["Apple","Orange","Melon","Grape","Pineapple"]
val = (random.choice(fruit)) # Select a value randomly from the fruit list.
if val == "Apple":
print("It's an apple")
elif val == "Orange":
print("It's an orange")
elif val == "Melon":
print("It's a melon")
else:
print(“Other fruit")
Logical operators
Python
and, or, not
age = 6
height = 130
# Age is 6 or older and height is 120cm or taller.
if(6 <= age and 120 <= height):
print("You can board.")
else:
print("You cannot board.")
# Age is 6 or older, or height is 120cm or taller.
if(6 <= age or 120 <= height):
print("You can board.")
else:
print(“You cannot board.")
# The person is not 6 years old.
if not(age == 6):
print("Not 6 years old.")
else:
print("6 years old.")
R
&, |, !
age <- 6
height <- 120
# Age is 6 or older and height is 120cm or taller.
if(6 <= age & 120 <= height){
print("You can board.")
}else{
print("You cannot board.")
}
# Age is 6 or older, or height is 120cm or taller.
if(6 <= age | 120 <= height){
print("You can board.")
}else{
print("You cannot board.")
}
# The person is not 6 years old.
if (!6 == age){
print("Not 6 years old.")
}else{
print("6 years old.")
}
for loop
Python
fruit=[“Apple","Orange","Melon","Grape","Pineapple"]
for val in fruit:
print(val)
for val in fruit:
if val == "Melon":
break
print(val)
R
fruit <- c("Apple","Orange","Melon","Grape","Pineapple")
for(val in fruit){
print(val)
}
for(val in fruit){
if(val=="Melon"){
break
}
print(val)
}
for(val in fruit){
if(val=="Melon"){
next
}
print(val)
}
while loop
Python
num=1
while num<10:
print(num)
num += 1
R
num <- 1
while(num < 10){
print(num)
num = num+1
}




