Let's start learning R !

R Tutorial 1.0

Starting the tutorial on R @ Ask Analytics.

We are in the process of learning R, hence sharing all our notes with you.   We are on a mission of simplifying R. Covering few basic things which are must know to start the programming in R.




We recommend to install R and then R-Studio in you PC as working directly on R is not very user friendly.

Introduction to R-Studio :




Let's now start learning the most basic coding of R

But before that few must know things about R
1. Unlike SAS, R is case sensitive in all respects. Here “a” and “A” are different objects.

2. Equality operator for assignment purpose is  “<-” or “->” or “=” . All these are same. I prefer to use "=" as I am primarily a SAS programmer and old habit die hard.

`A = 4
 B <-  4
 4 -> C

3. Any statement starting with # is considered as non-executable comment, but it can not be of multiple line. For multiple line comment, use # in start of every line.

4. Ctrl + Enter or Ctrl + R command is used to execute a code in Script Editor window.

5. Use ctrl + L for clearing console

We might add few points as we learn more about R

The best to learn any language or script, best way is start writing the code.  Please do not copy paste as it won't help you learn much.

Run these codes one by one as see what happens in R :

#Defining numeric vector ( Vector is an one dimensional object)

A = 4
B = 5
C = A + B

#Defining character vector

name =  "Ask Analytics“

#Checking values of an object (here vector), just type its name and execute

A

#You want to check the list of objects you have created so far

ls()

#You want to remove an object you have created

rm(A)

#You want to remove more than one object you have created

rm(B,C)

#You want to remove all the objects you have created

rm(list = ls())

Let's work a little more on Vectors

# let's now learn creating vectors in R  - as I have already told vectors are one dimensional objects

x1 =  c(1,3,5,7)

x2 = c("Male","Female")

x3 = 2:9

x4 =seq(from = 2, to = 10, by = 2)

x5 = 2:7 + 1:6

x6=   rep(1:4, times =  3)

# Arithmetic operations can be performed on a vector

a = 1:4
b = a*2
c = a*a
d = a^2

# Let's clear all the objects created so far

rm(list = ls())

# Arithmetic operations can be performed between among vectors of same length

a = 1:4
b = 2:5
c = 3:6
d= a+b+c
e = a+b
f = a^b
g = a*b


Now when we know how to create vectors, let's learn how to select a part of vector and how to modify it.




2 comments:

Do provide us your feedback, it would help us serve your better.