Professional Documents
Culture Documents
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
Result: [1] FALSE TRUE FALSE FALSE
|(logical OR):
• It combines each element of the first vector with the corresponding element of
the second vector and gives a output TRUE if one the elements is TRUE.
Example,
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
[1] TRUE FALSE TRUE TRUE
!(Logical Not):
• It is called Logical NOT operator. Takes each element of the vector and gives the
opposite logical value.
Example,
v <- c(3,0,TRUE,2+2i)
print(!v)
Contd.. R-Operators
Assignment Operators
These operators are used to assign values to vectors.
• Called Left Assignment (<- or = <<-)
v1 <- c(3,1,TRUE,2+3i)
print(v1)
Contd.. R-Operators
• Miscellaneous Operators(: and %in%)
These operators are used to for specific purpose and not general mathematical or logical computation.
: (Colon Operator)
Colon operator. It creates the series of numbers in sequence for a vector.
v <- 2:8
print(v)
Result - [1] 2 3 4 5 6 7 8
%in%
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)
[1] TRUE
[1] FALSE
Demonstrate R program to take input
message from user.
my.name <- readline(prompt="Enter name: ")
my.age <- readline(prompt="Enter age: ")
print(paste("Hi,", my.name, "next year you will be", my.age+1, "years old."))
Output :
Enter name: Ram
Enter age: 17
[1] "Hi, Mary next year you will be 18 years old."
Declare Array in R programming
Arrays are the R data objects which can store data in more than two
dimensions.
• For example − If we create an array of dimension (2, 3, 1) then it
creates 1 rectangular matrices each with 2 rows and 3 columns.
Arrays can store only data type.
• An array is created using the array() function.
• It takes vectors as input and uses the values in the dim parameter to
create an array.
Declare Array in R programming – Example
R- Arrays:
, , Matrix2
These allow you to control the flow of execution of a script typically inside of a
function. Common ones include:
if, else
for
while
repeat
break
next
return
We don't use these while working with R interactively but rather inside functions.
If
Syntax:
if (condition) {
# do something
} else {
# do something else
}
Example:
> x <- 1.5
> if (x>1) {
+ print("x is greater than 1")
+ } else {
+ print("x is less than 1")}
[1] "x is greater than 1"
>
Other way of writing ifelse(x <= 10, "x less than 10", "x greater than 10")
for
• A for loop works on an iterable variable and assigns successive values till
the end of a sequence.
Example:1
for (i in 1:4) {
print(i)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
Contd..for
Example2:
x <- c("a", "b", "c", "d")
for (i in 1:4) {
print(x[i])
}
Output:
a
b
c
d
While
i <- 1
while (i < 5) {
print(i)
i <- i + 1
}
Output:
1
2
3
4
repeat
• The repeat loop is an infinite loop and used in association with a
break statement.
Example:
a=1
repeat {
print(a)
a = a+1
if(a > 4)
break
}
Output: 1 2 3 4
break
• A break statement is used in a loop to stop the iterations and flow the control outside
of the loop.
Example:
x = 1:10
for (i in x){
if (i == 2){
break
}
print(i)
}
Ans: 1
next statement
• Next statement enables to skip the current iteration of a loop without terminating it.
x = 1: 4
for (i in x) {
if (i == 2){
next
}
print(i)
}
Output:
[1] 1
[1] 3
[1] 4
Write R Program to find positive or negative number
# initialize sum
sum = 0
# initialize sum
sum = 0
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + digit
temp = floor(temp / 10)
}
# display the result
print(paste(sum, "is an Answer"))
Write R Program to Reverse Number
# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
reverse = 0
temp = num
while(temp > 0) {
digit = temp %% 10
reverse = reverse*10 + digit
temp = floor(temp / 10)
}
# display the result
print(paste(reverse, "is an Answer"))
Logic of Armstrong Number, Sum of Digits, Reverse Number
• Armstrong Number
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp / 10)
}
• Sum of Digits
while(temp > 0) {
digit = temp %% 10
sum = sum + digit
temp = floor(temp / 10)}
• Reverse Number
while(temp > 0) {
digit = temp %% 10
reverse = reverse*10 + digit
temp = floor(temp / 10)
}
Write R Program to Palindrome Number???
reverse = 0
temp = num
while(temp > 0) {
digit = temp %% 10
reverse = reverse*10 + digit
temp = floor(temp / 10)
}
# display the result
if(num = reverse)
{
print(paste(reverse, "it is Palindrome"))
}
else
{
print(paste(reverse, "it is not Palindrome"))
}
Functions in R
• A function is a set of statements organized together to perform a specific task.
• R has a large number of in-built functions and the user can create their own
functions.
• In-Built Functions and User Defined Functions
Function Components:
• Function Definition and Function Calling
• Function Name
• Arguments
• Function Body
• Return Value
Built In functions
• Simple examples of in-built functions are seq(), mean(), max(), sum(x)
and paste(...) etc
• # Create a sequence of numbers from 32 to 44.
print(seq(32,44))
Output : [1] 32 33 34 35 36 37 38 39 40 41 42 43 44
Output:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
#1 Calling a Function with Argument Values
Output:
[1] 18
[1] 45
#2 Calling a Function with Argument Values
# Create a function with arguments.
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
> barplot(table(age),
+ main="Age Count of 10 Students",
+ xlab="Age",
+ ylab="Count",
+ border="red",
+ col="blue",
+ density=10
+)
>