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)