KEMBAR78
R Porgramming Notes | PDF | Matrix (Mathematics) | Computer Programming
0% found this document useful (0 votes)
20 views20 pages

R Porgramming Notes

The document contains a series of R programming tasks and their corresponding outputs, including generating prime numbers, calculating GCD, creating Fibonacci sequences, and working with data frames and lists. It also covers various statistical concepts such as mean, variance, and standard deviation, as well as operations on arrays and matrices. Additionally, it includes examples of visualizations like bar charts and pie charts, alongside user input prompts for various functionalities.

Uploaded by

Subhajit Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views20 pages

R Porgramming Notes

The document contains a series of R programming tasks and their corresponding outputs, including generating prime numbers, calculating GCD, creating Fibonacci sequences, and working with data frames and lists. It also covers various statistical concepts such as mean, variance, and standard deviation, as well as operations on arrays and matrices. Additionally, it includes examples of visualizations like bar charts and pie charts, alongside user input prompts for various functionalities.

Uploaded by

Subhajit Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

R PORGRAMMING NOTES

1) Write a program in R to generate all prime numbers less than a given


number n.

Output:-
Enter a number: 10
Prime numbers less than 10 are:
[1] 2 3 5 7

2)Write a recursive function in R to calculate the GCD (Greatest Common


Divisor) of two numbers.

Output:-
Enter the first number: 6
Enter the second number: 8
The GCD of 6 and 8 is: 2

3)Write an R function to generate the Fibonacci sequence up to n terms.

Output:-
[1] 0 1 1 2 3 5 8 13 21 34
4)Write an R program to find the sum of squares of the first n natural
numbers.

Output:-
The sum of squares of the first 5 natural numbers is: 55
5)Write a program in R that takes a list of numbers as input and prints
whether each number is even or odd.

Output:-
4 is even.
7 is odd.
12 is even.
15 is odd.
0 is even.
3 is odd.
8 is even.
6)Write a recursive function in R to evaluate the series: X + X2 /2! + X3 /3! +
……………+Xn/n! ,for any given value of n.

Output:-
Enter the value of X: 2
Enter the value of n: 3
The sum of the series for X = 2 and n = 3 is: 5.333333
7) A program is required to be written that prompts the user to enter a year
and check whether it is a leap year or not.

Output:-
Enter a year: 2024
2024 is a leap year.
8)Show how xy can be obtained using function in R.

9)What is a vector? Write a simple program to create an vector with inputs


from the terminal.
In R, a sequence of elements which share the same data type is known as
vector. A vector supports logical, integer, double, character, complex, or raw
data type.

Output:-
Enter the number of elements in the vector: 5
Enter element 1 : 2
Enter element 2 : 3
Enter element 3 : 4
Enter element 4 : 5
Enter element 5 : 6
The created vector is:
[1] 2 3 4 5 6
10)Write a function by which you can write data of a data frame in a .csv
file.

Output:-
Data frame has been written to Crop_recommendation.csv
[1] "After write the csv file"

11)Write a function count in R to count occurrence of a particular character


in a string.
Output:-
The character n appears 2 times in the string.
12)Write a program to draw a straight line with five points in R.

Output:-

13)A program is required to be written that prompts the user to enter a


string and check whether it is a palindrome or not.
Output:-
Enter a string: madam
madam is a palindrome.
14)Show how ab can be obtained using recursive function in R.

Output:-
2 raised to the power of 3 is: 8
15)What is an array? Write a simple program to create an array with inputs
from the terminal.
Array is a data structure that can store data of the same type in a multi-
dimensional grid. Arrays are similar to vectors but allow for more complex,
multidimensional data storage.
Output:-
Enter element 1 : 2
Enter element 2 : 98
Enter element 3 : 68
Enter element 4 : 45
Enter element 5 : 10
The created array is:
[1] 2 98 68 45 10
16)Write a function count in R to count occurrence of a particular character
in a string.
As 11 no question.
17)Write a program in R to draw a stacked bar chart with a suitable legend.

Output:-
18)Write a program to draw a Scatter plot in R.

Output:-

19)Write a program to read a .csv file in R.

