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.
#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 !
Click here to download Python Course Source Files !
For online Python training registration, click here ! Pandas provides flexible ways of generating data…
For online Python training registration, click here ! Data frame is the tabular data object…
Click her for course registration ! When a data frame in Python is created via…
We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…