Tuesday, August 25, 2020

R Programming Tutorials

 

R Programming

 

Data Types

Vector

a <- c('orange','white',"black")

print(a)

output

[1] "orange" "white"  "black" 

 

Lists

a <- list(c(12,33),'sabari')

print(a)

Output

[[1]]
[1] 12 33
 
[[2]]
[1] "sabari"
 

Matrix

M = matrix( c('a','a','b'), nrow = 1, ncol = 3, byrow = TRUE)

print(M)

Output

   [,1] [,2] [,3]
[1,] "a"  "a"  "b" 

 

Arrays

a <- array(c('sabari','nathan'),dim = c(2,2))

print(a)

Output

  [,1]     [,2]    
[1,] "sabari" "sabari"
[2,] "nathan" "nathan"

 

Factors

a <- c('sabari','sabari','nathan')

 

b <- factor(a)

 

print(b)

Output

[1] sabari sabari nathan
Levels: nathan sabari

 

DataFrames

a <-        data.frame(

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

   height = c(152, 171.5, 165)

)

print(a)

Output

  gender height
1   Male  152.0
2   Male  171.5
3 Female  165.0

 

 

 

Variable Declarations

#equal opeartor

a = c(0,1,2,3)          

print(a)

#left

a <- c(0,1,2,3)          

print(a)

#right

 c(0,1,2,3) -> a

print(a)

 

Output

[1] 0 1 2 3
[1] 0 1 2 3
[1] 0 1 2 3

 

If-else

a <- c("sabari","nathan")

 

if("sabari" %in% a) {

   print("found")

} else {

   print("not found")

}

Output

[1] "found"

 

Switch-Case

Example

x <- switch(

   2,

   "sabari",

   "nathan"

)

print(x)

Output

[1] "nathan"

 

For Loop

Syntax

for (value in vector) {
   statements
}

 

Example

a <- LETTERS[15:17]

for ( i in a) {

   print(i)

}

Output

[1] "O"
[1] "P"
[1] "Q"

 

While Loop

 

Syntax

while (test_expression) {
   statement
}

 

Example

a <- c("sabari","nathan","mca")

count <- 1

 

while (count <3) {

   print(a)

   count = count + 1

}

Output

[1] "sabari" "nathan" "mca"   
[1] "sabari" "nathan" "mca"   

 

Functions

1.Built-in Functions(Examples)

Sum

print(sum(50:70))

Output

1260

Mean

print(mean(15:30))

Output

22.5

 

Max

print(max(15,30))

Output

30
 
2.User Defined Functions

 

Example

a <- function(a) {

  print(a)

}

 

a("sabari")

Output

Sabari

Strings

a <- 'Sabarinathan'

print(a)

b <- "MCA"

print(b)

Output

[1] "Sabarinathan"
[1] "MCA"

 

toupper() & tolower()  Functions in String

a <- toupper('Sabarinathan')

print(a)

 

b <- tolower("MCA")

print(b)

Output

[1] "SABARINATHAN"
[1] "mca"

 

Substring

a <- substring('Sabarinathan',3,7)

print(a)

Output

[1] "barin"

 

Data-Interfaces

1.R with CSV Files


Save the file sabari.csv

data <- read.csv("sabari.csv")

print(data)

 

Output

 

      id,   name,    salary,   start_date,     dept
1      1    sabari     8000    2020-01-01      IT
2      2    nathan     6000    2020-02-23      Sales
3      3    saranya    9000    2020-03-15      IT
4      4    nithi      4000    2020-03-15      Sales

 

Analysis

 

data <- read.csv("sabari.csv")

print(ncol(data))

print(nrow(data))

 

Output

5

3

Maximum Salary

data <- read.csv("sabari.csv")

a <- max(data$salary)

print(a)

Output

9000

 

Get the details of the person with max salary

data <- read.csv("sabari.csv")

b<- subset(data, salary == max(salary))

print(b)


Output

 

id,   name,    salary,   start_date,     dept
3      3    saranya    9000    2020-03-15      IT

 

 

Get the details of the person with Working in Sales Department

