Module-1 Basics of R
> print("Welcome R Programming Language.")
[1] "Welcome R Programming Language."
> 5+6
[1] 11
> x=56**78
>x
[1] 2.283842e+136
> demo="hello R Programming"
> demo
[1] "hello R Programming"
> #this is r studio introduction
> demo1<-2+3
> demo2=5+6
> demo1
[1] 5
> demo2
[1] 11
> sessionInfo()
> y=1:5
> z=6:10
> plot(y,z)
> name="Parth Mistry"
> tolower(name)
[1] "parth mistry"
> toupper(name)
[1] "PARTH MISTRY"
> #R is case sensitive language
> a=10
> A=10
> #variable in R Programming
> var_name=10
> [Link]=10
> _var_name=10
Error: unexpected symbol in "_var_name"
> var_name*=10
Error: unexpected '=' in "var_name*="
> .var_name=10
> 5var_name=10
Error: unexpected symbol in "5var_name"
> v5ar_name5=10
> #Assignment of variable
> var1=10
> var2<-20
> 10->var3
> print(var1)
[1] 10
> cat(var1, " ",var2)
10 20
> cat(var1," ",var2, "\n",var3)
10 20
10
> #Types of Data
> #Under Atomic Types
> #1. Numeric
> x=25.67
> print(x)
[1] 25.67
> class(x)
[1] "numeric"
> typeof(x)
[1] "double"
> cat(x,class(x),typeof(x))
25.67 numeric double
> #2. Integer
> x=46864L
> print(x)
[1] 46864
> class(x)
[1] "integer"
> typeof(x)
[1] "integer"
> cat(x,class(x),typeof(x))
46864 integer integer
> #3. Character
> sub="Data Science"
> print(sub)
[1] "Data Science"
> class(sub)
[1] "character"
> typeof(sub)
[1] "character"
> cat(sub,class(sub),typeof(sub))
Data Science character character
> #4. Logical
> flag=TRUE
> print(flag)
[1] TRUE
> class(flag)
[1] "logical"
> typeof(flag)
[1] "logical"
> cat(flag,class(flag),typeof(flag))
TRUE logical logical
> # with & (used to compare 2 variables) as well as short of cat = c
> x=c(TRUE, FALSE, TRUE, FALSE)
> y=c(TRUE, TRUE, FALSE, FALSE)
> x[3] & y[3]
[1] FALSE
> # Logical &&
> x = c(TRUE, FALSE, TRUE)
> y = c(TRUE, TRUE, FALSE)
> result = x[2] && y[2]
> print(result)
[1] FALSE
> #Logical !
> x = TRUE
> !x
[1] FALSE
> #Logical | ||
> x = c(TRUE, FALSE, TRUE, TRUE, FALSE)
> y = c(FALSE, TRUE, FALSE, TRUE, FALSE)
> x|y
[1] TRUE TRUE TRUE TRUE FALSE
> x = c(TRUE, FALSE, TRUE)
> y = c(FALSE, TRUE, FALSE)
> x[1] || y[1]
[1] TRUE
> # Complex
> z=4+3i
> print(z)
[1] 4+3i
> class(z)
[1] "complex"
> typeof(z)
[1] "complex"
> #Raw
> raw_data= charToRaw("Hello")
> print(raw_data)
[1] 48 65 6c 6c 6f
> class(raw_data)
[1] "raw"
> typeof(raw_data)
[1] "raw"
> num = 123
> raw_data = charToRaw([Link](num))
> raw_data
[1] 31 32 33
> #converting data type in R
> num1<- [Link](26L)
> num1
[1] 26
> num2<- [Link](25-56i)> num2
[1] 25
> num3<- [Link](TRUE)
> num3
[1] 1
> num4<- [Link]("abcde456")
> num4
[1] NA
> num5<- [Link]("12345")
> num5
[1] 12345
> #integer
> int1<- [Link](52.6544)
> int1
[1] 52
> int2<- [Link](45-56i)
> int2
[1] 45
> int3<- [Link](TRUE)
> int3
[1] 1
> int4<- [Link]("12345abcd")
> int4
[1] NA
> #complex
> com1<- [Link](562.4556)
> com1
[1] 562.4556+0i
> com2<- [Link](45L)
> com2
[1] 45+0i
> com3<- [Link](FALSE)
> com3
[1] 0+0i
> #logical
> log1<- [Link](15.78i)
> log1
[1] TRUE
> log2<- [Link](0-5i)
> log2
[1] TRUE
> log3<- [Link](0)
> log3
[1] FALSE
> log4<- [Link]("1234")
> log4
[1] NA
> #character
> char1<- [Link](45L)
> char1
[1] "45"
> char2<- [Link](52-89i)
> char2
[1] "52-89i"
> char3<- [Link](TRUE)
> char3
[1] "TRUE"
#raw to character
> rawToChar([Link](c(0x68, 0x65, 0x6c, 0x6c, 0x6f)))
[1] "hello"
> rawToChar([Link](c(0x50, 0x61, 0x72, 0x74, 0x68, 0x20, 0x4d, 0x69, 0x73, 0x74, 0x72, 0x79)))
[1] "Parth Mistry"
> #data types=vectors, list, matrices, data frames, factors,
> ##element of vector are known as components
> #length(): no. of element in vector
> vec <-c(10, 20, 30, 40)
> print(vec)
[1] 10 20 30 40
> class(vec)
[1] "numeric"
> a<-c(3, 4, 5, 1, 5, 7)
>a
[1] 3 4 5 1 5 7
> b<--3:5
>b
[1] -3 -2 -1 0 1 2 3 4 5
> sq<-seq(1,5)
> sq
[1] 1 2 3 4 5
> sq<-seq(1,5, by=.5)
> sq
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
> sq<-seq(1,5, by=.4)
> sq
[1] 1.0 1.4 1.8 2.2 2.6 3.0 3.4 3.8 4.2 4.6 5.0
> sq<-seq(1,5,[Link]= 5)
> sq
[1] 1 2 3 4 5
sq<-seq(1,3, [Link]= 5)
> sq
[1] 1.0 1.5 2.0 2.5 3.0
> sq[4]
[1] 2.5
> sq[1]
[1] 1
[1] 1.00 25.75 50.50 75.25 100.00
> numv<-c(12,3, 52.6, 23,45, 89, 145)
> numv
[1] 12.0 3.0 52.6 23.0 45.0 89.0 145.0
> class(numv)
[1] "numeric"
> intv<-c(5, 6, 1, 8, 9, 7)
> intv<- [Link](intv)
> class(intv)
[1] "integer"
> intv<-c(5L, 6L, 1L, 8L, 9L, 7L)
> intv
[1] 5 6 1 8 9 7
> class(intv)
[1] "integer"
> charv<-c(1, 5, 8, 7, 9)
> charv<- [Link](charv)
> class(charv)
[1] "integer"
> charvv<-c("parth", "pruthvi", "varun", "rishi")
> charvv
[1] "parth" "pruthvi" "varun" "rishi"
> class(charvv)
[1] "character"
> #logical vector
> #Accessing element of vector
> #by indexing[]
> #indexing start from1 not 0
> char_vec<-c("parth"=12, "trusharkant"=32, "mistry"=31)
> char_vec
parth trusharkant mistry
12 32 31
> char_vec["parth"]
parth
12
> #vector operations
> a1<-c(1, 2, 3, 4, 5, 6, 7)
> a2<-c("parth", "trusharkant", "mistry")
> a3<-c(a1,a2)
> a3
[1] "1" "2" "3" "4" "5" "6"
[7] "7" "parth" "trusharkant" "mistry"
> a1<-c(1, 2, 3, 4, 5, 6, 7)
> a4<-c(5, 6, 7, 8, 1, 2, 4)
> a1+a4
[1] 6 8 10 12 6 8 11
> a4<-c(5, 6, 7, 8, 1, 2, 4)
> a1*a4
[1] 5 12 21 32 5 12 28
> a1-a4
[1] -4 -4 -4 -4 4 4 3
> a1/a4
[1] 0.2000000 0.3333333 0.4285714 0.5000000 5.0000000 3.0000000 1.7500000
> a2[3]
[1] "mistry"
> a2[7]
[1] NA
> a2[-2]
[1] "parth" "mistry"
> a2[2:4]
[1] "trusharkant" "mistry" NA
> #List
> lst <- list(25, "R language", TRUE)
> print(lst)
[[1]]
[1] 25
[[2]]
[1] "R language"
[[3]]
[1] TRUE
> #matrix
> mat <- matrix(1:6, nrow=2, ncol=3)
> print(mat)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> matrix(1:12, ncol=4, nrow=3)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(7:18, ncol=4, nrow=3)
[,1] [,2] [,3] [,4]
[1,] 7 10 13 16
[2,] 8 11 14 17
[3,] 9 12 15 18
> x<-matrix(1:12, nrow=3, ncol=4, byrow= TRUE)
>x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
> y<-matrix(7:18, nrow=3, ncol=4, byrow= TRUE)
>y
[,1] [,2] [,3] [,4]
[1,] 7 8 9 10
[2,] 11 12 13 14
[3,] 15 16 17 18
> z=x+y
>z
[,1] [,2] [,3] [,4]
[1,] 8 10 12 14
[2,] 16 18 20 22
[3,] 24 26 28 30
> z=x*y
>z
[,1] [,2] [,3] [,4]
[1,] 7 16 27 40
[2,] 55 72 91 112
[3,] 135 160 187 216
> d=x/y
>d
[,1] [,2] [,3] [,4]
[1,] 0.1428571 0.250 0.3333333 0.4000000
[2,] 0.4545455 0.500 0.5384615 0.5714286
[3,] 0.6000000 0.625 0.6470588 0.6666667
> e=x-y
>e
[,1] [,2] [,3] [,4]
[1,] -6 -6 -6 -6
[2,] -6 -6 -6 -6
[3,] -6 -6 -6 -6
> #data frames
> df <- [Link](Name=c("Alice", "Bob"),
+ Age=c(25,30),
+ Score=c(90.5,85))
> print(df)
Name Age Score
1 Alice 25 90.5
2 Bob 30 85.0
# Factor
> gender <- factor(c("Male", "Female", "Male", "Female"))
> gender
[1] Male Female Male Female
Levels: Female Male
> class(gender)
[1] "factor"
> # Arrays
> arr <- array(1:12, dim = c(3,2,2))
> arr
,,1
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
,,2
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
> # Type Checking & Type Conversion
> # Operators in R
> a <- 7.5
> b <- 2
> a+b #Addition
[1] 9.5
> a-b # Subtraction
[1] 5.5
> a*b #Multiplication
[1] 15
> a/b #Division
[1] 3.75
> a%%b #Reminder
[1] 1.5
> a%/%b #Quotient
[1] 3
> a^b #Exponent
[1] 56.25
> # Vectors Function (Collection of similar data types)
> c1 <- c(8,9,6)
> c2 <- c(2,4,5)
> c1 + c2
[1] 10 13 11
> c1 - c2
[1] 6 5 1
> c1 * c2
[1] 16 36 30
> c1 / c2
[1] 4.00 2.25 1.20
> c1 %% c2
[1] 0 1 1
> c1 %/% c2
[1] 4 2 1
> c1 ^ c2
[1] 64 6561 7776
> # Relational
>a
[1] 7.5
>b
[1] 2
> a<b
[1] FALSE
> a>b
[1] TRUE
> a==b
[1] FALSE
> a<=b
[1] FALSE
> a>=b
[1] TRUE
> a!=b
[1] TRUE
> c1
[1] 8 9 6
> c2
[1] 2 4 5
> c1<c2
[1] FALSE FALSE FALSE
> c1>c2
[1] TRUE TRUE TRUE
> c1==c2
[1] FALSE FALSE FALSE
> c1<=c2
[1] FALSE FALSE FALSE
> c1>=c2
[1] TRUE TRUE TRUE
> c1!=c2
[1] TRUE TRUE TRUE
> # R if-else Statements
> x <- 25L
> if([Link](x)){
+ print("x is an integer number")
+}
[1] "x is an integer number"
> if([Link](x)){
+ print("x is an integer number")
+ }else{
+ print("x is not an integer number")
+}
[1] "x is an integer number"
> # R Switch Case
> # Switch (expression, case1, case2)
> x <- switch (2,
+ "Nilam",
+ "Parmar",
+ "John",
+ "Sunita"
+)
>x
[1] "Parmar"
> x <- switch (4,
+ "Nilam",
+ "Parmar",
+ "John",
+ "Sunita"
+)
>x
[1] "Sunita"
> #data manepulation
> ##merging and joining data
> # creating sample data frames
> df1 <- [Link](ID=c(1,2,3), Name=c("A","B","C"))
> df2<- [Link](ID=c(2,3,4), Score=c(85,90,78))
> #Inner Join
> df_inner <- merge(df1,df2,by="ID")
> View(df_inner)
> #left Join
> df_left <- merge(df1,df2, by="ID", all.x = TRUE) #NA fill for missing value
> View(df_left)
> #Right join
> df_right <- merge(df1,df2, by="ID", all.x = TRUE)
> View(df_right)
> #Full join
> df_full <- merge(df1,df2, by="ID", all = TRUE)
> View(df_full)
> # Importing Data Set
> # Exporting Data Set
> # CSV File
> library(readxl) # IMPORT
> Parth <- read_excel("C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]")
New names:
• `` -> `...25`
• `` -> `...29`
> View(Parth)
> library(writexl) # EXPORT
> employee <- c("John", "Emily", "Sophia", "Michael", "Sarah", "David", "Anna")
> age <- c(28, 34, 25, 42, 31, 38, 29)
> salary <- c(50000, 60000, 52000, 80000, 56000, 75000, 54000)
> department <-c("HR", "Finance", "Marketing", "IT", "HR", "Finance", "Marketing")
> experience <- c(3, 10, 2, 15, 5, 12, 4)
> df <- [Link](employee, age, salary, department, experience)
> df
employee age salary department experience
1 John 28 50000 HR 3
2 Emily 34 60000 Finance 10
3 Sophia 25 52000 Marketing 2
4 Michael 42 80000 IT 15
5 Sarah 31 56000 HR 5
6 David 38 75000 Finance 12
7 Anna 29 54000 Marketing 4
> # FOR CSV
> [Link](df, "employee_data.csv", [Link] = T)
> # FOR EXCEL
> write_xlsx(df, "employee_data.xlsx")
> # FOR JavaScript
> write_json(df, "employee_data.json", pretty = T)
Module 2
#create data for the graph
#pie charts
x<-c(12,35,56,75)
lables<-c("India", "UK", "Japan", "USA")
pie(x,lables)
#pie chart (to write the head and colour fill)
x<-c(12,35,56,75)
lables<-c("India", "UK", "Japan", "USA")
pie(x,lables, main="country pie chart", col=rainbow(length(x)))
to add colour by self
x<-c(12,35,56,75)
lables<-c("India", "UK", "Japan", "USA")
colors<-c("blue","green","red","orange")
pie(x,lables, main = "country pie chart",col = colors)
to add length and colours
x<-c(12,35,56,75)
lables<-c("India", "UK", "Japan", "USA")
colors<-c("blue","green","red","orange")
pie(x,lables, main = "country pie chart",col = colors)
legend("topright", c("India","UK","Japan","USA"), cex = 0.8, fill = colors)
#Dimensional pie chart
x <- c(12, 35, 56, 75)
labels <- c("India", "UK", "japan", "USA")
pie_lables <- paste(round(100 * x/sum(x),2),"%")
pie(x, labels = pie_lables)
# labels and %
# Define Data
x <- c(12, 35, 56, 75)
labels <- c("India", "UK", "Japan", "USA")
#Calculate Percentages
percent <- round(x/sum(x)*100,1)
labels_with_percent <- paste(labels, percent, "%")
#Define custom colors
colors <- c("pink", "green", "red", "orange")
#Create the pie chart with percentages
pie(x, labels = labels_with_percent, main = "Country Pie Chart", col = colors)
#Legend
legend("topright", labels_with_percent, cex = 1, fill = colors)
#Excel Sheet
#Simple
# Read the data from the Excel file
data_path <- "C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
#count occurrences of each gender
gender_count <- table(data_df$Gender)
colors <- c("blue", 'red')
#Create Pie Chart
pie(gender_count, labels = names(gender_count),
col = colors, main = "Gender Distribution")
#bar charts
H1<-c(82,48,66,23,41)
barplot(H1)
H2<-c(12,35,20,40,33,38)
M2<-c("feb","mar","apr","may","may","jun")
barplot(H2, [Link]=M2, xlab="month",
ylab="Revenue", col="yellow", main="Revenue Bar Chart",
border="red")
#Group Bar Chart & Stacked Bar Chart
months<-c("jan","feb","mar","apr","may")
region<-c("west","north","south","east")
values<-matrix(c(21,22,25,40,67,66,87,78,69,56,44,23,88,55,85), nrow=4,
ncol=5, byrow= TRUE)
barplot(values, main="Total Revenue", [Link]=months,
xlab="month", ylab="Revenue",
col=c("pink","orange","black","yellow","brown","green"))
legend("topright", region, cex =.6, fill=c("gold","blue","lightblue"))
#Excel
#simple
#read data
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet= "Sheet1")
#count occurrences of each education level
gender_count <- table(data_df$Gender)
colors<- c("pink","purple")
#create a bar chart
bar_positions <- barplot(gender_count, col= colors,
main= "Gender Distribution",
xlab= "Gender Level", ylab= "count",
las= 2) #rotate x-axis lables for better visibility
text(bar_positions, gender_count, labels = gender_count,
pos = 1, cex = 1.0, col="black")
#number
#read data
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
#count occurrences of each gender
gender_count <- table(data_df$Gender)
colors<- c("pink", "purple")
#create a bar chart
bar_positions<- barplot(gender_count, col = colors,
main = "Gender Distribution",
xlab = "Gender Level", ylab = "count",
las = 2, ylim = c(0, max(gender_count) + 20))
text(bar_positions, gender_count, labels = gender_count,
pos = 1, cex = 1.0, col="black")
#Histogram
v<- c(12,24,16,38,21,13,5,17,39,10,60,59,58)
hist(v,xlab="weight", ylab="frequency", col="green", border="red")
#use of xlim and ylim parameter for limit
v<- c(12,24,16,38,21,13,5,17,39,10,60,59,58)
hist(v,xlab="weight", ylab="frequency", col="green", border="red",
xlim=c(0,40), ylim=c(0,3), breaks=4)
#Excel
#simple
#read the data
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
#create histogram using ggplots
ggplot(data_df, aes(x = EU1)) +
geom_histogram(binwidth = 5, fill = "blue", color = "black", alpha = 0.7) +
labs(title = "Histogram of EU1",x = "EU1", y = "frequency") + theme_minimal()
ERROR In this code
#with 5 variable
#read the data
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
#convert data to long format for ggplot
data_long <- data_df %>% pivot_longer(cols = c(EU1,EU2,EU3,EU4,EU5),
names_to = "Variable", values_to = "value")
#create multiple histogram in one plot
ggplot(data_long, aes(x = value, fill = variable)) +
geom_histogram(binwidth = 5, color="black", alpha = 0.6) +
facet_wrap(~variable, scale = "free") + #separate plots for each variable
labs(title = "Histograms of EU1,EU2,EU3,EU4, and EU5", x = "value",
y = "frequency") + theme_minimal()
#line graph
v<-c(18,22,28,7,31,52)
plot(v)
v<-c(18,22,28,7,31,52)
plot(v, type="p")
plot(v,type = "p", col = "red", xlab = "month", ylab = "Tempreature")
v<-c(18,22,28,7,31,52)
plot(v, type="l")
plot(v,type = "l", col = "green", xlab = "month", ylab = "Tempreature")
v<-c(18,22,28,7,31,52)
plot(v, type="o")
plot(v,type = "o", col = "blue", xlab = "month", ylab = "Tempreature")
#with different colour circle
#plot with blue line, no points
v<-c(18,22,28,7,31,52)
v
plot(v, type = "o", col="blue", xlab = "month", ylab = "Temperature", pch = NA)
#Add red points
points(v, col = "red", pch = 16) #16 is a solid circle point
#line charts containing multiple lines
v<-c(13,22,28,7,31)
w<-c(11,13,32,6,35)
x<-c(12,22,15,34,35)
plot(v,type = "o", col = "blue", xlab = "month", ylab = "Temperature")
lines(w,type = "o", col = "red")
lines(x,type = "o",col = "green")
#excel
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
plot(data_df$EU1, type = "o", col = "navyblue", xlab = "Index",
ylab = "EU2", main = "line graph of EU1")
Home work output
> #numeric
> x=10.5
> print(x)
[1] 10.5
> class(x)
[1] "numeric"
> cat(print(x),class(x))
[1] 10.5
10.5 numeric
> x1=25.60
> print(x1)
[1] 25.6
> class(x1)
[1] "numeric"
> cat(print(x1), class(x1))
[1] 25.6
25.6 numeric
> x2=28.9
> print(x2)
[1] 28.9
> class(x2)
[1] "numeric"
> cat(print(x2), class(x2))
[1] 28.9
28.9 numeric
> x3=23.5
> print(x3)
[1] 23.5
> class(x3)
[1] "numeric"
> cat(print(x3), class(x3))
[1] 23.5
23.5 numeric
> x4=123.6
> print(x4)
[1] 123.6
> class(x4)
[1] "numeric"
> cat(print(x4), class(x4))
[1] 123.6
123.6 numeric
> x5=9.8
> print(x5)
[1] 9.8
> class(x5)
[1] "numeric"
> cat(print(x5),class(x5))
[1] 9.8
9.8 numeric
> x6=58.67
> print(x6)
[1] 58.67
> class(x6)
[1] "numeric"
> cat(print(x6), class(x6))
[1] 58.67
58.67 numeric
> x7=123.5
> print(x7)
[1] 123.5
> class(x7)
[1] "numeric"
> cat(print(x7), class(x7))
[1] 123.5
123.5 numeric
> x8=147.67
> print(x8)
[1] 147.67
> class(x8)
[1] "numeric"
> cat(print(x8), class(x8))
[1] 147.67
147.67 numeric
> x9=146.688
> print(x9)
[1] 146.688
> class(x9)
[1] "numeric"
> cat(print(x9), class(x9))
[1] 146.688
146.688 numeric
> #Integer
> i1=11L
> print(i1)
[1] 11
> class(i1)
[1] "integer"
> cat(print(i1), class(i1))
[1] 11
11 integer
> i2=0L
> print(i2)
[1] 0
> class(i2)
[1] "integer"
> cat(print(i2), class(i2))
[1] 0
0 integer
> i3=15L
> print(i3)
[1] 15
> class(i3)
[1] "integer"
> cat(print(i3), class(i3))
[1] 15
15 integer
> i4= -17L
> print(i4)
[1] -17
> class(i4)
[1] "integer"
> cat(print(i4), class(i4))
[1] -17
-17 integer
> i5= -123L
> print(i5)
[1] -123
> class(i5)
[1] "integer"
> cat(print(i5), class(i5))
[1] -123
-123 integer
> i6=67L
> print(i6)
[1] 67
> class(i6)
[1] "integer"
> cat(print(i6), class(i6))
[1] 67
67 integer
> i7= -65L
> print(i7)
[1] -65
> class(i7)
[1] "integer"
> cat(print(i7), class(i7))
[1] -65
-65 integer
> i8=25L
> print(i8)
[1] 25
> class(i8)
[1] "integer"
> cat(print(i8), class(i8))
[1] 25
25 integer
> i9=26L
> print(i9)
[1] 26
> class(i9)
[1] "integer"
> cat(print(i9), class(i9))
[1] 26
26 integer
> i10=98L
> print(i10)
[1] 98
> class(i10)
[1] "integer"
> cat(print(i10), class(i10))
[1] 98
98 integer
> #character
> char1="parth"
> print(char1)
[1] "parth"
> cat(char1,class(char1))
parth character
> char2="Hello R Programming"
> print(char2)
[1] "Hello R Programming"
> class(char2)
[1] "character"
> cat(char2, class(char2))
Hello R Programming character
> char3="Data Science"
> print(char3)
[1] "Data Science"
> class(char3)
[1] "character"
> cat(char3, class(char3))
Data Science character
> char4="Machine Learning"
> print(char4)
[1] "Machine Learning"
> class(char4)
[1] "character"
> cat(char4, class(char4))
Machine Learning character
> char5="Data Types"
> print(char5)
[1] "Data Types"
> class(char5)
[1] "character"
> cat(char5, class(char5))
Data Types character
> char6="Variables"
> print(char6)
[1] "Variables"
> class(char6)
[1] "character"
> cat(char6, class(char6))
Variables character
> char7="Sensitive Language"
> print(char7)
[1] "Sensitive Language"
> class(char7)
[1] "character"
> cat(char7, class(char7))
Sensitive Language character
> char8="Fundamental"
> print(char8)
[1] "Fundamental"
> class(char8)
[1] "character"
> cat(char8,class(char8))
Fundamental character
> char9="Combinations"
> print(char9)
[1] "Combinations"
> class(char9)
[1] "character"
> cat(char9, class(char9))
Combinations character
> char10="Data Frames"
> print(char10)
[1] "Data Frames"
> class(char10)
[1] "character"
> cat(char10, class(char10))
Data Frames character
> #logical
> log1=TRUE
> print(log1)
[1] TRUE
> class(log1)
[1] "logical"
> cat(log1,class(log1))
TRUE logical
> x=c(FALSE, FALSE, TRUE, FALSE)
> y=c(TRUE, TRUE, TRUE, FALSE)
> x[2] & y[2]
[1] FALSE
> # Logical &&
> x = c(TRUE, FALSE, TRUE)
> y = c(TRUE, FALSE, FALSE)
> result = x[2] && y[2]
> print(result)
[1] FALSE
> #Logical !
> x = TRUE
> !x
[1] FALSE
> #Logical | ||
> x = c(TRUE, FALSE, TRUE, TRUE, FALSE)
> y = c(FALSE, TRUE, FALSE, TRUE, FALSE)
> x|y
[1] TRUE TRUE TRUE TRUE FALSE
> x = c(TRUE, FALSE, TRUE)
> y = c(FALSE, TRUE, FALSE)
> x[1] || y[1]
[1] TRUE
# Complex
> z=10+6i
> print(z)
[1] 10+6i
> class(z)
[1] "complex"
> typeof(z)
[1] "complex"
raw_data= charToRaw("Charusat")
> print(raw_data)
[1] 43 68 61 72 75 73 61 74
> class(raw_data)
[1] "raw"
> typeof(raw_data)
[1] "raw"
> raw_data1= charToRaw("Indukaka Ipcowala Institute of Management")
> print(raw_data1)
[1] 49 6e 64 75 6b 61 6b 61 20 49 70 63 6f 77 61 6c 61 20 49 6e 73 74 69 74 75 74 65 20 6f 66
[31] 20 4d 61 6e 61 67 65 6d 65 6e 74
> class(raw_data1)
[1] "raw"
> typeof(raw_data1)
[1] "raw"
> num = 123
> raw_data = charToRaw([Link](num))
> raw_data
[1] 31 32 33
#converting data type in R
> #Numeric to character
> # Code 1
> x <- 100
> [Link](x)
[1] "100"
> # Code 2
> x <- 25.5
> y <- [Link](x)
> print(y)
[1] "25.5"
> # Code 3
> num_vec <- c(1, 2, 3, 4)
> char_vec <- [Link](num_vec)
> # Code 4
> num <- 999
> typeof([Link](num))
[1] "character"
> # Code 5
> a <- 45
> b <- paste(a)
> class(b)
[1] "character"
> #Character to numeric
> # Code 1
> x <- "123"
> [Link](x)
[1] 123
> # Code 2
> y <- c("10", "20", "30")
> [Link](y)
[1] 10 20 30
> # Code 3
> num <- [Link]("45.67")
> print(num)
[1] 45.67
> # Code 4
> z <- "1000"
> typeof([Link](z))
[1] "double"
> # Code 5
> a <- "500"
> b <- [Link](a)
> class(b)
[1] "numeric"
> #numeric ("i is a imaginary number")
> num1<- [Link](50L)
> num1
[1] 50
> num2<- [Link](50-86i)
Warning message:
imaginary parts discarded in coercion
> num2
[1] 50
> num3<- [Link](FALSE)
> num3
[1] 0
> num4<- [Link]("abcde4789")
Warning message:
NAs introduced by coercion
> num4
[1] NA
> num5<- [Link]("123456789")
> num5
[1] 123456789
#integer
> int1<- [Link](80.5976)
> int1
[1] 80
> int2<- [Link](85-96i)
Warning message:
imaginary parts discarded in coercion
> int2
[1] 85
> int3<- [Link](FALSE)
> int3
[1] 0
> int4<- [Link]("123456789abcd")
Warning message:
NAs introduced by coercion
> int4
[1] NA
#complex
> com1<- [Link](892.4556)
> com1
[1] 892.4556+0i
> com2<- [Link](85L)
> com2
[1] 85+0i
> com3<- [Link](FALSE)
> com3
[1] 0+0i
> com4<- [Link]("123456789abcd")
Warning message:
NAs introduced by coercion
> com4
[1] NA
> #logical
> log1<- [Link](88.78i)
> log1
[1] TRUE
> log2<- [Link](0-8i)
> log2
[1] TRUE
> log3<- [Link](0)
> log3
[1] FALSE
> log4<- [Link]("123456789")
> log4
[1] NA
> #character
> char1<- [Link](88L)
> char1
[1] "88"
> char2<- [Link](82-99i)
> char2
[1] "82-99i"
> char3<- [Link](TRUE)
> char3
[1] "TRUE"
> rawToChar([Link](c(0x43, 0x68, 0x61, 0x72, 0x75, 0x73, 0x61, 0x74)))
[1] "Charusat"
> #data types=vectors, list, matrices, data frames, factors,
> ##element of vector are known as components
> #length(): no. of element in vector
> # Code 1
> v1 <- c(10, 20, 30, 40, 50)
> print(v1)
[1] 10 20 30 40 50
> length(v1)
[1] 5
> class(v1)
[1] "numeric"
> typeof(v1)
[1] "double"
> #logical vector
> # Code 1
> v3 <- c(TRUE, FALSE, TRUE, TRUE)
> print(v3)
[1] TRUE FALSE TRUE TRUE
> length(v3)
[1] 4
> class(v3)
[1] "logical"
> typeof(v3)
[1] "logical"
> #Sequence Functions to Create Vector
> v4 <- 1:10
> print(v4)
[1] 1 2 3 4 5 6 7 8 9 10
> v5 <- seq(1, 20, by = 2)
> length(v5)
[1] 10
> class(v5)
[1] "numeric"
> vec <-c(20, 30, 40, 50)
> print(vec)
[1] 20 30 40 50
> class(vec)
[1] "numeric"
> a<-c(8, 4, 7, 2, 1, 9)
>a
[1] 8 4 7 2 1 9
> b<--5:8
>b
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
> sq<-seq(2,5)
> sq
[1] 2 3 4 5
> sq<-seq(2,5, by=.5)
> sq
[1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0
> sq<-seq(2,5, by=.4)
> sq
[1] 2.0 2.4 2.8 3.2 3.6 4.0 4.4 4.8
> sq<-seq(2,5,[Link]= 5)
> sq
[1] 2.00 2.75 3.50 4.25 5.00
> sq<-seq(5,100, [Link]= 5)
> sq
[1] 5.00 28.75 52.50 76.25 100.00
> sq[4]
[1] 76.25
> sq[1]
[1] 5
> numv<-c(10,13, 57.6, 73,75, 89, 145)
> numv
[1] 10.0 13.0 57.6 73.0 75.0 89.0 145.0
> class(numv)
[1] "numeric"
> #integer vector
> intv<-c(8, 7, 6, 5, 9, 2)
> intv<- [Link](intv)
> class(intv)
[1] "integer"
> intv<-c(7L, 6L, 5L, 8L, 9L, 5L)
> intv
[1] 7 6 5 8 9 5
> class(intv)
[1] "integer"
> charv<-c(7, 5, 2, 4, 9)
> charv<- [Link](charv)
> class(charv)
[1] "integer"
> #character vector
> charvv<-c("Abhay", "Shlok", "Akash", "Deep")
> charvv
[1] "Abhay" "Shlok" "Akash" "Deep"
> class(charvv)
[1] "character"
> #logical vector
> #Accessing element of vector
> #by indexing[]
> #indexing start from1 not 0
> char_vec<-c("Krishna"=32, "trusharkant"=12, "mistry"=11)
> char_vec
Krishna trusharkant mistry
32 12 11
> char_vec["Krishna"]
Krishna
32
> #vector operations
> a1<-c(1, 2, 3, 4, 5, 6, 7)
> a2<-c("Krishan", "trusharkant", "mistry")
> a3<-c(a1,a2)
> a3
[1] "1" "2" "3" "4" "5" "6"
[7] "7" "Krishan" "trusharkant" "mistry"
> a1<-c(1, 2, 3, 4, 5, 6, 7, 8, 9,10)
> a4<-c(5, 6, 7, 8, 1, 2, 4, 3, 2, 1)
> a1+a4
[1] 6 8 10 12 6 8 11 11 11 11
> a1-a4
[1] -4 -4 -4 -4 4 4 3 5 7 9
> a1*a4
[1] 5 12 21 32 5 12 28 24 18 10
> a1/a4
[1] 0.2000000 0.3333333 0.4285714 0.5000000 5.0000000 3.0000000 1.7500000 2.6666667
[9] 4.5000000 10.0000000
> a2[4]
[1] NA
> a2[5]
[1] NA
> a2[-5]
[1] "Krishan" "trusharkant" "mistry"
> a2[1:6]
[1] "Krishan" "trusharkant" "mistry" NA NA NA
> #List
> lst <- list(30, "R language", FALSE)
> print(lst)
[[1]]
[1] 30
[[2]]
[1] "R language"
[[3]]
[1] FALSE
> #matrix
> mat <- matrix(1:8, nrow=2, ncol=2)
Warning message:
In matrix(1:8, nrow = 2, ncol = 2) :
data length differs from size of matrix: [8 != 2 x 2]
> #matrix
> mat <- matrix(1:6, nrow=2, ncol=2)
Warning message:
In matrix(1:6, nrow = 2, ncol = 2) :
data length differs from size of matrix: [6 != 2 x 2]
> print(mat)
[,1] [,2]
[1,] 1 3
[2,] 2 4
> matrix(1:12, ncol=4, nrow=3)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(7:18, ncol=4, nrow=3)
[,1] [,2] [,3] [,4]
[1,] 7 10 13 16
[2,] 8 11 14 17
[3,] 9 12 15 18
> x<-matrix(1:10, nrow=3, ncol=4, byrow= TRUE)
Warning message:
In matrix(1:10, nrow = 3, ncol = 4, byrow = TRUE) :
data length [10] is not a sub-multiple or multiple of the number of rows [3]
>x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 1 2
> y<-matrix(7:16, nrow=3, ncol=4, byrow= TRUE)
Warning message:
In matrix(7:16, nrow = 3, ncol = 4, byrow = TRUE) :
data length [10] is not a sub-multiple or multiple of the number of rows [3]
>y
[,1] [,2] [,3] [,4]
[1,] 7 8 9 10
[2,] 11 12 13 14
[3,] 15 16 7 8
> z=x+y
>z
[,1] [,2] [,3] [,4]
[1,] 8 10 12 14
[2,] 16 18 20 22
[3,] 24 26 8 10
> z=x*y
>z
[,1] [,2] [,3] [,4]
[1,] 7 16 27 40
[2,] 55 72 91 112
[3,] 135 160 7 16
> d=x/y
>d
[,1] [,2] [,3] [,4]
[1,] 0.1428571 0.250 0.3333333 0.4000000
[2,] 0.4545455 0.500 0.5384615 0.5714286
[3,] 0.6000000 0.625 0.1428571 0.2500000
> e=x-y
>e
[,1] [,2] [,3] [,4]
[1,] -6 -6 -6 -6
[2,] -6 -6 -6 -6
[3,] -6 -6 -6 -6
> #data frames
> df <- [Link](Name=c("Ajay", "Rahul"),
+ Age=c(35,40),
+ Score=c(80.5,95))
> print(df)
Name Age Score
1 Ajay 35 80.5
2 Rahul 40 95.0
> # Factor
> gender <- factor(c("Male", "Female", "Male", "Female"))
> gender
[1] Male Female Male Female
Levels: Female Male
> class(gender)
[1] "factor"
> # Arrays
> arr <- array(1:10, dim = c(3,3,3))
> arr
,,1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
,,2
[,1] [,2] [,3]
[1,] 10 3 6
[2,] 1 4 7
[3,] 2 5 8
,,3
[,1] [,2] [,3]
[1,] 9 2 5
[2,] 10 3 6
[3,] 1 4 7
> # Type Checking & Type Conversion
> # Operators in R
> a <- 8.5
> b <- 5
> a+b #Addition
[1] 13.5
> a-b # Subtraction
[1] 3.5
> a*b #Multiplication
[1] 42.5
> a/b #Division
[1] 1.7
> a%%b #Reminder
[1] 3.5
> a%/%b #Quotient
[1] 1
> a^b #Exponent
[1] 44370.53
> # Vectors Function (Collection of similar data types)
> c1 <- c(8,9,6,7,5)
> c2 <- c(2,4,5,3)
> c1 + c2
[1] 10 13 11 10 7
> c1 - c2
[1] 6 5 1 4 3
> c1 * c2
[1] 16 36 30 21 10
> c1 / c2
[1] 4.000000 2.250000 1.200000 2.333333 2.500000
> c1 %% c2
[1] 0 1 1 1 1
> c1 %/% c2
[1] 4 2 1 2 2
> c1 ^ c2
[1] 64 6561 7776 343 25
> # Relational
>a
[1] 8.5
>b
[1] 5
> a<b
[1] FALSE
> a>b
[1] TRUE
> a==b
[1] FALSE
> a<=b
[1] FALSE
> a>=b
[1] TRUE
> a!=b
[1] TRUE
> c1
[1] 8 9 6 7 5
> c2
[1] 2 4 5 3
> c1<c2
[1] FALSE FALSE FALSE FALSE FALSE
> c1>c2
[1] TRUE TRUE TRUE TRUE TRUE
> c1==c2
[1] FALSE FALSE FALSE FALSE FALSE
> c1<=c2
[1] FALSE FALSE FALSE FALSE FALSE
> c1>=c2
[1] TRUE TRUE TRUE TRUE TRUE
> c1!=c2
[1] TRUE TRUE TRUE TRUE TRUE
> # R if-else Statements
> x <- 55L
> if([Link](x)){
+ print("x is an integer number")
+}
[1] "x is an integer number"
> if([Link](x)){
+ print("x is an integer number")
+ }else{
+ print("x is not an integer number")
+}
[1] "x is an integer number"
> # R Switch Case
> # Switch (expression, case1, case2)
> x <- switch (2,
+ "Parth",
+ "Trusharkant",
+ "Mistry"
+)
>x
[1] "Trusharkant"
> x <- switch (3,
+ "Parth",
+ "Trusharkant",
+ "Mistry"
+)
>x
[1] "Mistry"
#data manepulation
##merging and joining data
# creating sample data frames
df1 <- [Link](ID=c(1,2,3,4), Name=c("A","B","C","D"))
df2<- [Link](ID=c(2,3,4,5), Score=c(85,90,78,88))
#Inner Join
df_inner <- merge(df1,df2,by="ID")
View(df_inner)
#left Join
df_left <- merge(df1,df2, by="ID", all.x = TRUE) #NA fill for missing value
View(df_left)
#Right join
df_right <- merge(df1,df2, by="ID", all.x = TRUE)
View(df_right)
#Full join
df_full <- merge(df1,df2, by="ID", all = TRUE)
View(df_full)
# Importing Data Set
# Exporting Data Set
# CSV File
library(readxl) # IMPORT
Parth <- read_excel("C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]")
View(Parth)
> library(writexl) # EXPORT
> employee <- c("John", "Emily", "Sophia", "Michael", "Sarah", "David", "Anna")
> age <- c(33, 54, 65, 42, 21, 38, 39)
> salary <- c(50000, 60000, 52000, 80000, 56000, 75000, 54000)
> department <-c("HR", "Finance", "Marketing", "IT", "HR", "Finance", "Marketing")
> experience <- c(3, 11, 5, 8, 5, 12, 4)
> df <- [Link](employee, age, salary, department, experience)
> df
employee age salary department experience
1 John 33 50000 HR 3
2 Emily 54 60000 Finance 11
3 Sophia 65 52000 Marketing 5
4 Michael 42 80000 IT 8
5 Sarah 21 56000 HR 5
6 David 38 75000 Finance 12
7 Anna 39 54000 Marketing 4
> # FOR JavaScript
> write_json(df, "employee_data.json", pretty = T)
> print(toJSON(df, pretty = T)) # Optional
"employee": "John",
"age": 33,
"salary": 50000,
"department": "HR",
"experience": 3
},
"employee": "Emily",
"age": 54,
"salary": 60000,
"department": "Finance",
"experience": 11
},
"employee": "Sophia",
"age": 65,
"salary": 52000,
"department": "Marketing",
"experience": 5
},
"employee": "Michael",
"age": 42,
"salary": 80000,
"department": "IT",
"experience": 8
},
"employee": "Sarah",
"age": 21,
"salary": 56000,
"department": "HR",
"experience": 5
},
"employee": "David",
"age": 38,
"salary": 75000,
"department": "Finance",
"experience": 12
},
"employee": "Anna",
"age": 39,
"salary": 54000,
"department": "Marketing",
"experience": 4
> # 1. Create numeric vector
> v1 <- c(5, 10, 15, 20)
> v2 <- c("Red", "Blue", "Green")
> v3 <- seq(1, 10, by = 2)
> v1[2]
[1] 10
> v1 * 2
[1] 10 20 30 40
> # 1. Create simple list
> list1 <- list(1, "Apple", TRUE)
> list2 <- list(Name = "John", Age = 25, Passed = TRUE)
> list2$Name
[1] "John"
> list3 <- list(A = c(1,2,3), B = list("X","Y"))
> list2$City <- "Mumbai
> # 1. Create matrix
> m1 <- matrix(c(1,2,3,4), nrow = 2)
> m2 <- matrix(1:9, nrow = 3, ncol = 3)
> m2[2,3]
[1] 8
> m2 + 5
[,1] [,2] [,3]
[1,] 6 9 12
[2,] 7 10 13
[3,] 8 11 14
> t(m2)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> df1 <- [Link](
+ ID = c(1,2,3,4),
+ Name = c("Alice","Bob","Charlie","David"),
+ Age = c(25,30,35,40)
+)
>
> df1
ID Name Age
1 1 Alice 25
2 2 Bob 30
3 3 Charlie 35
4 4 David 40
> df2 <- [Link](
+ Product = c("Laptop","Tablet","Phone","Monitor"),
+ Price = c(700,300,NA,200),
+ Stock = c(10,NA,15,20)
+)
> df2
Product Price Stock
1 Laptop 700 10
2 Tablet 300 NA
3 Phone NA 15
4 Monitor 200 20
> df3 <- [Link](
+ Student = c("John","Emma","Liam","Sophia"),
+ Score = c(85,90,78,88),
+ Grade = c("B","A","C","B")
+)
> df3
Student Score Grade
1 John 85 B
2 Emma 90 A
3 Liam 78 C
4 Sophia 88 B
df4 <- [Link](
+ EmpID = c(101,102,103),
+ EmpName = c("Rahul","Priya","Aman"),
+ Salary = c(50000,60000,55000)
+)
>
> df4
EmpID EmpName Salary
1 101 Rahul 50000
2 102 Priya 60000
3 103 Aman 55000
> df5 <- [Link](
+ BookID = c(1,2,3),
+ Title = c("Math","Science","English"),
+ Price = c(250,300,200)
+)
>
> df5
BookID Title Price
1 1 Math 250
2 2 Science 300
3 3 English 200
> #Factors
> # 1. Colors
> f1 <- factor(c("Red", "Blue", "Green", "Red", "Green", "Blue"))
> f1
[1] Red Blue Green Red Green Blue
Levels: Blue Green Red
> # 2. Education Levels
> f2 <- factor(c("High School", "Bachelor", "Master", "PhD", "Master"))
> f2
[1] High School Bachelor Master PhD Master
Levels: Bachelor High School Master PhD
> # 3. Job Positions
> f3 <- factor(c("Analyst", "Manager", "Executive", "Manager", "Analyst"))
> f3
[1] Analyst Manager Executive Manager Analyst
Levels: Analyst Executive Manager
> # 4. Numeric as Factor
> f4 <- factor(c(5, 3, 4, 2, 1, 4, 5))
> f4
[1] 5 3 4 2 1 4 5
Levels: 1 2 3 4 5
> # 5. Yes/No/Maybe
> f5 <- factor(c("Yes", "No", "Yes", "Yes", "No", "Maybe", "Yes", "Maybe"))
> f5
[1] Yes No Yes Yes No Maybe Yes Maybe
Levels: Maybe No Yes
> # 6. Performance Rating
> f6 <- factor(c("Good", "Bad", "Average", "Excellent", "Good", "Bad", "Excellent"))
> f6
[1] Good Bad Average Excellent Good Bad Excellent
Levels: Average Bad Excellent Good
> #ARITHMETIC OPERATORS
> # 1. 2:13
> arr1 <- array(2:13)
> arr1
[1] 2 3 4 5 6 7 8 9 10 11 12 13
>
> # 2. 20:40
> arr2 <- array(20:40)
> arr2
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
>
> # 3. 1:16 (4x4 array)
> arr3 <- array(1:16, dim = c(4,4))
> arr3
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
>
> # 4. 6:18
> arr4 <- array(6:18)
> arr4
[1] 6 7 8 9 10 11 12 13 14 15 16 17 18
>
> # 5. 7:24 (3x6 array)
> arr5 <- array(7:24, dim = c(3,6))
> arr5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 7 10 13 16 19 22
[2,] 8 11 14 17 20 23
[3,] 9 12 15 18 21 24
> #RELATIONAL OPERATORS
>a<b
[1] FALSE
>a>b
[1] FALSE
> a == b
[1] TRUE
> a <= b
[1] TRUE
> a >= b
[1] TRUE
> a != b
[1] FALSE
> a <- 3.2
> b <- 2
>a+b
[1] 5.2
>a>b
[1] TRUE
> # Arithmetic
>a+b
[1] 5.2
>a-b
[1] 1.2
>a*b
[1] 6.4
>a/b
[1] 1.6
> a %% b
[1] 1.2
> a %/% b
[1] 1
>a^b
[1] 10.24
> # Relational
>a<b
[1] FALSE
>a>b
[1] TRUE
> a == b
[1] FALSE
> a <= b
[1] FALSE
> a >= b
[1] TRUE
> a != b
[1] TRUE
> a <- 12
> b <- 2
> # Arithmetic
>a+b
[1] 14
>a-b
[1] 10
>a*b
[1] 24
>a/b
[1] 6
> a %% b
[1] 0
> a %/% b
[1] 6
>a^b
[1] 144
> # Relational
>a<b
[1] FALSE
>a>b
[1] TRUE
> a == b
[1] FALSE
> a <= b
[1] FALSE
> a >= b
[1] TRUE
> a != b
[1] TRUE
> a <- 9
> b <- 3
> # Arithmetic
>a+b
[1] 12
>a-b
[1] 6
>a*b
[1] 27
>a/b
[1] 3
> a %% b
[1] 0
> a %/% b
[1] 3
>a^b
[1] 729
> # Relational
>a<b
[1] FALSE
>a>b
[1] TRUE
> a == b
[1] FALSE
> a <= b
[1] FALSE
> a >= b
[1] TRUE
> a != b
[1] TRUE
> c1 <- c(8,8,6)
> c2 <- c(3,4,5)
> # Arithmetic
> c1 + c2
[1] 11 12 11
> c1 - c2
[1] 5 4 1
> c1 * c2
[1] 24 32 30
> c1 / c2
[1] 2.666667 2.000000 1.200000
> c1 %% c2
[1] 2 0 1
> c1 %/% c2
[1] 2 2 1
> c1 ^ c2
[1] 512 4096 7776
> # Relational
> c1 < c2
[1] FALSE FALSE FALSE
> c1 > c2
[1] TRUE TRUE TRUE
> c1 == c2
[1] FALSE FALSE FALSE
> c1 <= c2
[1] FALSE FALSE FALSE
> c1 >= c2
[1] TRUE TRUE TRUE
> c1 != c2
[1] TRUE TRUE TRUE
> c1 <- c(7,8,5)
> c2 <- c(2,2,4)
> c1 + c2
[1] 9 10 9
> c1 - c2
[1] 5 6 1
> c1 * c2
[1] 14 16 20
> c1 / c2
[1] 3.50 4.00 1.25
> c1 %% c2
[1] 1 0 1
> c1 %/% c2
[1] 3 4 1
> c1 ^ c2
[1] 49 64 625
> c1 < c2
[1] FALSE FALSE FALSE
> c1 > c2
[1] TRUE TRUE TRUE
> c1 == c2
[1] FALSE FALSE FALSE
> c1 <= c2
[1] FALSE FALSE FALSE
> c1 >= c2
[1] TRUE TRUE TRUE
> c1 != c2
[1] TRUE TRUE TRUE
> #if and else
> num <- 10
> if (num > 0) {
+ print("Positive Number")
+ } else {
+ print("Negative Number")
+}
[1] "Positive Number"
> num <- 7
> if (num %% 2 == 0) {
+ print("Even Number")
+ } else {
+ print("Odd Number")
+}
[1] "Odd Number"
> age <- 18
> if (age >= 18) {
+ print("Eligible to Vote")
+ } else {
+ print("Not Eligible")
+}
[1] "Eligible to Vote"
> a <- 12
> b <- 8
> if (a > b) {
+ print("a is greater")
+ } else if (a < b) {
+ print("b is greater")
+ } else {
+ print("Both are equal")
+}
[1] "a is greater"
> #switching data
> day <- 2
>
> result <- switch(day,
+ "Sunday",
+ "Monday",
+ "Tuesday",
+ "Wednesday",
+ "Invalid")
> print(result)
[1] "Monday"
>
> operation <- "+"
> a <- 10
> b <- 5
>
> result <- switch(operation,
+ "+" = a + b,
+ "-" = a - b,
+ "*" = a * b,
+ "/" = a / b,
+ "Invalid")
> print(result)
[1] 15
>
> grade <- "A"
>
> message <- switch(grade,
+ "A" = "Excellent",
+ "B" = "Good",
+ "C" = "Average",
+ "Poor")
> print(message)
[1] "Excellent"
> #data frame
> df2 <- [Link](
+ Product = c("Laptop","Tablet","Phone"),
+ Price = c(700,300,500),
+ Stock = c(10,5,15)
+)
> str(df2)
'[Link]': 3 obs. of 3 variables:
$ Product: chr "Laptop" "Tablet" "Phone"
$ Price : num 700 300 500
$ Stock : num 10 5 15
> summary(df2)
Product Price Stock
Length:3 Min. :300 Min. : 5.0
Class :character 1st Qu.:400 1st Qu.: 7.5
Mode :character Median :500 Median :10.0
Mean :500 Mean :10.0
3rd Qu.:600 3rd Qu.:12.5
Max. :700 Max. :15.0
> df3 <- [Link](
+ EmpID = c(101,102,103),
+ EmpName = c("Rahul","Priya","Aman"),
+ Salary = c(50000,60000,55000)
+)
> str(df3)
'[Link]': 3 obs. of 3 variables:
$ EmpID : num 101 102 103
$ EmpName: chr "Rahul" "Priya" "Aman"
$ Salary : num 50000 60000 55000
> summary(df3)
EmpID EmpName Salary
Min. :101.0 Length:3 Min. :50000
1st Qu.:101.5 Class :character 1st Qu.:52500
Median :102.0 Mode :character Median :55000
Mean :102.0 Mean :55000
3rd Qu.:102.5 3rd Qu.:57500
Max. :103.0 Max. :60000
#Module 2
#Module 2
#create data for the graph
#pie charts
x<-c(12,55,36,70)
lables<-c("US", "UK", "Singapore", "Indonesia")
pie(x,lables)
#pie chart (to write the head and colour fill)
x<-c(20,35,66,70)
lables<-c("Singapore", "UK", "Japan", "Indonesia")
pie(x,lables, main="country pie chart", col=rainbow(length(x)))
x<-c(12,35,56,75)
lables<-c("Singapore", "UK", "Japan", "Indonesia")
colors<-c("blue","green","red","orange")
pie(x,lables, main = "country pie chart",col = colors)
x<-c(12,35,56,75)
lables<-c("Singapore", "UK", "Japan", "Indonesia")
colors<-c("blue","green","red","orange")
pie(x,lables, main = "country pie chart",col = colors)
legend("topright", c("Singapore", "UK", "Japan", "Indonesia"), cex = 0.8, fill = colors)
#Dimensional pie chart
x <- c(20, 55, 76, 52)
lables<-c("Singapore", "UK", "Japan", "Indonesia")
pie_lables <- paste(round(100 * x/sum(x),2),"%")
pie(x, labels = pie_lables)
# labels and %
# Define Data
lables<-c("Singapore", "UK", "Japan", "Indonesia")
#Calculate Percentages
percent <- round(x/sum(x)*100,1)
labels_with_percent <- paste(labels, percent, "%")
#Define custom colors
colors <- c("pink", "green", "red", "orange")
#Create the pie chart with percentages
pie(x, labels = labels_with_percent, main = "Country Pie Chart", col = colors)
#Legend
legend("topright", labels_with_percent, cex = 1, fill = colors)
#Excel Sheet
#Simple
# Read the data from the Excel file
data_path <- "C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet = "Sheet1")
#count occurrences of each gender
gender_count <- table(data_df$Gender)
colors <- c("gold", 'orange')
#Create Pie Chart
pie(gender_count, labels = names(gender_count),
col = colors, main = "Gender Distribution")
#bar charts
H1<-c(42,88,76,23,41)
barplot(H1)
H2<-c(72,30,40,50,33,38)
M2<-c("feb","mar","apr","may","may","jun")
barplot(H2, [Link]=M2, xlab="month",
ylab="Revenue", col="yellow", main="Revenue Bar Chart",
border="red")
#Group Bar Chart & Stacked Bar Chart
months<-c("jan","feb","mar","apr","may")
region<-c("west","north","south","east")
values<-matrix(c(21,22,25,40,67,66,87,78,69,56,44,23,88,55,85), nrow=4,
ncol=5, byrow= TRUE)
barplot(values, main="Total Revenue", [Link]=months,
xlab="month", ylab="Revenue",
col=c("pink","orange","black","yellow","brown","green"))
legend("topright", region, cex =.6, fill=c("gold","blue","lightblue"))
#Excel
#simple
#read data
data_path <-"C:/Users/parth/OneDrive/Desktop/SEM-4/PBAR/[Link]"
data_df <- read_excel(data_path, sheet= "Sheet1")
#count occurrences of each education level
gender_count <- table(data_df$Gender)
colors<- c("pink","blue")
#create a bar chart
bar_positions <- barplot(gender_count, col= colors,
main= "Gender Distribution",
xlab= "Gender Level", ylab= "count",
las= 2) #rotate x-axis lables for better visibility
text(bar_positions, gender_count, labels = gender_count,
pos = 1, cex = 1.0, col="black")