Skip to content

Instantly share code, notes, and snippets.

@sureshgorakala
Created December 2, 2017 09:15
Show Gist options
  • Save sureshgorakala/3ba55758350f4ee5da632078f82d32d3 to your computer and use it in GitHub Desktop.
Save sureshgorakala/3ba55758350f4ee5da632078f82d32d3 to your computer and use it in GitHub Desktop.
getting started with RStudio
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rstudio OverView"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"R-Studio overview \n",
"we have 4 panes \n",
"1) script pan - to write and save the programing script \n",
"2) Console pane - where all the code will get executed \n",
"3) Environment/history pane - displays all the variables created,functions used with in the current session \n",
"4) Helper pane - contains multiple tabs to install/display pacakges, view visualization plots, \n",
"locate files within the workspace \n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"help(mean)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# getting and setting workspace"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"'C:/Users/Suresh/mlclassscripts'"
],
"text/latex": [
"'C:/Users/Suresh/mlclassscripts'"
],
"text/markdown": [
"'C:/Users/Suresh/mlclassscripts'"
],
"text/plain": [
"[1] \"C:/Users/Suresh/mlclassscripts\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# to display current working directory use getwd() function\n",
"getwd()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# to set up workspace or working directory use setwd() function\n",
"#syntax is shown below\n",
"setwd(\"path\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"setwd(\"C:\\\\Suresh\\\\R&D\\\\Projects\\\\ML classroom training\\\\sessions\")\n",
"setwd(\"C:/Suresh/R&D/Projects/ML classroom training/sessions\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# getting help in R"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get help within R environment, we use help() function to get the documentation for any of the functions/packages available within R environment. \n",
"To see the arguments required for a function, we use args() function. \n",
"to see the example of a function, example() function is used. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"help(\"stats\")\n",
"help(\"mean\")\n",
"args(\"mean\")\n",
"example(\"mean\")\n",
"\n",
"#getting help documentation for a package\n",
"help(package=\"caret\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# online help for R programming"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can get online help on available packages in R from official website of R-Cran \n",
"https://cran.r-project.org/web/views/\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also get online support for our day to day activities from below websites: \n",
"https://stackoverflow.com/ \n",
"https://stats.stackexchange.com \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Installing Packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#install pacakges in R can be done in two ways,\n",
"#1) using install.packages() function and from the bottom right pane of Rstudio\n",
"install.packages(\"randomForest\")\n",
"\n",
"#loading of installed or downloaded packages can be done using library() function. Note that we can only load the package if\n",
"# we have installed the package already within our R environment\n",
"library(cluster)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#below code to first verify if the library is installed in the R environment, if it is not available\n",
"# then the package will get installed.\n",
"if(!library(cluster)){\n",
"install.pacakges(\"cluster\")\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# basic operations in R"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Adding two numericals\n",
"1+1\n",
"\n",
"#multiplying two numericals\n",
"10*2\n",
"\n",
"#dividing two numericals\n",
"10/2\n",
"\n",
"#applying modulus operation on two numericals\n",
"10%%2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# printing results to R console"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#printing the data on the console\n",
"print(10*2)\n",
"\n",
"print(\"data science\")\n",
"\n",
"print(pi^2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Variable declaration and assignment in R"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"variable assignment: In the below example, we are creating variable named z: "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"z <- 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we use left arrow or = symbol for variable assignment. Its always good practice to use left arrow for assignment. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"z = 10.009\n",
"z <- 10.009"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loading existing or default datasets available in R environment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can access default datasets avaiable in R using data() function. \n",
"data() function will displays all the avaiable datasets within R. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to load a specific dataset into R, we need to give the dataset name as argument to the data() function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data(AirPassengers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Viewing data of R objects"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To view first 5 records of a R object (ex:dataframe), we use head() function. \n",
"head() function expects the data object as argument and prints the first 5 records on the R console. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"head(AirPassengers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"to view all the records in a nice tabular view"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"View(AirPassengers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting the decription and structure of R object"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"use str function to see the descriptions of the data object,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"str(AirPassengers)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment