R vs. Python – Basic differences


R and python are two of the main programming languages used in data science. Sometimes it may be necessary to move from one programming language to another. For example because python is the more generalized programming language while R is more specialized on data analysis.
At the pyconDE 2022 I gave a talk with the title Rewriting your R analysis code in Python.
This is part 1 of a series of blogposts about R vs. Python.
Part 1 is about the simple and more obvious differences.

Summary

  • Part 1: Basic differences
  • Part 2: Pipes
  • Part 3: Classes
  • Part 4: Library Loading
  • Part 5: … vs. args,kwargs
  • Part 6: Non-standard evaluation
  • Part 7: Run R code from python and vice versa

Basic Syntax

R has a classic C-like syntax with curly brackets {} for flow control
and parentheses () for stuff like if, for and while.
Python does not. It uses the colon : to start a block and indentation to define the block content. This also means python needs a no-operation keyword for empty blocks. pass

R:

x <- 1
if(x > 0){
  # do nothing
}

python:

x = 1
if x > 0:
    pass
    # do nothing

Assignment

Python goes the way of most programming languages of using = for assigning values to variable names:

python:

x = 1

R:

The =-sign can also be used in R, but this in not recommended.
The recommended way is using <-:

x <- 1

There is also the assignment operator <<-. This can be used when you want to assign values to variables that are not defined in the function you are writing, or should be usable outside of the function that does the assignment. This is useful when you work with classes and objects for example.

Non-local assignment:

foo <- function(x){
  b <<- 1
}
foo(2)
print(b)

And, thus also recommended, there is ->

1 -> x

Scalar vs. Vector

Despite having a different syntax, the most obvious difference between python and R is, that indexing in R starts counting its indices at 1, while python starts at 0.

R:

data <- c(1,2,3,4)
print(data[1])

python:

```r
data <- [1,2,3,4]
print(data[0])

In R vectors, all values have the same data type. They are similar to numpy arrays in python:

import numpy as np
foo = np.array([1])
print(foo[0])   

Indexing: 1 vs. 0

The other major difference between R and python is, that each variable is a vector by default in R:

foo <- 1
print(foo[1])

While in python you have scalars:

foo = 1
print(foo[0])
>>> TypeError: 'int' object is not subscriptable

Lists

Lists in R can have different data types per value. They are similar to lists and dictionaries in python.

python:

myList = [1,"abc",[1,2,3]]

R:

myList <- list(1,"abc",list(1,2,3))

Kommentar verfassen

Ihre E-Mail-Adresse wird nicht veröffentlicht.