Output:-
Name Age Score
1 Alice 25 85
2 Bob 30 90
3 Ron 35 95
20) A program is required to be written that prompts the user to enter a
number and return the reverse of it.

Output:-
Enter a number: 9865
The reverse of 9865 is: 5689
21) Write a program in R to draw a pie chart with a suitable legend.

Output:-
22)Create a data frame Employee which will contain the following columns
Emp_id, Emp_ Name, Department and perform the following information.
i) Display the data frame.
ii) Add a new row and display
iii) Add a new column and display
iv) Modify a particular element and display.
v) Delete a particular row and display.

Output:-
Initial Data Frame:
Emp_id Emp_Name Department
1 101 Alice HR
2 102 Bob Finance
3 103 Ron IT
Data Frame after adding a new row:
Emp_id Emp_Name Department
1 101 Alice HR
2 102 Bob Finance
3 103 Ron IT
4 104 David Marketing
Data Frame after adding a new column (Salary):
Emp_id Emp_Name Department Salary
1 101 Alice HR 50000
2 102 Bob Finance 55000
3 103 Ron IT 60000
4 104 David Marketing 45000
Data Frame after modifying Bob's salary:
Emp_id Emp_Name Department Salary
1 101 Alice HR 50000
2 102 Bob Finance 60000
3 103 Ron IT 60000
4 104 David Marketing 45000
Data Frame after deleting Ron's row:
Emp_id Emp_Name Department Salary
1 101 Alice HR 50000
2 102 Bob Finance 60000
4 104 David Marketing 45000
23)Create a named list Vehicle which contains serial_no, type and colour of
vehicles and perform the following operation.
i) Display the entire list.
ii) Display only the colour of vehicles.
ii) Append some data in the list.
iv) Delete some data from the list.
v)Create another list which will contain price and brand_name of vehicles.
Then concatenate the list with the previous list. Display the concatenated
list.

Output:-
Entire Vehicle List:
$serial_no
[1] 101 102 103
$type
[1] "Car" "Truck" "Motorcycle"
$colour
[1] "Red" "Blue" "Green"
Colour of Vehicles:
[1] "Red" "Blue" "Green"

Vehicle List after appending new data:


$serial_no
[1] 101 102 103 104
$type
[1] "Car" "Truck" "Motorcycle" "Bus"
$colour
[1] "Red" "Blue" "Green" "Yellow"
Vehicle List after deleting the second vehicle (Truck):
$serial_no
[1] 101 103 104
$type
[1] "Car" "Motorcycle" "Bus"
$colour
[1] "Red" "Green" "Yellow"
23)Write a short note on any two of the following:
i) array in R
ii) matrix in R
iii) mean, variance and standard deviation in statistics.
iv) Operators in R
v) vector in R
vi) mean, median and mode in statistics.
Array in R :-
In R array is a data structure that can store data of the same type in a multi-
dimensional grid. Arrays are similar to vectors but allow for more complex,
multidimensional data storage. Arrays are used for the allocation of space at
contiguous memory locations. An R array can be created with the use
of array() the function. A list of elements is passed to the array() functions
along with the dimensions as required.
Martix in R :-
In R, a matrix is a collection of elements of the same data type (numeric,
character, or logical) arranged into a fixed number of rows and columns.
Since you are only working with rows and columns, a matrix is called two-
dimensional. A matrix is created with the help of the vector input to the
matrix function. On R matrices, we can perform addition, subtraction,
multiplication, and division operation.
mean, variance and standard deviation in statistics :-
The mean is the average of a set of numbers, calculated as the sum of all
values divided by the count of values. Formula is – Sum of all values /
Number of values. In R we use the mean() function.
numbers <- c(5, 10, 15, 20)
mean_value <- mean(numbers)
print(mean_value) # Output: 12.5

Variance measures the spread of a set of numbers around their mean. It


quantifies how much the data varies. In R we use the var() function.

numbers <- c(5, 10, 15, 20)


variance_value <- var(numbers)
print(variance_value) # Output: 41.66667

The standard deviation is the square root of the variance. It indicates how
much the values deviate, on average, from the mean. In R we use the sd()
function.

numbers <- c(5, 10, 15, 20)


