Blog posts on Data Science, Machine Learning, Data Mining, Artificial Intelligence, Spark Machine Learning

Saturday, December 2, 2017

Getting Started with R

In this post we get familiar with R Studio and basic syntax of R programming language

Rstudio OverView

we have 4 panes
1) script pane - to write and save the programing script
2) Console pane - where all the code will get executed
3) Environment/history pane - displays all the variables created,functions
used with in the current session
4) Helper pane - contains multiple tabs to install/display pacakges,
view visualization plots,
locate files within the workspace

In [1]:

getting and setting workspace

In [2]:
# to display current working directory use getwd() function
In [ ]:
# to set up workspace or working directory use setwd() function
#syntax is shown below
In [6]:
setwd("C:\\Suresh\\R&D\\Projects\\ML classroom training\\sessions")
setwd("C:/Suresh/R&D/Projects/ML classroom training/sessions")

getting help in R

To get help within R environment, we use help() function to get the
for any of the functions/packages available within R environment.
To see the arguments required for a function, we use args() function.
to see the example of a function, example() function is used.
In [ ]:

#getting help documentation for a package

online help for R programming

We can get online help on available packages in R from official website of R-Cran
We can also get online support for our day to day activities from below websites:

Installing Packages

In [ ]:
#install pacakges in R can be done in two ways,
#1) using install.packages() function and from the bottom right pane of Rstudio

#loading of installed or downloaded packages can be done using library() function.  
#Note that we can only load the package if
# we have installed the package already within our R environment
In [ ]:
#below code to first verify if the library is installed in the R environment, 
#if it is not available
# then the package will get installed.

basic operations in R

In [ ]:
# Adding two numericals

#multiplying two numericals

#dividing two numericals

#applying modulus operation on two numericals

printing results to R console

In [ ]:
#printing the data on the console

print("data science")


Variable declaration and assignment in R

variable assignment: In the below example, we are creating variable named z:
In [8]:
z <-  100
we use left arrow or = symbol for variable assignment. Its always good
practice to use left arrow for assignment.
In [9]:
z = 10.009
z <- 10.009

Loading existing or default datasets available in R environment

we can access default datasets avaiable in R using data() function.
data() function will displays all the avaiable datasets within R.
In [ ]:
In order to load a specific dataset into R, we need to give the dataset name as argument to the data() function
In [ ]:

Viewing data of R objects

To view first 5 records of a R object (ex:dataframe), we use head() function.
head() function expects the data object as argument and prints the first 5 records on the R console.
In [ ]:
to view all the records in a nice tabular view
In [ ]:

Getting the description and structure of R object

use str function to see the descriptions of the data object,
In [ ]:


  1. Hey! Would you mind if I share your blog with my myspace group? There's a lot of folks that I think would really enjoy your content. Please let me know. digital marketing agency

  2. Wonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks internet marketing company singapore

  3. You made some first rate points there. I seemed on the internet for the issue and found most individuals will go together with along with your website. phonak hearing aids

  4. Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original ideas on this subject. realy thank you for beginning this up. this website is something that is wanted on the internet, somebody with just a little originality. useful job for bringing something new to the web! Yoox

  5. you are really a good webmaster. The website loading speed is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterpiece. you have done a fantastic job on this topic! reputation management

  6. I get pleasure from, lead to I discovered exactly what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye. web design in singapore

  7. I don’t even know how I ended up right here, however I thought this submit was great. I do not recognise who you are however certainly you're going to a well-known blogger when you aren't already ;) Cheers!
    Link building in Singapore

  8. Wow! Thank you! I constantly wanted to write on my site something like that. Can I take a portion of your post to my website? seo plan

  9. You could certainly see your enthusiasm in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.

  10. I was wondering if you ever thought of changing the layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 images. Maybe you could space it out better? strategy for serious digital marketer

  11. Hi,
    I found your web site by means of Google while looking for a related subject, your site got here up I will make sure to bookmark it and come back to read more of your useful information. I really appreciate this post.

  12. If you were to buy domain singapore, you should look for MediaOne. The agency should be able to handle your specific needs and requirements in the right manner. They would ensure that you would have a significant presence in the online realm.