1.
DATA IN R
AIM
To write a R program working with directories, working with export and import
files.
a) WORKING WITH DIRECTORIES IN R
1. getwd()
getwd()
Output
>getwd()
[1] “C:/Users/avcde/OneDrive/Documents”)
2. setwd()
Setwd(“C:/Users/avcde/OneDrive/Documents”)
Output
Setwd(“C:/Users/avcde/OneDrive/Documents”)
>getwd()
[1]”C:/Users/avcde/OneDribe/Documents”)
3. [Link]()
>[Link]()[1]”Custom office Templates” “[Link]”
“kingsoftData”
[4]”[Link]” project” “[Link]”
[7] “[Link]” “[Link]” “Sample.o”
b) IMPORTING FILES IN R
1. IMPORTING TEXT FILES
Getwd(_
Data<-[Link](“[Link]”, header= FALSE, sep=””)
Print(data)
Print(class(data))
Output
>data<-[Link](“[Link]”,header=FALSE, sep=””)
>print(data)
V1 V2 V3
1 100 A a
2 200 B b
3 300 C c
4 400 D d
5 500 E e
6 600 F f
>print(class(data))
[1]”[Link]”
2. Importing CSV files
getwd()
data<-[Link](“[Link]”, header=FALSE, sep=”\t”)
print(data)
print(class(data)
Output:
>data<-[Link](“[Link]”, header=FALSE, sep=”\t”)
>print(data)
V1
1 V1, V2, V3
2 100, AB, ab
3 200,CD,cd
4 300,EF,ef
5 400,GH,gh
6 500,IJ,ij
>print(class(data)
Output
3. Importing Excel files
[Link](“openxlsx”)
library(openxlsx)
getwd()
data<-[Link](“[Link]”,sheet=1)
print(data)
print(class(data))
output:
>getwd()
[1]”C:/Users/avcde/OneDrive/Documents”
>
Data<-[Link](“[Link],sheet=1)
>print(data)
X1 X2 X3
1 1000 ABC abc
2 2000 DEF def
3 3000 GHI ghi
4 4000 JKL jki
5 5000 MNO mno
>
>print(class(data))
[1]”[Link]”
c) EXPORTING FILES IN R
(1) Cat()
Str=”word”
Cat(“Hello.”,str,file=”[Link]”)
Output
(2) Sink()
Sink(“[Link]”)
X<-c(1,3,4,5,10)
Print(mean(x))
Print(class(x))\print(median(x))
Sink()
Output
(3) Writing to CSV Files
X<-c(1,3,4,5,10)
Y<-c(2,4,6,8,10)
Z<-c(10,12,14,16,18)
Data<-cbind(x,y,z)
[Link](data, file=”[Link]”,[Link]=FALSE)
Output
RESULT
Thus, above program executed successfully
2. READING AND WRITING DATA IN R
AIM
To write a R program working with reading and writing data.
1. Data()
#list of all the dataset
Data()
Output
#Load a specific dataset(e.g., ‘AirPassengers’)
Datqa(AirPassengers)
#Display the first few rows of the dataset
Head(AirPassengers)
Jan Feb Mar Apr May Jun
1949 112 118 132 129 121 135
2. Reading data in R
getwd()
data<-[Link](“[Link]”|)
data<-[Link](“[Link]”)
output
>getwd()
[1]”C:/Users/avcde/OneDrive/Documents”
>data<-[Link](“[Link]”)
>data<-[Link](“[Link]”)
3. Writing data in R
X<-[Link](V1=5, V2=10, V3=pi)
[Link](x,file=”[Link]”,sep=”,”)
Output
RESULT
Thus, above program executed successfully
3. DATES IN R
AIM
To write a R program working with dates.
1. Lubridate
Library(lubridate)
date_object<-ymd(“2025-12-03”)
date_object
Output:
>date_object<-ymd(“2025-12-03”)
>date_object
[1]”2025-12-03”
2. Chron
#Load the necessary packages
library(chron)
library(libridate)
#Create a datetime object with corrected date format
datetime_object<- chron(dates = c(“02/03/2024”,”02/04/2024”),
times =c(“12:30:45”,”15:20:10”))
#display the datetime object
datetime_object
Output
>library(lubridate)
>#create a datetime object with corrected date format
>datetime_object<-chron(dates=c(“02/03/2024”,”02/04/2024”),
+times=c(“12:30:45”,”15:20:10”))
>#display the datetime object
>datetime_object
[1](02/03/24 12:30:45)(02/04/24 15:20:10)
3. zoo
#load the zoo package
Library(zoo)
#create a simple dataset
Date<-[Link](
Date_column=[Link](c(“2024-02-01”, “2024-02-03”,”2024-02-06”)),
Value_column=c(10,15,20)
)
#display the dataset
Print(data)
Output
>#display the dataset
>print(data)
Date_column value_column
1 2024-02-01 10
2 2024-02-03 15
3 2024-02-06 20
3. ggplot2
library(ggplot2)
ggplot(data, aes(x=date_column, y=value_column))+
geom_line()+
labs(title=”Time Series Plot”,x=”Date”,y=”Value”)
labs
Output
[Link] operations with date and time
#Extract year, month, and day
Year(date_object)
month(date_object)
day(date_object)
Output
>year(date_object)
[1]2025
>month(date_object)
[1]12
day(date_object)
[1]3
Date and time arithmetic
date1<-date_object+8
date 1
#subtract 3 months from the date
date2<-date_object-months(3)
date2
#calculate the difference in days between two dates
time_difference<-difftime(date1, date2, units=”days”)
time_difference
Output
>date1<-date_object+8
>date 1
[1]”2025-12-11”
>#subtract 3 months from the date
>date2<-date_object-months(3)
>date2
[1]”2025-09-03”
>#calculate the difference in days between two dates
>time_difference<-difftime(date1, date2, units=”days”)
>time_difference
Time difference of 99 days
4. FACTORS IN R
AIM
To write a R program working with character manipulation
Creating a factor in R
>x<-c(“female”,”male”,”male”,”female”)
>print(x)
Output
[1]”female” ”male””male””female”
>gender<-factor(x)
>print(gender)
[1] female male male female
Levels: female male
Creating levels in factor
>gender<-factor(c(“female”,”male”,”male”,”female”),
+ levels=c(“female”,”transgender”,”male”))
>print(gender)
Output
[1] female male male female
Levels: female transgender male
Checking for a factor
>gender<-factor(c(“female”,”male”,”male”,”female”));
>print([Link](gender))
Output
[1] TRUE
Use of function class()
>gender<-factor(c(“female”,”male”,”male”,”female”))
>class(gender)
Output
[1]”factor”
Accessing elements of a factor
>gender<-factor(c(“female”,”male”,”male”,”female”))
>print(gender[3])
Output
[1] male
Levels:female male
>gender<-factor(c(“female”,”male”,”male”,”female”))
>print(gender[c(2,4)])
Output
[1] male female
Levels: female male
Modification of a factor
>gender<-factor(c(“female”,”male”,”male”,”female”))
>gender[2]<-“female”
>print(gender)
Output
[1] female female male female
Levels:female male
Removing elements from a factor in R
>gender<-factor(c(“female”,”male”,”male”,”female”))
>print(gender[-3])
Output
[1]Female male female
Levels: female male
Factors in date frame
>age>-c(40,49,48,40,67,52,53)
>salary<-c(103200,106200,150200,10606,10390,14070,10220)
>gender<c(“male”,”male”,”transgender”,”female”,”male”,”female”,
”transgender”)
>employee<-[Link](age,salary,gender=factor(gender))
>print(employee)
Output
Age salary gender
1 40 103200 male
2 49 106200 male
3 48 150200 transgender
4 40 10606 female
5 67 10390 male
6 52 14070 female
7 53 10220 transgender
>print([Link](employee$gender))
Output
[1] TRUE
RESULT
Thus, above program executed successfully
5. CHARACTER MANIPULATION IN R
AIM
To write a R program working with character manipulation.
1. Conversion to Upper Case
> print (toupper(c("r", "PROgramming")))
[1] "R" "PROGRAMMING"
2. Conversion to Lower Case
> print (tolower(c("Learn R", "hI")))
[1] "learn r" "hi"
3. Using casefold () function
> print (casefold(c("Learn R", "hI")))
[1] "learn r" "hi"
Using casefold () function with upper
> print (casefold(c("Learn R", "hI"), upper = TRUE))
[1] "LEARN R" "HI"
4. Character Replacement
> chartr("a", "A", "An honest man gave that")
[1] "An honest mAn gAve thAt"
> chartr("is", "#@", c("This is it", "It is great"))
[1] "Th#@#@#t" "It #@ great"
5. Splitting the String
> strsplit("Welcome to R", "")
[1] "Welcome" "to" "R"
6. Working with Sub strings
> substr("Learn Code in R", 1, 4)
[1] "Lear"
Extraction of Character from a String
> str <- c("program","with","a","new","language")
> substr(str, 3, 3)<-c("%")
> print(str)
[1] "pr%gram" "wi%h" "a" "ne%" "la%guage”
RESULT
Thus, above program executed successfully
6. DATA AGGREGATION IN R
AIM
To write a R program working with data aggregation.
1. Aggregate the sum of marks
data= [Link](subjects=c("java", "python", "java", "java", "php", "php"),
+ id=c(1, 2, 3, 4, 5, 6),
+ names=c("manoj", "sai", "mounika", "durga", "deepika", "roshan"),
+ marks=c(89, 89, 76, 89, 90, 67))
> cat("\nSample Data Frame\n")
Output:
Sample Data Frame
> print(data)
Output:
1 java 1 Manoj 89
2 python 2 sai 89
3 java 3 mounika 76
4 java 4 durga 89
5 php 5 deepika 90
6php 6 roshan 67
> cat("\nAggregate sum of marks with subjects\n")
Output:
Aggregate sum of marks with subjects
> print(aggregate(data$marks, list(data$subjects), FUN-sum))
Output:
Group.1-x
2 java 254
2 php 157
3 python 89
2. Aggregate the minimum marks
> data=[Link](subjects=c("java", "python", "java", "java", "php", "php"),
+ id=c(1, 2, 3, 4, 5, 6),
+ names=c("manoj", "sai", "mounika", "durga", "deepika", "roshan").
+ marks c(89, 89, 76, 89, 90, 67))
> cat("\nSample Data Frame\n")
Output:
Sample Data Frame
> print(data)
Output:
subjects id names marks
1 java 1 manoj 89
2 python 2 sai 89
3 java 3 mounika 76
4 java 4 durga 89
5 php 5 deepika 90
6 php 6 roshan 67
> cat("\nAggregate minimum of marks with subjects\n")
Output:
Aggregate manimum of marks with subjects
>print(aggregate(data$marks, list(data$subjects),FUN=min))
Output:
Group. 1 x
1 java 76
2 php 67
3 python 89
3. Aggregate the maximum marks
> data [Link](subjects=c("java", "python", "java","java", "php", "php"),
+ id=c(1, 2, 3, 4, 5, 6),
+names=c("manoj", "sai", "mounika", "durga", "deepika", "roshan"),
+marks=c(89, 89, 76, 89, 90, 67))
> cat("\nSample Data Frame\n")
Output
Sample Data Frame
> print(data)
Output
subjects id names marks
1 java 1 manoj 89
2 python 2 sai 89
3 java 3 mounika 76
4 java 4 durga 89
5 php 5 deepika 90
6 php 6 roshan 67
> cat("\nAgggregate maximum of marks with subjects\n")
Output
Agggregate maximum of marks with subjects
> print(aggregate(data$marks, list(data$subjects), FUN-max))
Output
Group. 1 x
1 java 89
2 php 90
3 python 89
4. Aggregate the mran marks
> data = [Link](subjects=c("java", "python", "java","java", "php", php"),
+ id=c(1, 2, 3, 4, 5, 6),
+ names=c("manoj", "sai", "mounika", "durga", "deepika", "roshan"),
+ marks=(89, 89, 76, 89, 90, 67))
> cat("\nSample Data Frame\n")
Output
Sample Data Frame
> print(data)
Output
subjects id names marks
1 java 1 manoj 89
2 python 2 sai 89
3 java 3 mounika 76
4 java 4 durga 89
5 php 5 deepika 90
6 php 6 roshan 67
> cat("\nAgggregate mean of marks with subjects\n")
Output
Agggregate mean of marks with subjects
> print(aggregate(data$marks, list(data$subjects), FUN-mean))
Output:
Group 1 x
1 java 84.66667
4. php 78.50000
5. python 89.00000
RESULT
Thus, above program executed successfully
[Link] DATA BASICS IN R
AIM
To write a R program working with reshaping data basics.
1. Transpose of a Matrix
> first <-matrix(c(1:12), nrow 4, byrow TRUE)
> print("Original Matrix")
Output
[1] "Original Matrix"
> first
Output
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
> first <- t(first)
> print("Transpose of the Matrix")
Output
[1] "Transpose of the Matrix"
> first
[,1] [,2] [,3] [,4]
[1,] 14 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
2. Joining rows and columns in data frame
name<-c("Shaoni", "esha", "soumitra", "soumi")
>age<-c (24, 53, 62, 29)
> address<- c("puducherry", "kolkata", "delhi", "bangalore")
>info <-cbind(name, age, address)
print("Combining vectors into data frame using cbind ")
Output
[1] "Combining vectors into data frame using cbind "
> print(info)
Output
name age address
[1,] "Shaoni" "24" "puducherry"
[2,] "esha" "53" "kolkata"
[3,] "soumitra" "62" "delhi"
[4,] "soumi" "29" "bangalore"
> newd <- [Link](name=c("sounak", "bhabani"),
+age=c("28", "87"),
+address=c("bangalore", "kolkata"))
> [Link] <- rbind(info, newd)
> print("Combining data frames using rbind ")
Output
[1] "Combining data frames using rbind "
> print([Link])
Output
name age address
1 Shaoni 24 puducherry
2 esha 53 kolkata
3 soumitra 62 delhi
4 soumi 29 bangalore
5 sounak 28 bangalore
6 bhabani 87 kolkata
3. Merging two data frames
>d1 <- [Link](name=c("shaoni", "soumi", "arjun"),
+ID=c("111", "112", "113"))
> d2 <- [Link](name=c("sounak", "esha"),
+ID=c("114", "115"))
> total <- merge(d1, d2, all=TRUE)
> print(total)
Output
name ID
I arjun 113
2 esha 115
3 shaoni 111
4 soumi 112
5 sounak 114
[Link] & Casting
[Link]("reshape2")
library(reshape2)
a<- [Link](id=c("1", "1", "2", "2"),
points=c("1", "2", "1", "2"),
x1 =c("5", "3", "6", "2"),
x2=c("6", "5", "1", "4"))
a$x1<-[Link]([Link](a$x1))
a$x2 <- [Link]([Link](a$x2))
print("Melting")
m<-melt(a, id = c("id", "points"))
print(m)
print("Casting")
idmn <- dcast(m, id ~ variable, mean)
print(idmn)
Output
> library(reshape2)
>a<- [Link](id = c("1", "1", "2", "2"),
+points=c("1", "2", "1", "2").
+x1=c("5", "3", "6", "2")
+x2=c("6", "5", "1", "4"))
> a$x1 <- [Link]([Link](a$x1))
> a$x2 <- [Link]([Link](a$x2))
> print("Melting")
[1] "Melting"
>m<-melt(a, id = c("id", "points"))
> print(m)
id points variable value
111 x1 5
21 2 xl 3
32 1xl6
42 2 x1 2
511x2 6
612x2 5
72 1x2 1
82 2x2 4
> print("Casting")
[1] "Casting"
> idmn <- dcast(m, id~ variable, mean)
> print(idmn)
id x1 x2
1 1 45.5
2 2 4 2.5
RESULT
Thus, above program executed successfully
8. ENVIRONMENTS IN R PROGRAMMING
AIM
To write a R program working environments.
[Link] new environment
># R program to illustrate
> #Environments in R
>#Create new environment
> [Link]()
>#Assigning variables
> newEnv$x<-1
> newEnv$y - "GFG"
> newEnvSz <- 1:10
># Print
> print(newEnv$z)
Output
[1] 1 2 3 4 5 6 7 8 9 10
[Link] bindings and environments
>#R program to illustrate
>#Environments in R
># Prints all the bindings and environments
>#attached to Global Environment
>1s()
Output
[1] "a" "address" "age" "d1"" "d2" "data"
[7] "first" "info" "name" "[Link]" "newd" "newEnv"
[13] "str" "total"
># Prints bindings of newEnv
> Is(newEnv)
Output
[1] "x" "y" "z"
> # Lists all the environments of the parent environment
> search()
Output
[1] " .GlobalEnv" "package:stats" "package: graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
3. Removing a variable from an environment
> # R program to illustrate
> # Environments in R
> # Remove newEnv
> rm(newEnv)
> # List
> 1s()
[1] "a""address" "age" "d1" "d2" "data"
[7] "first" "info" "name" "[Link]" "newd" "str"
[13] "total"
RESULT
Thus, above program executed successfully
9. PROBABILITY DISTRIBUTIONS IN R PROGRAMMING
DISCRETE PROBABILITY DISTRIBUTIONS IN R
AIM
To write a R program working with probability distributions in r programming
discrete probability distributions
[Link] Distribution
> random_binom-rbinom(100, size 10, prob-0.5)
> print(random_binom)
Output
[1]5355477572735476653465881457455546442
[38] 6644553556654555456645466355446532565
[75] 65744354566844534556866454
2. Bernoulli Distribution
random_bern <-rbinom(100, size=1, prob = 0.7)
> print(random_bern)
Output
[1]1001111111011100011111111111111010011
[8]11111 10011111111100411110111
[75]11101111101101111 1111111
[Link] Distribution
> random_pois <- rpois(100, lambda4)
> print(random_pois)
Output
[1] 632153 3 0 0 4 4 3 4 24 23 24 312365 242324312365
[26] 3424327542452674782915163
[51] 51 6 7 5 11 6 2 2 1 5 6 5 324 232714467
[76] 8446755512365253354275343
4. Geometric Distribution
> random_geom <- rgeom(100, prob=0.3)
> print(random_pois)
Output
[1] 6 3 2 15 3 3 0 0 4 4 3 4 2 4 2 3 2 4 3 12365
[26] 3 4 2 4 3 2 7 5 4 2 4 5 2 674782915163
[51] 5 1 6 7 5 11 6 2 2 1 5 6 5324232714467
[76] 8 4 4 6 7 5 5 5 1 2 3 6 5 2 5 3 3 5 4 27 5343
5. Multinomial Distribution
> random_multinom <- rmultinom(5, size 10, prob c(0.2, 0.3, 0.5))
> print(random_multinom)
Output
[,1] [,2] [,3] [,4] [,5]
[1,] 3 2 3 2 3
[2,] 3 1 2 3 4
[3,] 4 7 5 5 3
CONTINUOUS PROBABILITY DISTRIBUTIONS IN R
1. Normal Distribution
> random_norm <-morm(100, mean = 0, sd = 1)
> print(random_norm)
Output
[1]-0.919341657 1.390720033 0.809427983 0.351979925
2.227143010
[ 6] 0.063440247-0.753314534-0.398877249-1.103400587 0.413265794
[11] -1.645516667 -0.912209724 1.046925285 -2.463157337 0.811463879
[16] 0.186918539 0.086174140 1.741701208 1.238743963 0.278767290
[21] 0.636776725 -0.011686320 1.054278568-0.115092194-1.086577815
[26]-1.290395920 0.043986470 -0.267697236-1.360023095-0.370810474
[31] 0.316889693 1.510905420-0.997148912-1.012304397 0.073613130
[36] 0.038407972 0.481388187-0.115197146-1.825224306-0.382039833
[41] 0.584063373 0.431331722-2.376357160 0.106689153 0.008351961
[46] 0.955417547 0.407145929 3.362575385 0.595817429-1.264127393
[51] -0.718443749 -0.792294059 0.296234438 0.770099800 1.342398655
[56] 1.248027986 1.415457849-0,738777697-0.941330327 1.605260718
[61] -0.789742684-1.106685771-1.726383567-0.782109821 0.462486722
[66]-1.119982577 1.277523389 0.340938020 1.347927890-1.292153516
[71] -0.533149653-0.652763339-0.481542289-0.885681502 0.219070132
[76] 0.365520802 0.519800485-1.489817216 0.409562956 1.915442847
[81] 1.164499733 0.196295276 -0.673814844 -1.536498355 -0.656317808
[86] 0.127347357 0.051864540-0.871026596 1.359360383 0.143801199
[91] -0.209749159 0.382418022 -0.360529013 1.282419809-1.381024615
[96] 0.372625613 0.210984384 -1.145737403 -0.952252820 0.722684729
[Link] Distribution
> random_unif <- runif(100, min = 0, max = 10)
> print(random_unif)
Output
[1] 2.6256160 1.2332170 8.7770636 9.3443948 2.6381241 4.4573952
5.8870576
[8] 1.6395074 5.6255084 5.8423445 0.1700955 1.1360783 4.0108352
3.3453966
[15] 5.4271806 6.4049868 8.3614948 2.1057522 7.6694073 2.4131689
8.6231069
[22] 2.6916112 4.3951967 0.4834655 7.5561720 1.2676518 3.0633500
4.2898338
[29] 5.0143844 9.6283371 2.4018921 3.8982415 2.3650430 1 .5879233
7.5292500
[36] 1.5993802 2.2806803 3.9371402 0.9789073 6.4488886 4.2314811
8.6435711
[43] 2.74951731.7454290 6.0513264 5.2774339 3.7955800 6.5703037
8.7987157
[50] 1.1709596 1.1202194 8.9612933 6.1166142 7.8970843 2.3541449
3.5539400
[57] 7.8416800 8.0973503 6.3013054 4.8838248 2.3594661 4.4522716
0.2448638
[64] 9.5756048 9.9041982 9.1944898 3.9654951 4.8767827 5.3329513
8.0905837
[71] 6.14200846.4174191 7.4760896 7.6427828 3.6870927 1.6374277
1.5210226
[78] 0.7048777 8.1870492 5.6156130 1.9538286 6.5231240 2.0095016
0.9265736
[85] 0.4270713 2.7067859 4.3479043 3.8769554 4.3437046 5.8257682
0.5707062[9
2] 5.4367867 1.3887391 6.4161711 9.4166001 6.4041892 3.3836290
4.9091925
[99] 6.8731746 2.2128284
[Link] Distribution
> random_exp <-rexp(100, rate 0.2)
> print(random_exp)
Output
[1] 4.5327532 2.1668321 1.9772678 5.0846330 5.4311406 2.2909370
[7] 2.4043474 11.3842944 16.6327974 0.9230381 0.5895209 5.4444799
[13] 6.5456017 0.7626095 2.9978745 2.4529865 2.0446709 1.2417154
[19] 7.6957020 2.3186856 5.2318249 2.9535085 2.5051816 2.4167934
[25]1.2316907 3.9254978 0.9512432 3.7183720 8.1380046 1.9690159
[31] 5.2426306 2.2050108 8.8306825 1.8778072 3.4922518 3.9046181
[37] 1.3555960 2.2406531 0.3535281 8.8219606 1.6353217 0.6560930
[43] 3.5281410 3.3605720 3.9624594 1.5517642 0.3044121 5.6210719
[49] 3.5395618 10.3912864 4.9563014 10.9038191 0.3495357 5.8940973
[55] 8.9726434 1.2262921 0.3245226 1.0070736 5.3654620 3.7122087
[61] 1.5046875 6.6179955 0.5743696 18.4141039 9.8222738 8.2660317
[67] 1.5271626 1.2650589 0.1476639 3.5766104 2.0515863 3.8311461
[73] 0.2057002 0.5362979 8.2067138 1.9822141 2.6161216 13.0829947
[79] 3.7336106 0.9614676 1.2686809 10.5735512 6.2967761 1.6048500
[85] 2.0193197 25.2059811 5.4777930 5.2751219 1.6820785 3.6524680
[91] 12.1164474 9.9290416 3.5191814 5.5131822 15.0861995 3.7734848
[97] 7.4115789 0.2878848 0.5810395 0.7238995
[Link]-Square Distribution
> random_chisq <- rchisq(100, df = 5)
>print(random_chisq)
Output
[1] 5.5758180 6.2804199 4.8720346 2.8778300 3.9935383 1.4949945
[7] 3.2159761 4.5516127 9.5956268 0.2430110 2.6412218 5.7251423
[13] 5.0743572 9.0702417 2.3388406 2.8961188 3.2848282 5.0505868
[19]3.1236124 4.9610649 2.7987953 3.8206417 2.5264850 2.3045163
[25]7.1725068 3.1369965 12.3964382 0.2068554 0.4431692 6.2652135
[31]7.1199901 4.1508109 2.0193734 7.2021280 5.2948424 2.0750069
[37]1.6846540 8.1422755 3.6276832 5.3095023 5.9205225 1.6287872
[43] 2.2572593 5.8827826 4.0755820 2.2747781 8.4636136 8.6965359
[49] 2.6992146 7.3604034 2.7551497 9.8806293 9.0641367 3.0943505
[55] 13.4150738 14.4956024 6.6621349 3.1682860 7.0034344 9.7766336
[61] 5.8162856 1.8077478 1.9634453 11.6121387 5.0035986 4.2977837
[67] 4.1624703 8.3957026 3.5651599 4.7815358 2.6967483 1.9289805
[73] 3.0940093 1.9574113 2.0476225 5.8019280 4.1976845 2.8510848
[79] 8.1958123 5.2185593 1.1607806 2.8348691 3.9921539 6.2654673
[85] 5.3487922 2.2530770 8.5474384 6.5413368 1.1117534 3.2678811
[91] 3.2513491 1.6764915 4.8348654 4.3721826 17.8135254 6.1280407
[ 97] 2.0589410 9.2894836 2.5495691 6.2857099
RESULT
Thus, above program executed successfully
10. READING TABULAR DATA IN R
AIM
To write a R program working with reading tabular data
(a) Reading Tabular Data from a CSV file
[Link]("[Link]")
Output
Index [Link] [Link] Last Name
Company
1 DD37Cf93accA6Dc Sheryl Baxter Rasmussen Group
2 Preston Lozano Vega-Gentry
1Ef7b82A4CAAD10
3 6F94879bDAfE5a6 Roy Berry Murillo-Perry
4 5Cef8BFA16c5e3c Linda Olsen Dominguez,
Mcmillan and
Donovan
5 053d585Ab6b3159 Joanna Bender Martin, Lang
and Andrade
6 2d08FB17EE273F4 Aimee Downs Steele Group
7 EA4d384DfDbBf77 Darren Peck Lester, Woodard
and Mitchell
(b) Reading Tabular Data from a JSON file
[Link]("rjson")
library(rjson) #loads the rjson library
fromJSON(file="[Link]")
Output
Sname
[1] "John"
Sage
[1] 25
Scity
[1] "Sampleville"
$married
[1] FALSE
$hobbies
[1]"reading" "traveling" "programming"
RESULT
Thus, above program executed successfully
11. POWER ANALYSIS IN STATISTICS R
AIM
To write a R program working with power analysis in statistics
(a) Load the required package in power analysis
[Link]("pwr")
library(pwr)
(b) Set parameters and conduct a Priori Power Analysis for a Two Sample T
test
#Parameters for two-sample t-test
effect_size_t <-0.5 # Moderate effect size (Cohen's d)
alpha_t<0.05
# Significance level
power_t - 0.8
# Desired power
#Calculate required sample size
sample_size_t [Link](d = effect_size_t, [Link] = alpha_t,
power power_t, type = "[Link]")$n
#Output
cat("Sample Size for Two-Sample t-Test:", sample_size_t, "\n")
Output
Sample Size for Two-Sample t-Test: 63.76561
(c) Conduct a Prior Power Analysis for one-way ANOVA
#Parameters for one-way ANOVA
effect_size_anova <-0.25 #Small effect size (Cohen's f)
alpha_anova<0.05
# Significance level
power_anova 0.8
# Desired power
#Calculate required sample size
sample_size_anova [Link](k=3, f= effect_size_anova,
[Link] = alpha_anova, power power_anova)Sn
# Output the result
cat("Sample Size for One-Way ANOVA:", sample_size_anova, "\n")
Output
Sample Size for One-Way ANOVA: 52.3966
d) Generate a Power Curve
#Parameters for power curve
sample_size_curve <-100 #Sample size per group
effect_sizes_curve <- seq(0.2, 0.8, by=0.1) # Range of effect sizes
#Calculate power values
power_values_curve <- sapply(effect_sizes_curve, function(d) [Link](d = d,
nsample_size_curve,
[Link] = alpha_t,
type="[Link]") Spower)
# Plot power curve
plot(effect_sizes_curve, power_values_curve, type = "b",
main = "Power Curve for Two-Sample t-Test",
xlab= "Effect Size (Cohen's d)",
ylab = "Power",
ylim = c(0, 1))
Output
RESULT
Thus, above program executed successfully
12. MULTIPLE REGRESSION IN R
AIM
To write a R program working with multiple regression
(a) Input Data
> input - mtcars[,c("mpg","disp","hp","wt")]
> print(head(input))
Output
mpg disp hp wt
Mazda RX4
21.0 160 110 2.620
Mazda RX4 Wag
21.0 160 110 2.875
Datsun 710
22.8 108 93 2.320
Hornet 4 Drive
21.4 258 110 3.215
Hornet Sportabout
18.7 360.175 3.440
Valiant
18.1 225 105 3.460
(b) Create Relationship Model and get the coefficients
> input <- mtcars[,c("mpg","disp","hp","wt")]
> # Create the relationship model.
> model <- Im(mpg-disp+hp+wt, data = input)
> # Show the model.
> print(model)
Output
Call
Im(formula mpg-disp+hp+wt, data = input)
Coefficients:
(Intercept)
disp
hp
wt
37.105505
-0.000937
-0.031157
-3.800891
(c) Print the Coefficients and Displacement Values:
>#Get the Intercept and coefficients as vector elements.
> cat("#### The Coefficient Values ###","")
####The Coefficient Values ###
>a<- coef(model)[1]
> print(a)
(Intercept)
Output
37.10551
> Xdisp <- coef(model)[2]
>Xhp <- coef(model)[3]
>Xwtcoef(model) [4]
> print(Xdisp)
Output
-0.0009370091
> print(Xhp)
hp
Output
-0.03115655
> print(Xwt)
wt
Output
-3.800891
(d) Apply Equation for predicting new Values
>#For a car with disp=221, hp 102 and wt = 2.91 the predicted mileage is
>#Intercept-37.15,Disp=-0.000937,hp--0.0311,wt=-3.8008
> Y-37.15+(-0.000937)*221+(-0.0311)*102+(-3.8008)*2.91-22.7104
RESULT
Thus, above program executed successfully
13. LOGISTIC REGRESSION IN R
AIM
To write a R program working with logistic regression
1. Importing the Data Set
[Link]("dplyr")
library(dplyr)
head(mtcars)
Output
2. Splitting the Data Set
[Link]("caTools")
library(caTools)
split <- [Link](mtcars, SplitRatio = 0.8)
train_reg <- subset(mtcars, split "TRUE")
test_reg <- subset(mtcars, split "FALSE")
3. Building the Model
logistic_model <- glm(vs-wt + disp,
data train_reg.
family = "binomial")
logistic_model
Output
Call: glm(formulavswt + disp, family "binomial", data train_reg)
Coefficients:
(Intercept)
wt
disp
3.29098
1.12527
-0.03253
Degrees of Freedom: 22 Total (i.e. Null); 20 Residual
Null Deviance: 31.84
Residual Deviance: 13.96
AIC: 19.96
summary(logistic_model)
Output
Coefficients:
Estimate Std.
Error z
value
Pr(>/z)
(Intercept)
3.29098
3.59135
0.916
0.3595
wt
1.12527
1.82008
0.618
0.5364
disp
-0.03253
0.01640
-1.983
0.0474
Signif. codes: 0****0.001 *** 0.01 '*' 0.05 0.1'' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 31.841 on 22 degrees of freedom
Residual deviance: 13.964 on 20 degrees of freedom
AIC: 19.964
Number of Fisher Scoring iterations: 6
4. Predict Test Data based on the Model
predict_reg <- predict(logistic_model,
test_reg, type="response")
predict_regas.[Link]
(predict_reg)
predict_reg
Output
predict_reg
Datsun 710
0.915950166
Hornet Sportabout
0.010474670
Merc 240D
0.891745334
Merc 450SLC
0.193598691
Lincoln Continental
0.003801488
Honda Civic
0.933757769
Pontiac Firebird
0.004524736
Porsche 914-2
0.856424577
Ferrari Dino
0.844405349
RESULT
Thus, above program executed successfully
14. SURVIVAL ANALYSIS IN R
AIM
To write a R program working with survival analysis
(a) Kaplan-Meier Method
#Installing package
[Link]("survival")
# Loading package
library(survival)
#Dataset information
?lung
#Fitting the survival model
Survival_Function survfit(Surv(lung$time,lung$status2)-1)
Survival_Function
#Plotting the function
plot(Survival_Function)
Output
(b) Cox Proportional Hazard Model
#Installing package
[Link]("survival")
#Loading package
library(survival)
#Dataset information
Plung
#Fitting the Cox model
Cox_mod - coxph(Surv(lung$time, lungSstatus 2), data = lung)
#Summarizing the model
summary(Cox mod)
# Fitting survfit()
Cox <- survfit(Cox_mod)
#Plotting the function
plot(Cox)
Output
RESULT
Thus, above program executed successfully
[Link] REGRESSION IN R
AIM
To write a R program working with poisson regression
input warpbreaks
print(head(input))
EXERO
0
Output
breaks
wool
tension
1 26
A
L
2
30
A
L
3
54
A
L
4
25
A
L
5
70
A
L
6 52
A
L
output-glm(formula breaks wool + tension,data warpbreaks, family = poisson)
print(summary(output))
Output
Call
glm(formula breaks - wool + tension, family poisson, data warpbreaks)
Coefficients
Estimate Std. Error z value Pr(>/z)
(Intercept) 3.69196 0.04541 81.302 <2e-16***
woolB -0.20599 0.05157 -3.994 6.49e-05***
tensionM
-0.32132 0.06027 -5.332 9.730-08
tensionH -0.51849 0.06396-8.107.5.21e-16***
Signif. codes: 0
0.001 0.01 0.05 0.11
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 297.37 on 53 degrees of freedom
Residual deviance: 210.39 on 50 degrees of freedom
AIC: 493.06
Number of Fisher Scoring iterations: 4
RESULT
Thus, above program executed successfully
16. NON-LINEAR CURVE FITTING IN R
AIM
To write a R program working with non-linear curve fitting
library([Link])
library(ggplot2)
x <- c(0, 1, 2, 3, 4, 5)
yc(1, 2, 4, 8, 16, 32)
start_values <- c(a=4, b=2)
fitnls(ya exp(bx),
start = start values,
algorithm = "port",
control = [Link](maxiter = 1000))
summary(fit)
Output
Formula: ya exp(bx)
Parameters
Estimate Std. Error t value Pr(>/t 피)
a 1.000e+00 6.762e-14 1.479e+13 <2e-16***
b 6.931e-01 1.434e-14 4.832e+13 <2e-16***
Signif. codes: 0***0.001** 0.01 0.05 0.11
Residual standard error: 3.498e-13 on 4 degrees of freedom
Algorithm "port", convergence message: absolute function convergence (6)
Plot the Exponential Regression Line with Points
ggplot([Link](x, y), aes(x, y)) +
geom_point() +
geom_line(aes(x, predict(fit, newdata = [Link](x)))) +
ggtitle("Exponential Regression") +
xlab("x") +
ylab("y")
Output
RESULT
Thus, above program executed successfully