In R programming, there are three operators, which we can use to assign the values to the
variable.
We can use leftward, rightward, and equal_to operator for this purpose.
using = operator using “=” equal to or assignment operator
using <- operator leftward operator (“ <- ”)
using -> operator rightward operator “->”
There are two functions which are used to print the value of the variable i.e.,
print() and cat().
paste() to concatenate one or more variables
"\n" new line character
readLines() function in R Language reads text lines from an input file
as.integer() function in R Language is used to convert a character object to integer object.
1. Program to show Arithmetic Operations in R with integers
# Program to show arithmetic Operations in R with integers
#Arithmetic operators + - * / %% %/% ^
A <- 20 #value 20 assigned using leftward operator "<-" to variable A
B = 10 #value 10 assigned using EQUAL TO operator "=" to variable B
print(A+B) #Addition
print(A-B) #subtraction
print(A*B) #Multiplication
print(A/B) #division
print(A%%B) #Remainder
print(A%/%B) #Quotient
print(A^B) #power
Output:
source("D:/R PROGRAMMING/operators.R", echo=TRUE)
> # Program to show arithmetic Operations in R with integers
> #Arithmetic operators + - * / %% %/% ^
> A <- 20 #value 20 ass .... [TRUNCATED]
> B = 10 #value 10 assigned using EQUAL TO operator "=" to variable B
> print(A+B) #Addition
[1] 30
> print(A-B) #subtraction
[1] 10
> print(A*B) #Multiplication
[1] 200
> print(A/B) #division
[1] 2
> print(A%%B) #Remainder
[1] 0
> print(A%/%B) #Quotient
[1] 2
> print(A^B) #power
[1] 1.024e+13
2. Program to declare and initialize Variables in R
# Program to declare and initialize variables in R
#using = operator using “=” equal to or assignment operator
R = "Welcome to Learn R"
print(R)
# using <- operator leftward operator (“ <- ”)
R <- "R programming "
print(R)
#using -> operator rightward operator “->”
"Variables in R" -> R
print(R)
#paste() to concatenate one or more variables
A <- "Apple"
B <- "is Red Colour"
print(paste(A, B))
cat(paste(A, B))
A=B=C="HAI"
print(A)
cat(paste(A, B))
cat(A)
cat(B)
cat(C)
cat("\n")
#"\n" new line character
cat(A,"\n")
cat(B,"\n")
cat(C,"\n")
Output:
> source("D:/R PROGRAMMING/var.R", echo=TRUE)
> # Program to declare and initialize in R
> #using = operator using “=” equal to or assignment operator
> R = "Welcome to Learn R"
> print(R)
[1] "Welcome to Learn R"
> # using <- operator leftward operator (“ <- ”)
> R <- "R programming "
> print(R)
[1] "R programming "
> #using -> operator rightward operator “->”
> "Variables in R" -> R
> print(R)
[1] "Variables in R"
> #paste() to concatenate one or more variables
> A <- "Apple"
> B <- "is Red Colour"
> print(paste(A, B))
[1] "Apple is Red Colour"
> cat(paste(A, B))
Apple is Red Colour
> A=B=C="HAI"
> print(A)
[1] "HAI"
> cat(paste(A, B))
HAI HAI
> cat(A)
HAI
> cat(B)
HAI
> cat(C)
HAI
> cat("\n")
> #"\n" new line character
> cat(A,"\n")
HAI
> cat(B,"\n")
HAI
> cat(C,"\n")
HAI
3. To get the Input from a user; compute Total and Average of Four Numbers
# taking multiple inputs
# using braces
{
var1 = readline("Enter 1st number : ");
var2 = readline("Enter 2nd number : ");
var3 = readline("Enter 3rd number : ");
var4 = readline("Enter 4th number : ");
# converting each value
var1 = as.integer(var1);
var2 = as.integer(var2);
var3 = as.integer(var3);
var4 = as.integer(var4);
# print the sum of the 4 number
total=(var1 + var2 + var3 + var4);
print (paste("Total=", total))
average=((var1 + var2 + var3 + var4))/4;
print (paste("Average=", average))
}
Output:
> source("D:/R PROGRAMMING/total.R", echo=TRUE)
> # taking multiple inputs
> # using braces
>{
+ var1 = readline("Enter 1st number : ");
+ var2 = readline("Enter 2nd number : ");
+ var3 .... [TRUNCATED]
Enter 1st number : 67
Enter 2nd number : 89
Enter 3rd number : 66
Enter 4th number : 77
[1] "Total= 299"
[1] "Average= 74.75"
>
4. R Booleans (Comparison and Logical Operators)
# Boolean values in R can be TRUE or FALSE
# decl
are boolean
x <- TRUE
print(x)
print(class(x))
# declare boolean using single character
y <- F
print(y)
print(class(y))
# print & of TRUE and FALSE combinations
TRUE & TRUE
TRUE & FALSE
FALSE & TRUE
FALSE & FALSE
# Comparison between two or more variables
x <- 10
y <- 23
z <- 12
print(x<y & y>z)
# Comparison between two or more values
10 > 9
10 == 9
10 < 9
a <- 10
b <- 9
a>b
if (b > a) {
print ("b is greater than a")
} else {
print("b is not greater than a")
}
Output:
> source("D:/R PROGRAMMING/boolean.R", echo=TRUE)
> # Boolean values in R can be TRUE or FALSE
>
> # declare boolean
> x <- TRUE
> print(x)
[1] TRUE
> print(class(x))
[1] "logical"
> # declare boolean using single character
> y <- F
> print(y)
[1] FALSE
> print(class(y))
[1] "logical"
> # print & of TRUE and FALSE combinations
> TRUE & TRUE
[1] TRUE
> TRUE & FALSE
[1] FALSE
> FALSE & TRUE
[1] FALSE
> FALSE & FALSE
[1] FALSE
> # Comparison between two or more variables
>
> x <- 10
> y <- 23
> z <- 12
> print(x<y & y>z)
[1] TRUE
> # Comparison between two or more values
> 10 > 9
[1] TRUE
> 10 == 9
[1] FALSE
> 10 < 9
[1] FALSE
> a <- 10
> b <- 9
>a>b
[1] TRUE
> if (b > a) {
+ print ("b is greater than a")
+ } else {
+ print("b is not greater than a")
+}
[1] "b is not greater than a"
5. To implement the concept of Built-in Functions
a<- -6.899
b <- 9
c<- 6.3
d <- 1.53289
e <- 4
# prints absolute value of a
print(abs(a))
# prints the square root of b
print(sqrt(b))
#prints maximum of a,b,c
print(max(a,b,c))
#prints minimum of a,b,c
print(min(a,b,c))
print(max(29,3,5,-80))
print(min(34.54,5,4,-3,3))
# prints the nearest integer which is larger than or equal to c
print(ceiling(c))
# prints the least value integer of the number
print(floor(c))
# prints truncates the decimal values of d
print(trunc(d))
# round(number,digits) that rounds the number to the number of digits
print(round(d,digits=3))
print(round(7.8999, digits=1))
# prints natural log of e
print(log(e))
Output:
a <- "885854351"
b <- " Have a Nice Day"
c <- "Tamil Nadu"
d = "Hai!
how are
you ?"
print(substr(a,3,6))
print(strsplit(a, ""))
print(tolower(c))
print(toupper(c))
print(cat(d))
> a<- -6.899
>
> b <- 9
>
> c<- 6.3
>
> d <- 1.53289
>
> e <- 4
>
>
> # prints absolute value of a
> print(abs(a))
[1] 6.899
>
> # prints the square root of b
> print(sqrt(b))
[1] 3
>
> #prints maximum of a,b,c
> print(max(a,b,c))
[1] 9
>
> #prints minimum of a,b,c
> print(min(a,b,c))
[1] -6.899
>
>
> print(max(29,3,5,-80))
[1] 29
>
> print(min(34.54,5,4,-3,3))
[1] -3
>
> # prints the nearest integer which is larger than or equal to c
> print(ceiling(c))
[1] 7
>
> # prints the least value integer of the number
> print(floor(c))
[1] 6
>
> # prints truncates the decimal values of d
> print(trunc(d))
[1] 1
>
>
> # round(number,digits) that rounds the number to the number of digits
> print(round(d,digits=3))
[1] 1.533
>
> print(round(7.8999, digits=1))
[1] 7.9
>
> # prints natural log of e
> print(log(e))
[1] 1.386294
>
> a <- "885854351"
>
> b <- " Have a Nice Day"
>
> c <- "Tamil Nadu"
>
> d = "Hai!
+ how are
+ you ?"
>
>
>
>
> print(substr(a,3,6))
[1] "5854"
>
> print(strsplit(a, ""))
[[1]]
[1] "8" "8" "5" "8" "5" "4" "3" "5" "1"
>
> print(tolower(c))
[1] "tamil nadu"
>
> print(toupper(c))
[1] "TAMIL NADU"
>
> print(cat(d))
Hai!
how are
you ?NULL