data <- read.csv("sabari.csv")

b<- subset(data, dept ==”Sales”)

print(b)

 

Output

      id,   name,    salary,   start_date,     dept
2      2    nathan     6000    2020-02-23      Sales
4      4    nithi      4000    2020-03-15      Sales

 

Get the persons in Sales  department  & salary is greater than 5000

data <- read.csv("sabari.csv")

b    <- subset(data, salary > 5000 & dept == "Sales")

print(b)

 

Output

      id,   name,    salary,   start_date,     dept
2      2    nathan     6000    2020-02-23      Sales

 

Writing into a CSV File

data <- read.csv("sabari.csv")

b<- subset(data, salary == max(salary))

write.csv(b,"output.csv")

c <- read.csv("output.csv")

print(c)

Output

id,   name,    salary,   start_date,     dept
3      3    saranya    9000    2020-03-15      IT

 

Excel Format

Sabari.xlsx


Step 1
 
install.packages("xlsx")

 

Step 2

data <- read.xlsx("sabari.xlsx", sheetIndex = 1)

print(data)

 

 

 

Output

 

      id,   name,    salary,   start_date,     dept
1      1    sabari     8000    2020-01-01      IT
2      2    nathan     6000    2020-02-23      Sales
3      3    saranya    9000    2020-03-15      IT
4      4    nithi      4000    2020-03-15      Sales

 

XML Format

Step 1

install.packages("XML")

 

<RECORDS>

   <EMPLOYEE>

      <ID>1</ID>

      <NAME>Sabari</NAME>

      <SALARY>8000</SALARY>

      <STARTDATE>1/1/2020</STARTDATE>

      <DEPT>IT</DEPT>

   </EMPLOYEE>

               

   <EMPLOYEE>

      <ID>2</ID>

      <NAME>nathan</NAME>

      <SALARY>6000</SALARY>

      <STARTDATE>23/2/2020</STARTDATE>

      <DEPT>Sales</DEPT>

   </EMPLOYEE>

  

   <EMPLOYEE>

      <ID>3</ID>

      <NAME>saranya</NAME>

      <SALARY>9000</SALARY>

      <STARTDATE>15/03/2020</STARTDATE>

      <DEPT>IT</DEPT>

   </EMPLOYEE>

  

   <EMPLOYEE>

      <ID>4</ID>

      <NAME>nithi</NAME>

      <SALARY>4000</SALARY>

      <STARTDATE>15/03/2020</STARTDATE>

      <DEPT>Sales</DEPT>

   </EMPLOYEE>

  

               

</RECORDS>

Step2

library("XML")

library("methods")

a <- xmlParse(file = "sabari.xml")

print(result)

 

Output

1

sabari

8000      

1/1/2020             

IT

2

nathan 

6000      

23/2/2020           

Sales

3

saranya               

9000      

15/03/2020        

IT

4

nithi      

4000      

15/03/2020        

Sales

 

JSON

Step1

install.packages("rjson")

 

sabari.json

{

    "Sheet1": [

        {

            "id": "1",

            "name": "sabari",

            "salary": "8000",

            "start_date": "1/1/20",

            "dept": "IT"

        },

        {

            "id": "2",

            "name": "nathan",

            "salary": "6000",

            "start_date": "23/2/2020",

            "dept": "Sales"

        },

        {

            "id": "3",

            "name": "saranya",

            "salary": "9000",

            "start_date": "15/03/2020",

            "dept": "IT"

        },

        {

            "id": "4",

            "name": "nithi",

            "salary": "4000",

            "start_date": "15/03/2020",

            "dept": "Sales"

        }

    ]

}

Example

library("rjson")

a <- fromJSON(file = "sabari.json")

print(a)

Output

 

$ID
[1] "1"   "2"   "3"   "4"   
$Name
[1] "Sabari"     "nathan"      "saranya" "nithi"     
$Salary
[1] "8000"  "6000"  "9000"    "4000"    
$StartDate

[1] " 1/1/2020"   " 23/2/2020"  " 15/03/2020" " 15/03/2020