sd_value <- sd(numbers)
print(sd_value) # Output: 6.454972
Operators in R :-
1. Arithmetic Operators:-
Arithmetic operators in R are used for basic mathematical computations.
They operate on numerical data (vectors, scalars) to perform addition,
subtraction, multiplication, division, and other arithmetic tasks. Some
arithmetic operators are there in R such as – addition operator, subtraction,
multiplication, division, modulo(%%) operator, Exponentiation(^), Integer
Division(%/%).
2. Logical Operators:-
Logical operators in R are used to perform logical operations on variables or
values, typically to evaluate conditions. They return logical values: TRUE or
FALSE. Some logical operators are there in R such as – Element-wise Logical
AND operator (&), Element-wise Logical OR operator (|), NOT operator (!),
Logical AND operator (&&), Logical OR operator (||).
3. Relational Operators:-
The Relational Operators in R carry out comparison operations between the
corresponding elements of the operands. Returns a boolean TRUE value if
the first operand satisfies the relation compared to the second. A TRUE value
is always considered to be greater than the FALSE. Some relational operators
are there in R such as – Less than (<), Less than equal to (<=), Greater than
(>), Greater than equal to (>=), Not equal to (!=)
4. Assignment Operators
Assignment Operators in R are used to assigning values to various data
objects in R. The objects may be integers, vectors, or functions. These values
are then stored by the assigned variable names. There are two kinds of
assignment operators: Left Assignment (<- or <<- or =), and Right
Assignment (-> or ->>).
5. Miscellaneous Operators
Miscellaneous Operator are the mixed operators in R that simulate the
printing of sequences and assignment of vectors, either left or right-handed.
%in% Operator
Checks if an element belongs to a list and returns a boolean value TRUE if the
value is present else FALSE.
%*% Operator
This operator is used to multiply a matrix with its transpose.
v) vector in R :-
In R, a sequence of elements which share the same data type is known as
vector. A vector supports logical, integer, double, character, complex, or raw
data type.
vi) mean, median and mode in statistics.
24)Write a R program to calculate factorial of a number.

Output:-
Enter a number to calculate its factorial: 6
The factorial of 6 is: 720
25) A program is required to be written that prompts the user to enter a
year and check whether it is a leap year or not.
As previous.
26) A program is required to be written that prompts the user to enter a
string and return the reverse of the string.
As previous.
27) Write a program in R to draw a grouped bar chart with a suitable
legend.
Output:-

28) (i) A program is required to be written that prompts the user to enter a
number and return the sum of digits.

Output:-
Enter a number: 42
The sum of the digits of 42 is: 6

29) (ii) Write a program in R to draw a pie chart with a suitable legend.
As previous
30) Create a data frame Employee which will contain the following columns
Emp_id, Emp_ Name, Department and perform the following information.
i) Display the data frame.
ii) Add a new row and display
iii) Add a new column and display
iv) Modify a particular element and display.
v) Delete a particular row and display.
As previous 22 no question.
31) Create a list Student which contains roll, name and address of students
and perform the following operation.
i) Display the entire list.
ii) Display only the name of students.
ii) Append some data in the list.
iv) Delete some data from the list.
v) Create another list which will contain marks and gender of students.
Then concatenate the list with the previous list. Display the concatenated
list.

Output:-
Entire Student List:
$roll
[1] 101 102 103
$name
[1] "Alice" "Bob" "Ron"
$address
[1] "New York" "Los Angeles" "Chicago"
Names of Students:
[1] "Alice" "Bob" "Ron"

Student List After Appending Age Data:


$roll
[1] 101 102 103
$name
[1] "Alice" "Bob" "Ron"
$address
[1] "New York" "Los Angeles" "Chicago"
$age
[1] 20 21 22

Student List After Deleting Address Data:


$roll
[1] 101 102 103
$name
[1] "Alice" "Bob" "Ron"
$age
[1] 20 21 22

Concatenated Student List:


$roll
[1] 101 102 103
$name
[1] "Alice" "Bob" "Ron"
$age
[1] 20 21 22
$marks
[1] 85 90 88
$gender
[1] "Female" "Male" "Male"

You might also like