Posts

Showing posts from January, 2023

Data Frame

 Data Frame Code id <- c(101,102,103) name <- c('John','Jonny','Jack') marks <- c(78.25,59.45,63.85) students <- data.frame(id,name,marks) students studentslist <- list(id,name,marks) studentslist students[1,] students[1:2,] students[,2] students[1]  #Just refers to colomn students$id students$id[2] students[[3]][2] = 89.65  # First Index is always colomn students students[3,1] = 103.5 students[3,3] = 'No Marks' students[3,2] = 'Bob'      #Something about Factor students stud = students[-2,-3] stud report=subset(students,marks>60) report report=subset(students,marks>60 & id==101) report report=subset(students,marks>60,select=c(name)) report report=subset(students,marks>60,select=c(name,marks)) report report=subset(students,marks>60,select=name:marks) report report=subset(students,marks>60,select=-id) report report=subset(students,select=c(name,marks)) report id = c(101,102,103) name = c('John','Jon...

List

 x <- list(1, "a", TRUE) x  x <- vector("list", length = 3)  x rollno = c(101,102,103) snames = c('John','Jonny','Jack') Code marks = c(78.25,59.45,63.85) students = list(rollno,snames,marks) students students = list(c(101,102,103),c('John','Jonny','Jack'),c(78.25,59.45,63.85)) students id = c(101,'102',103) name = c('John','Jonny','Jack') marks = c(78.25,59.45,63.85) students = list(id, name, marks) students rollno = c(101,102,103) snames = c('John','Jonny','Jack') marks = c(78.25,59.45,63.85) students = list(rollno, snames, marks) students students[1] students[2] students[3] students[[1]] students[[2]] students[[3]] students[[1]][2] students[[2]][1] students[[3]][3] students[[3]] students[[3]][2] = 89.65 students[[3]] # $ subset operator, indexing students = list('ids'=rollno,'names'=snames,'score'=marks) print(students$ids) print(s...

Matrices

 Code: Matrices: m <- matrix(nrow=2,ncol=3) m dim(m) attributes(m) m <- matrix(c(1,2,3,4,5,6)) m dim(m) attributes(m) m <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3) m <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow = TRUE) m <- matrix(1:6) m <- matrix(1:6,nrow=2,ncol=3) m <- matrix(1:6,nrow=2,ncol=3,byrow = TRUE) m <- c(1,2,3,4,5,6) m dim(m) <- c(2,3) m dim(m) <- c(3,2) m m <- matrix(c(2,3,4,0,1,2,-1,-2,-3,5,4,3),nrow=4,ncol=3,byrow = TRUE) m dim(m) nrow(m) ncol(m) prod(dim(m)) length(m) Using Diag Function  m <- matrix(4,3,3) m m <- diag(1,3,3) m m <- diag(5,3,3) m m <- diag(1:5) m Naming Row and Column  m <- matrix(c(2,3,4,0,1,2,-1,-2,-3,5,4,3),nrow=4,ncol=3,byrow = TRUE) m rownames(m) <- c(1,2,3,4) colnames(m) <- c('A','B','C') m m[,'A'] Indexing in Matrix  A <- matrix(c(2,3,4,0,1,2,-1,-2,-3,5,4,3),nrow=4,ncol=3,byrow = TRUE) print(A) A[1,2] A[3,1] A[,1] A[3,] A[nrow(A),] A[,ncol(A)] A[,-2] A[-3,] A[3,3...

Vector

 Code: Vectors x <- 10 typeof(x) x <- 10.25 typeof(x) x <- 'Hello' typeof(x) x <- "hello" typeof(x) x <- TRUE typeof(x) x <- c(10, 20, 30, 40, 50) x typeof(x) assign('y', c(10, 20, 30, 40, 50)) y length(x) x <- c(10.25,3.5,8.75) x x <- c('a','h','j') x x <- c('aaa','bbb','ccc') x x <- c("a","h","j") x x <- c(TRUE,TRUE,FALSE,TRUE) x x <- c(T,F,F) x x <- 3+4i x y <- c(1,2,3,4,5,6,7,8,9,10) y x <- 1:10  # : Sequence operator x x <- c(10, 20, 30, 40, 50) x y <- c(60.5,70.6,80) y x = c(x,y) x y <- c('aaa','bbb') y x = c(x,y) x z <- y <- x <- c(10,30,50) x y z x <- vector() x x <- vector('numeric', length=5) x x <- vector('integer', length=5) x x <- vector('logical', length=5) x x <- vector('character', length=5) x x <- vector('complex', length=5) x x ...

Condition, Loops and functions

 Code: #If Else: x = 0 if (x >= 0) {   print('Positive Number') } if (x >= 0) {   print('Positive Number') } else {   print('Negative Number') } if (x > 0) {   print('Positive Number') } else if (x < 0) {   print('Negative Number') } else{   print('Zero Number') } x <- -5 ifelse(x>=0,'Positive Number','Negative Number') x <- c(10,45,30,50,35,40,80,25) ifelse(x%%2==0,1,0) For Loop for(i in 1:5)   print(i) for(i in 1:5) {   print(i) } #for(i in 1:5)  #  i  #doesnt work should use print x <- 1:5 for(i in seq_along(x))   print(x[i]) for(i in x) {   print(i)   #prints every element of the list } for(i in x) print(i)   #prints every element of the list x <- 'Hello' for(i in x)   print(i) While Loop i <- 0 while(i<5){   print(i)   i <- i + 1 } Repeat Loop  i <- 0 repeat {   print(i)   if (i>=5)     break   i <- i + 1...

1 Basic of R Program & Basic Mathematical Operations

Code: print('Hello R Program') x <- 10 y <- 20 z <- x + y z print(z) 18 + 4 18 - 4 18 * 4 4 - 18 4.5 + 2 4.5 - 2 4.5 * 2 10 / 2 4.5 / 3 2 ** 3  # Exponential 2 ^ 3   # Exponential 3.5 ** 2 3.5 ^ 2 10.0 ** 2 10.0 ^ 2 3.0 ** 3 3.0 ^ 3 2 ** 1.5 2 ^ 1.5 9 ** 0.5 9 ^ 0.5 9 ^ .5 9 ^ 1/2 9 ^ (1/2) sqrt(9) # operators with in %% are special operators  8 / 3 8 %/% 3     # Integer Division -8 %/% 3    # Integer Division 8 %/% -3    # Integer Division 14 %% 4     # Modulus  14.75 %% 4  # Modulus  10 %% 3.5   # Modulus  TRUE + TRUE TRUE + FALSE TRUE * TRUE TRUE * FALSE # Not Possible 'ghg' + 3 '5' + 3 3 + '5' 'hk' * 3 Data Type and variable  age = 24 age <- 24 marks <- 78 marks1 <- 78 subject_java <- 85 subject.java <- 85 subject_1 <- 75 subject.1 <- 75 #1subject <- 75 #_subject <- 75 #$subject <- 75 x = 10 typeof(x) mode(x) storage.mode(x) class(x) typeof(...