$Dept
[1] "IT"  "Sales " "IT"   "IT”
 
Database with R

 

Step 1

install.packages("RMySQL")

 

Example

con = dbConnect(MySQL(), user = 'root', password = '', dbname = 'sabari',

   host = 'localhost')

 dbListTables(mysqlconnection)

Output

[1] "t"

 

 

Example

result = dbSendQuery(mysqlconnection, "select * from t")

a = fetch(result, n = 1)

print(a)

Output

        id,   name,    salary,   start_date,     dept
1      1    sabari     8000    2020-01-01      IT

 

 

R charts & Graphs

 

Pie-Charts

 

x <- c(80,90,70)

labels <- c("trichy", "chennai", "madurai")

png(file = "D://city.png")

pie(x,labels)

dev.off()

Output


 

Bar Charts

a<- c(10,11,20,30)

png(file = "D://city.png")

barplot(a)

dev.off()

 

Output







Histogram

v <-  c(20,30,25,72,89,90)

png(file = "D://city.png")

hist(v,xlab = "title",col = "green",border = "yellow")

dev.off()

output

 


Line Graph

v <-  c(20,30,25,72,89,90)

png(file = "D://city.png")

plot(v,type = "o")

dev.off()

Output


 R Statitics

1.Mean

x <- c(12,7,3)

a <-  mean(x)

print(a)

Output

[1] 7.333333

2.Median

x <- c(12,7,3)

a <-  median(x)

print(a)

Output

[1] 7

3.Linear Regression

Formula

y = ax + b

 

 

x <- c(10,20,30,25,90)

y <- c(93,42,42,12,3)

a <- lm(y~x)

print(a)

Output

 

 Call:

lm(formula = y ~ x)

 

Coefficients:

(Intercept)            x

      65.70        -0.78

 

Logistic Regression

Formula

y = 1/(1+e^-(a+b1x1+b2x2+b3x3+...))

 

a <- mtcars[,c("am","cyl")]

 

print(head(a))

Output

                          am cyl

Mazda RX4          1   6

Mazda RX4 Wag      1   6

Datsun 710         1   4

Hornet 4 Drive     0   6

Hornet Sportabout  0   8

Valiant            0   6

Normal Distribution

x <- seq(-20,1, by = .2)

y <- dnorm(x, mean = 1, sd = .2)

png(file = "D://city.png")

plot(x,y)

dev.off()

Output

 


 

pnorm

x <- seq(-20,1, by = .2)

y <- pnorm(x, mean = 1, sd = .2)

png(file = "D://city.png")

plot(x,y)

dev.off()

Output



Binomial Distribution

dbinom()

x <- seq(-20,1, by = .2)

y <- dbinom(x,50,.2)

png(file = "D://city.png")

plot(x,y)

dev.off()

Output

 



 

pbinom

x <- pbinom(20,30,0.5)

 

print(x)

output

[1] 0.978613

 

Decision Tree

install.packages("party")

 

library(party)

a <- readingSkills[c(1:105),]

png(file = "D://city.png")

b <- ctree(

  nativeSpeaker ~ age + shoeSize + score,

  data = a)

plot(b)

dev.off()

Output

 


Chi Square Test

library("MASS")

car.data <- data.frame(Cars93$AirBags, Cars93$Type)

car.data = table(Cars93$AirBags, Cars93$Type)

print(chisq.test(car.data))

Output

        Pearson's Chi-squared test

 

data:  car.data

X-squared = 33.001, df = 10, p-value = 0.0002723

 

Random Forest

install.packages("randomForest)

 

library(party)

library(randomForest)

a <- randomForest(nativeSpeaker ~ age + shoeSize + score,

           data = readingSkills)

print(a)

Time Series

rainfall <- c(90,12,33,22,11,66,88,99)

rainfall.timeseries <- ts(rainfall,start = c(2012,1),frequency = 12)

print(rainfall.timeseries)

png(file = "D://city.png")

plot(rainfall.timeseries)

dev.off()

 

Output

 



No comments:

Post a Comment

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...