DEV Community

loading...
Cover image for R Programming Quick Tutorial

R Programming Quick Tutorial

jetsql profile image Shailesh chaudhary 惻3 min read

R is a open source free statistical programming language.R is a free software environment for statistical computing and graphics. It compiles and runs on a wide variety of UNIX platforms, Windows and MacOS.

Variable in R
There are 3 basic way to declare a variable in R.

1sy way, a=24
2nd way, b <- "Hello"
3rd way, False -> C

Others, Exceptional - c =5L, L-integer

Data Operator in R

  1. Arithmetic: (+, -,*,/,^, %%, %/%) 2.Assignment: (=,->>,<<-) 3.Relational (>,<,==,!=) 4.Logical (&(AND), |(OR), !(NOT)) 5.Special (:(range), %in%(between))

Data Type in R

  1. Vector - sequence of element

exp -
Vrt=(1,2,3,4) or vtr <- (1,2,3,4)
Operation - Indexing, replacing and sort

  1. Lists

Lists are the R object which contain elements of different types. like - number, strings, vectors and another lists.

Exp-
n=c(2,3,4)
s=c("aa","vv")
x=list(n,s, TRUE)

Operations - Merging (List1<-(1,2), List2('hi') merged.list <-c(list1,list2)), Slicing, Indexing

  1. Arrays

Arrays are the R data object which can store data in more than two dimensions.

Exp-

Vector1 <- c(4,6,7)
Vector2 <- c(10,12,13)
result <- array(c(vector1,vector2),dim=c(2,2,1))

4.Matrices

Matrices are the R object in which the elements are arranged in a 2-d rectangular layout.

-A matrix is created using matrix() function.

matrix(data, nrow, ncol, byrow, dimnames)
data- is the input vector which becomes the data elements of the matrix.
nrow-is the number of the rows to be created.
ncol-is the number of the columns to be created.
byrow- is the logical clue. If TRUE then input vector elements are the arraged by row.
dimname-is the names assigned to rows and columns.

exp-
vrt1 <- c(2,6,14,18,22)
vrt2 <- c(5,7,9,45,36)
mtr <- matrix(c(vtr1, vtr2), 5,5)

5.Factors
Factors are the data objects which are used to categorize the data and store it as levels.

  • They can store both strings and integers.
  • They are useful in the data analysis for statistical modeling.

data <- c("East","West","North","North","North","East","West","East")

factor_data <- factor(data)

exp -
vrt1 <- c(2,6,14,18,22)
factvtr <- factor(vrt1)

  1. Data Frames A data frame is a table or a 2-d array-like strucuture in which each column contains values of one variable and each row contains one set of values from each column.

emp_id=c(1:5),
emp_name=c("Richa", "Antima","Priya"),
salary= c(564.3,321.2,675.1),
emp.data <- data.frame(emp_id, emp_name, salary)

Flow Control Statements -:

IF,
x=5
if(x>3){
print("X is greater than 3")
}

IF-ELSE,
x=5
if(x>5){
print("X is greater than 5")
}
else if (x==5){
print("x equal to 5")
}
else{
print("x is not greater than 5")
}

Switch Case:

vtr <- c(150,200,250,300,350,400)
option <- "mean"
switch(option,
"mean"=mean((vtr)),
"mode"=mode((vtr))),
"median"=median((vtr)))
print("value invali")
)

Loops:
x=2
repeat{
x=x^2
print(x)
if(x>100)
break
}


x=2
while(x<1000){
x=x^2
print(x)
}

--

vtr <- c(7,19, 25,65,45)
for(i in vtr){
print(i)
}
print(vtr)


for(i in 1:15){
if (i%%2)==0){
next
}
print(i)
}

--

Visualization in R:

  1. Pie Chart

vtr <- c(12,28,11,30,17)
name <- c("R&D","Marketing","Sales","Support")
pie(vtr,name)

  1. Bar Chart: vtr <- c(12,28,11,30,17) name <- c("R&D","Marketing","Sales","Support") barplot(vtr)

3.Boxplot
boxplot(mpg ~cyl, data= mtcars, xlab = "number of Cylinders", ylab= "Miles per Gallon", main = "Mileage data")

  1. Histogram

vtr =(9,1,3,7,8,4,70)
hist(vtr)

  1. Line Graph:
    vtr =(9,1,3,7,8,4,70)
    plot(vtr, type = "o")

  2. Scatterplot:
    vtr1 =(9,1,3,7,8,4,70)
    vtr2 =(6,1,3,3,8,0,70)
    plot(vtr1, vtr2)

Iā€™m Shailesh Chaudhary an energetic and innovative Digital marketing specialist with over 3 years of experience. Iā€™m keen self-learner and dedicated online marketing enthusiast. As Digital marketing professional I learn & implement everything who related to marketing, data and strategy.

Visit my linkedin profile: https://www.linkedin.com/in/ishailesh/

Visit my blog: https://ishailesh.org/seo-hindi-tutorial/

Discussion (0)

pic
Editor guide