Equivalents in R, Python and Perl

Last update 2015 September 9th

I've been using Perl heavily for several years until I started my PhD back in 2010 (I still use it for many tasks but much more sparingly). Perl was widely used back in the early days when the human genome was yet to be sequenced and this famous article explained some of the reasons. However, the general trend is that Perl is on the decline, while Python on the rise. There are many reasons for this trend and at least for the biologists/bioinformaticians who program in Python that I have spoken to, they all claim that syntactically, Perl is ugly when compared to Python. Perl was what I learned first when entering bioinformatics, so I got used to looking at Perl code. However, given this trend, I will need to learn some Python to be able to read the growing number of scripts/applications written in Python. Surely it's not necessary, but I have a penchant for looking into how things are done.

Nowadays I find myself using R more often, for its awesome graphical capabilities and wide array of statistical packages for bioinformatics. In fact R is also garnering more interest in the era of big data and data analysis. There have been debates over R versus Python for data analysis but personally I have decided to dedicate more time learning R given Bioconductor.

My plan is to at least learn the very basics of Python and R, with respect to Perl. So I thought I would write a post on the coding equivalents in R, Python and Perl. The motto of Perl is that there is more than one way to do it, so for Perl there can be several examples (especially since I'm more familiar with Perl).

Lastly, check out these online learning resources for learning R and Python.

For loops

As mentioned in the comments by Pablo and Jason (and as I found out later from experience), don't use loops in R but use apply().

#prints 0 to 9
for (i in 0:9){
  print(i)
}

#using an index
index <- c('one', 'two', 'three')
for (i in index){
  print(i)
}

#example from Pablo
sapply(X=0:9, FUN=function(x) x )

In Python (indentation is required as part of the language):

#prints 0 to 9
for num in range(0, 10):
   print num

#using a list
my_list = [1,2,3,4,5,6]
for i in my_list:
    print i

In Perl:

for (my $i = 0; $i < 10; ++$i){
   print "$i\n";
}

for $i (0 .. 9) {
   print "$i\n";
}

While loops

In R:

i <- 1
while (i < 10){
  print(i)
  i <- i + 1
}

In Python:

count = 0
while (count < 9):
   print count
   count = count + 1

In Perl:

$i = 0;
while ($i < 10) {
   print ++$i, "\n";
}

Arrays

In R (http://cran.r-project.org/doc/manuals/R-intro.html#Arrays):

a <- array(1:5)
a
[1] 1 2 3 4 5

for(i in a){
  print(i)
}

In Python and more information here:

from array import *
a=array('i',[1,2,3,4,5])
for i in a:
   print(i)

In Perl:

@a = (1,2,3,4,5);
for $i (@a) {
   print "$i\n";
}

Defining functions

In R:

multiply_by_two <- function(n) {
   n*2
}
multiply_by_two(2)
#4

In Python (see http://docs.python.org/release/1.5.1p1/tut/functions.html):

#The keyword def introduces a function definition
def multiply_by_two(n):
   print n * 2

multiply_by_two(2)

In Perl:

sub multiply_by_two {
   my ($n) = @_;
   #last evaluation is returned by default
   $n*2;
}
print multiply_by_two(2), "\n";

Conditionals

In R:

greater_less_equal_5 <- function(answer){
  if (answer > 5){
    print(1)
  }
  else if (answer < 5){
    print(-1)
  } else {
    print(0)
  }
}
greater_less_equal_5(4)
greater_less_equal_5(5)
greater_less_equal_5(6)

In Python:

def greater_less_equal_5(answer):
    if answer > 5:
        return 1
    elif answer < 5:
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

In Perl:

sub greater_less_equal_5 {
   $answer = @_[0];
   if ($answer > 5){
      return(1)
   } elsif ($answer < 5){
      return(-1)
   } else {
      return(0)
   }
}

print greater_less_equal_5(4), "\n";
print greater_less_equal_5(5), "\n";
print greater_less_equal_5(6), "\n";

Objects

Firstly, everything in R is treated like as an object. However, R has three object oriented (OO) systems: [[S3]], [[S4]] and [[R5]]. The basic idea is that a function is defined which creates a list.

NorthAmerican <- function(eatsBreakfast=TRUE,myFavorite="cereal"){
   me <- list(hasBreakfast = eatsBreakfast,
              favoriteBreakfast = myFavorite
             )
   ## Set the name for the class
   class(me) <- append(class(me),"NorthAmerican")
   return(me)
}

bubba <- NorthAmerican()

In Python, the simplest form of class definition looks like this:

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'

From perlootut:

An object is a data structure that bundles together data and subroutines which operate on that data. An object's data is called attributes, and its subroutines are called methods. An object can be thought of as a noun (a person, a web service, a computer). A class defines the behaviour of a category of objects. A class is a name for a category (like "File"), and a class also defines the behaviour of objects in that category; all objects belong to a specific class.

In Perl most objects are hashes and a Perl class is defined in a Perl module file (*.pm) with a package declaration. Here's an example:

package Shape;

sub new {
    my $class = shift;
    my $self = {
        color  => 'black',
        length => 1,
        width  => 1,
    };
    return bless $self, $class;
}

1;

And a script:

#!/usr/bin/env perl

use strict;
use warnings;
use Shape;

# create a new Shape object
my $shape = Shape->new;

# print the shape object attributes
print join("\t", $shape->{color}, $shape->{length}, $shape->{width}), "\n";

exit(0);

Notes

As suggested in the comments, R is not optimised for loops so please see the link in the comments section on how to use the apply() function.

Will be continually updated...

Links

An online environment for interactively exploring various programming languages

Print Friendly



Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.
9 comments Add yours
  1. Want to give you some better Perl examples. In Perl you actually don't use the 3 part for loop that much and a for loop over a range should be the following to be equivalent to the other languages:

    #!/usr/bin/perl
    for $i (0 .. 9) {
    print "$i\n";
    }

    In the while loop example merge the two lines of code in the loop:

    #!/usr/bin/perl
    $i = 0;
    while ($i < 10) {
    print ++$i, "\n";
    }

    The array example would be

    #!/usr/bin/perl
    @a = (1,2,3,4,5);
    for $i (@a) {
    print "$i\n";
    }

    1. I wrote the 3 part for loop for people that may be coming from C. But surely I think it is better to list them both. Thank you for the suggestions.

  2. Avoid big loops if you can in R. It is optimised for vectorisation (like Numpy) and really bad at loops. Instead use the various apply statements. R being a functional language is a natural Map/Reduce platform.

          1. Thanks for your blog. I enjoy it very much.
            I also bumped into R while studying data science and I use it quite often now.

            Just to smooth the move from your original basic examples to the advanced material in the post by Neil Saunders (referred to here by Dave) allow me to simplify further the R code that does a for loop printing 0 to 9 to the screen.

            For that let's use the functional programming style that R is more efficient at.
            The philosophy is to apply a function over a range of values described by a vector. It looks like this using the simple apply function:

            > sapply( X=0:9, FUN=function(x) x )
            [1] 0 1 2 3 4 5 6 7 8 9

            or even like this if you strip the optional parameter labels of sapply:

            > sapply(0:9, function(x) x )
            [1] 0 1 2 3 4 5 6 7 8 9

            Thanks again

            1. Thanks Pablo; glad you found the blog enjoyable 🙂 I now avoid loops whenever possible in R, after learning a bit more about R since writing this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.