Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

R Programs I Mba II Sem

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

R PROGRAMS

1. R program to Add Two Vectors

>x
[1] 3 6 8
>y
[1] 2 9 0
>x+y
[1] 5 15 8
> x + 1 # 1 is recycled to (1,1,1)
[1] 4 7 9
> x + c(1,4) # (1,4) is recycled to (1,4,1) but warning issued
[1] 4 10 9
Warning message:
In x + c(1, 4) :
longer object length is not a multiple of shorter object length

As we can see above the two vectors x and y are of equal length so they can be added
together without difficulty.
The expression x + 1 also works fine because the single 1 is recycled into a vector of three
1's.
Similarly, in the last example, a two element vector is recycled into a three element
vector. But a warning is issued in this case as 3 is not an integral multiple of 2.

2. R program to Find Sum, Mean and Product of Vector


in R Programming
> sum(2,7,5)
[1] 14
>x
[1] 2 NA 3 1 4
> sum(x) # if any element is NA or NaN, result is NA or NaN
[1] NA
> sum(x, na.rm=TRUE) # this way we can ignore NA and NaN values
[1] 10
> mean(x, na.rm=TRUE)
[1] 2.5
> prod(x, na.rm=TRUE)
[1] 24

Whenever a vector contains NA (Not Available) or NaN (Not a Number), functions such
as sum(), mean(), prod() etc. produce NA or NaN respectively.

3. R Program to Find Minimum and Maximum

>x
[1] 5 8 3 9 2 7 4 6 10
> # find the minimum
> min(x)
[1] 2
> # find the maximum
> max(x)
[1] 10
> # find the range
> range(x)
[1] 2 10

If we want to find where the minimum or maximum is located, i.e. the index instead of
the actual value, then we can use which.min() and which.max() functions.
Note that these functions will return the index of the first minimum or maximum in case
multiple of them exists.

4. R Program to Find the Factorial of a Number

# take input from the user


num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
# check is the number is negative, positive or zero
if(num < 0) {
print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
print("The factorial of 0 is 1")
} else {
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}

Enter a number: 8
[1] "The factorial of 8 is 40320"

Here, we take input from the user and check if the number is negative, zero or positive
using if...else statement.
If the number is positive, we use for loop to calculate the factorial.

5. R Program to Sort a Vector

>x
[1] 7 1 8 3 2 6 5 2 2 4
> # sort in ascending order
> sort(x)
[1] 1 2 2 2 3 4 5 6 7 8
> # sort in descending order
> sort(x, decreasing=TRUE)
[1] 8 7 6 5 4 3 2 2 2 1
> # vector x remains unaffected
>x
[1] 7 1 8 3 2 6 5 2 2 4

> order(x)
[1] 2 5 8 9 4 10 7 6 1 3
> order(x, decreasing=TRUE)
[1] 3 1 6 7 10 4 5 8 9 2
> x[order(x)] # this will also sort x
[1] 1 2 2 2 3 4 5 6 7 8

6. R program to display Multiplication Table

# R Program to find the multiplicationtable (from 1 to 10)


# take input from the user
num = as.integer(readline(prompt = "Enter a number: "))
# use for loop to iterate 10 times
for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}

Output:

Enter a number: 7
[1] "7 x 1 = 7"
[1] "7 x 2 = 14"
[1] "7 x 3 = 21"
[1] "7 x 4 = 28"
[1] "7 x 5 = 35"
[1] "7 x 6 = 42"
[1] "7 x 7 = 49"
[1] "7 x 8 = 56"
[1] "7 x 9 = 63"
[1] "7 x 10 = 70"

7. R Program to Check if a Number is Odd or Even in R


Programming

# Program to check if the input number is odd or even.


# A number is even if division by 2 give a remainder of 0.
# If remainder is 1, it is odd.
num = as.integer(readline(prompt="Enter a number: "))
if((num %% 2) == 0) {
print(paste(num,"is Even"))
} else {
print(paste(num,"is Odd"))
}

Output:

Enter a number: 89
[1] "89 is Odd"

Enter a number: 24
[1] "24 is Even"

8. R Program to Print the Fibonacci Sequence

# take input from the user


nterms = as.integer(readline(prompt="How many terms? "))
# first two terms
n1 = 0
n2 = 1
count = 2
# check if the number of terms is valid
if(nterms <= 0) {
print("Plese enter a positive integer")
} else {
if(nterms == 1) {
print("Fibonacci sequence:")
print(n1)
} else {
print("Fibonacci sequence:")
print(n1)
print(n2)
while(count < nterms) {
nth = n1 + n2
print(nth)
# update values
n1 = n2
n2 = nth
count = count + 1
}
}
}

Output:

How many terms? 7


[1] "Fibonacci sequence:"
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8

Here, we ask the user for the number of terms in the sequence. We initialize the first term
to 0 and the seconde term to 1.
If the number of terms is more than 2, we use a while loop to find the next term in the
sequence.
Inside the while loop, we first print the first two terms n1 and n2 respectively. Then, we
calculate the next term nth by adding the last two terms and print it.
Now, we update the values of n1 and n2 to the last two terms, i.e. the term in n2 to n1 and
the term we just calculated nth to n2.
This goes on until the number of terms reaches the nterms entered by the user.
9. R Program to Check Prime Number

# Program to check if the input number is prime or not


# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
flag = 0
# prime numbers are greater than 1
if(num > 1) {
# check for factors
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
}
}
}
if(num == 2) flag = 1
if(flag == 1) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}

Output:

Enter a number: 25
[1] "25 is not a prime number"

Enter a number: 19
[1] "19 is a prime number"

10. R Program to check Armstrong Number

# take input from the user


num = as.integer(readline(prompt="Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp / 10)
}
# display the result
if(num == sum) {
print(paste(num, "is an Armstrong number"))
} else {
print(paste(num, "is not an Armstrong number"))
}

Output:

Enter a number: 23
[1] "23 is not an Armstrong number"

Enter a number: 370


[1] "370 is an Armstrong number"

Here, we ask the user for a number and check if it is an Armstrong number.
We need to calculate the sum of cube of each digit. So, we initialize the sum to 0 and
obtain each digit number by using the modulus operator %%.
Remainder of a number when it is divided by 10 is the last digit of the number.
We take the cubes using exponent operator. Finally, we compare the sum with the
original number and conclude it is an Armstrong number if they are equal.

You might also like