Arithmetic Operators Operator Description Example - Subtraction 5 - 1 = 4 + Addition 5 + 1 = 6 * Multiplication 5 * 3 = 15 / Division 10 / 2 = 5 ^ or ** Exponentiation 2*2*2*2*2 as 2 to the power of 5 x%%y Modulus 5%%2 is 1 x%/%y Integer Division 5%/%2 is 2Relational and Logical Operators Operator Description Example < less than 5 < 10 <= less than or equal to <= 5 > greater than 10 > 5 >= greater than or equal to >= 10 == exactly equal to == 10 != not equal to != 5 !x not x x <- c(5), !x x | y x or y x <- c(5), y <- c(10), x | y x & y x and y x <- c(5), y <- c(10), x & y isTRUE(x) tests whether x is true x <- TRUE, isTRUE(x) [1] FALSE # Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE. v <- c(3,0,TRUE,2+2i) t <- c(1,3,TRUE,2+3i) print(v&&t) # Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE. v <- c(0,0,TRUE,2+2i) t <- c(0,3,TRUE,2+3i) print(v||t) # This operator is used to identify if an element belongs to a vector. v1 <- 8 v2 <- 12 t <- 1:10 print(v1 %in% t) print(v2 %in% t) num1=10 num2=20 if(num1<=num2){ print("Num1 is less or equal to Num2") } x <- 1:15 if (sample(x, 1) <= 10) { print("x is less than 10") } else { print("x is greater than 10") } x <- 1:15 ifelse(x <= 10, "x less than 10", "x greater than 10") if (10==10) print('10 is equal to 10') else print('invalid') x <- 3 y <- if(x>2){0}else{1} y z <- 1:10 ifelse(z<2 | z>8, 1, 0) ifelse(z>5,'TRUE','FALSE') z <- 1:10 ifelse(z>4&z>8,"x","?") x <- c("what","is","truth") if("Truth" %in% x) { print("Truth is found the first time") } else if ("truth" %in% x) { print("truth is found the second time") } else { print("No truth found") } vector <- c("aaa","bbb","ccc") for(i in vector){ print(i) } for (year in c(2010,2011,2012,2013,2014,2015)){ print(paste("The year is", year)) } for(i in 2:5){ z <- i +1 print(z) } x <- c('a', 'b', 'c', 'd') for (i in seq_along(x)){ print(x[i]) } #alternatively for (words in x){ print(words) } mymat <- matrix(1:9,3,3) mymat for (i in seq_len(nrow(mymat))){ for (j in seq_len(ncol(mymat))){ print(mymat[i,j]) } } x <- head(women,10) x mrnull <- NULL for(i in seq_along(x[,1])){ concat <- c(mrnull, mean(as.numeric(x[i, 1:2]))) print(concat) } # Create your three-dimensional array my_array <- array(1:20, dim=c(20, 20, 20)) for (i in 1:dim(my_array)[1]) { for (j in 1:dim(my_array)[2]) { for (k in 1:dim(my_array)[3]) { my_array[i,j,k] = i*j*k } } } # Show a 10x10x15 chunk of your array print(my_array[1:10, 1:10, 1:15]) i <- 1 while (i < 6) { print(i) i = i+1 } a <- 0 b <- 1 print(a) while (b < 50) { print(b) temp <- a + b a <- b b <- temp } a <- 0 b <- 1 print(a) while (b < 50) { print(b) temp <- a + b a <- b b <- temp } x <- 1 repeat { print(x) x = x+1 if (x == 6){ break } } x <- 1:5 for (val in x) { if (val == 3){ break } print(val) } x <- 1:10 for (num in x){ if (num==6) break mynum <- paste(num, "and so on. ", sep = " ") print(mynum) } x <- 1:5 for (val in x) { if (val == 3){ next } print(val) } m=20 odd=numeric() for (k in 1:m) { if (!k %% 2) next odd <- c(odd,k) } odd for (i in 1:10) { if (!i %% 2){ next } print(i) } x <- 1:10 for (num in x){ if (num %% 3 == 1) next mynum <- paste(num, "and so on. ", sep = " ") print(mynum) } switch(2,"red","green","blue") # [1] "green" switch(1,"red","green","blue") # [1] "red" ################ x <- "red" switch(x, red="cloth", size=5, name="table") ################# # If the numeric value is out of range (greater than the number of items in the list or smaller than 1), then, # NULL is returned. x <- switch(4,"red","green","blue") x x <- switch(0,"red","green","blue") x number1 <- 30 number2 <- 20 operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ") switch(operator, "+" = print(paste("Addition of two numbers is: ", number1 + number2)), "-" = print(paste("Subtraction of two numbers is: ", number1 - number2)), "*" = print(paste("Multiplication of two numbers is: ", number1 * number2)), "^" = print(paste("Exponent of two numbers is: ", number1 ^ number2)), "/" = print(paste("Division of two numbers is: ", number1 / number2)), "%/%" = print(paste("Integer Division of two numbers is: ", number1 %/% number2)), "%%" = print(paste("Division of two numbers is: ", number1 %% number2)) ) # let us take a vector vect <- c(1,2,3,4,5,6,7,9) # now we multiply each element of vect with 5 print(vect * 5) # now we add each element of vect with 5 print(vect + 5) # now we subtract each element of vect with 5 print(vect - 5) For example, to add pairs of numbers contained in two vectors first <- c(86, 42, 123, -45) second <- c(78, 210, 48, 52) first * 5 # with scalar second / 2.3 first + second # vector with another vector first * second first - second first/second a <- 1:10 b <- 1:10 res <- numeric(length = length(a)) for (i in seq_along(a)) { res[i] <- a[i] + b[i] } res res2 <- a + b res2 all.equal(res, res2) a <- 1:10 b <- 1:5 a + b a <- 1:10 b <- 5 a * b # here b is a vector of length 1 a <- 1:10 b <- 1:7 a + b df <- data.frame(x = 1:3, y = c("a", "b", "c")) df df1 <- cbind(df, 1) print(df1)