R Programs I Mba II Sem
R Programs I Mba II Sem
R Programs I Mba II Sem
>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.
Whenever a vector contains NA (Not Available) or NaN (Not a Number), functions such
as sum(), mean(), prod() etc. produce NA or NaN respectively.
>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.
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.
>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
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"
Output:
Enter a number: 89
[1] "89 is Odd"
Enter a number: 24
[1] "24 is Even"
Output:
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
Output:
Enter a number: 25
[1] "25 is not a prime number"
Enter a number: 19
[1] "19 is a prime number"
Output:
Enter a number: 23
[1] "23 is not 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.