R Programming

Some simple differences between R and Python

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

Both R and Python are popular programming languages for data analysts. R is designed mainly for statistical data analysis, and Python may have more usage in deep learning and web application in addition to general data analysis. Although there are some common points between R and Python, i.e. they are both free and open source, and use data frames as tubular data storage, there are still some distinct differences between these two languages. Trying to understand and remember these characteristics will help you feel less confused when facing similar techniques for each of them.

  1. Python use = as assignment symbol, and R use <- as assignment symbol.
#In Python, to create an object 'age' , with value 23
age = 23

#In R, an object 'age' is created with scalar value 23
age <- 23

2. Like most of other famous programming languages, such as C++ and Java, Python has index starting from 0. However, R has index starting from 1.

#In Python, create a list of 4 countries
countries = ['USA','UK','Japan', 'Norway']
#print out the third country
print(countries[2])
#output
Japan

#In R, create a vector of four countries
countries<- c('USA','UK','Japan', 'Norway')
#print out the third country
> print(countries[3])
#output
[1] "Japan"

3. Python use indentation to indicate the following statements belong to for example, for loop, function definition, etc. R use braces to enclose a block (for loop, function, etc).


#In Python, indentation combined with colon to represent block
#using for loop to print out each element of a list 'countries'
for country in countries:
    print(country) 
#output
USA
UK
Japan
Norway

#In R, using braces to represent block
#using for loop to print out each element of a vector 'countries'
for (country in countries) {
  print(country)
}

4. In R, using <- to create a new distinct object with the same value as the old one. In Python, using = to create a new object, but both the new and old objects refer to a single object.

#In R, create a new vector 'new_countries', same as 'countries'
new_countries <-countries
new_countries
#output
[1] "USA"    "UK"     "Japan"  "Norway"
#add a new element to 'new_countries'
new_countries[5] <-"Germany"
#to show values of the new vector
new_countries
#output
[1] "USA"     "UK"      "Japan"   "Norway"  "Germany"
#to show values of the old vector
countries
#output
[1] "USA"    "UK"     "Japan"  "Norway"
#new vector and old vector are not the same thing

#In Python, create a new list, same as the old one
new_countries = countries
new_countries 
#output
Out[5]: ['USA', 'UK', 'Japan', 'Norway']
#add a new element to the new vector
new_countries.append("Germany")
#print out the new list again
new_countries
#output
Out[9]: ['USA', 'UK', 'Japan', 'Norway', 'Germany']
#print out the old list again
countries
Out[10]: ['USA', 'UK', 'Japan', 'Norway', 'Germany']
#you can see that the old list has been added the new element too.
#Both the new and the old list refer to the same object. 


For getting more knowledge of R, you can watch R tutorial videos on our YouTube channel !

For more tutorials of learning Python, you can watch Python full video in 10 hours from our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago