0% found this document useful (0 votes)
8 views16 pages

R Pro

Uploaded by

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

R Pro

Uploaded by

Dr M.Sathya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

EXPRESSIONS AND DATA STRUCTURES IN R

AIM:
Write a program to find the expressions and data structures in R.

ALGORITHM:
Step 1: Start the R program and display the message "Expressions".
This indicates the beginning of the program.
Step2: Assign values 1 and 4 to variables a and b.
These variables are used for the arithmetic calculation.
Step 3: Compute the expression a^2 + 2*a*b + b^2.
Store the result in variable c.
Step 4: Print the message for the result and display c.
This shows the calculated value of ((a+b)^2).
Step 5: Display the message "Vector Operations".
This indicates the start of vector-related tasks.
Step 6: Create a numeric vector x using c(1,2,3,4,5).
Display the elements of the vector.
Step 7: Create character vectors y and z.
Display both vectors to show their values.
Step 8: Stop the program.
RESULT:
Thus the program expressions and data structures in R executed
successfully.
2. MANIPULATION OF VECTORS AND MATRIX

AIM:
Write a R program for manipulation of vectors and matrix.

ALGORITHM:
Step 1: Start the R program and display "Creating Vectors".
This indicates the beginning of vector creation.
Step 2: Create numeric vectors a and b using c().
These vectors store the values to form matrices.
Step 3: Display the vectors a and b.
This shows the elements of each vector.
Step 4: Create matrix A from vector a using matrix() with 3 rows
and 3 columns.
byrow=TRUE fills the matrix row-wise.
Step 5: Create matrix B from vector b using matrix() with 3
rows and 3 columns.
byrow=3 also fills the matrix by rows.
Step 6: Display matrices A and B.
This shows the 3×3 matrices created from the vectors.
Step 7: Perform matrix addition C = A + B and assign row and column names.
Display C to show the sum of corresponding elements.
Step 8: Stop the R program
RESULT:
Thus the program manipulation of vectors and matrix R executed
successfully.
3. OPERATIONS ON FACTORS IN R
AIM:
Write a program to implement operations on factors in R.

ALGORITHM:
Step 1: Start the R program and display "Creating a Vector".
Create a character vector news containing directions like North,
South, East, and West.
Step 2: Display the vector news.
This shows all elements stored in the vector.
Step 3: Convert the vector news into a factor using factor().
Store it in variable a to represent categorical data.
Step 4: Display the factor a.
This shows the factor values and their levels.
Step 5: Check whether a is a factor using [Link]().
Print the result as TRUE or FALSE.
Step 6: Access single elements of the factor using indexing (a[4], a[7], a[10]).
This retrieves specific values from the factor.
Step 7: Access multiple elements using a[c(4,6,8)].
This retrieves multiple factor values at once.
Step 8: Stop the R program.
RESULT:
Thus the program operations on factors in R executed successfully.
4. DATA FRAMES IN R

AIM:
Write a program create the data frames in R.

ALGORITHM:
Step 1: Create vectors Name, Maths, and Science.
These vectors store data for students.
Step 2: Combine vectors into a data frame a using [Link]().
Display a to show the tabular data.
Step 3: Find the dimension of the data frame using dim(a).
This shows the number of rows and columns.
Step 4: Access specific columns by name (a["Name"], a["Maths"]).
This retrieves data for individual subjects.
Step 5: Add a new column Tamil using cbind().
Display the updated data frame b.
Step 6: Summarize data frames using summary(a) and summary(b).
This gives statistical details like mean, min, max, etc.
Step 7: Remove rows/columns using negative indexing and display.
This shows selected portions of the data frame.
Step 8: Bind new rows to data frame using rbind(c,a).
Display the combined data frame d.

Step 9: Stop the program.


RESULT:
Thus the program data frames in R executed successfully.
5. LISTS AND OPERATORS
AIM:
Write a program using list and operators in R.

ALGORITHM:
Step 1: Create a list mylist with some elements.
Display the list to show its contents.
Step 2: Access elements of the list using indexing (mylist[2], mylist[4]).
Retrieve specific items from the list.
Step 3: Change an item in the list (mylist[1] <- "Orange").
Display the updated list.
Step 4: Find the length of the list using length(mylist).
This shows the number of elements in the list.
Step 5: Check if an item exists using %in%.
Returns TRUE or FALSE.
Step 6: Add an item using append().
Display the updated list with the new item.
Step 7: Remove an item using negative indexing and store in newlist.
Display the modified list.
Step 8: Combine two lists using c().
Display the final combined list.

Step 9: Stop the program.


RESULT:
Thus the program list and operators in R executed successfully.
6. WORKING WITH LOOPING STATEMENTS

AIM:
Write a program using with looping statements in R.

ALGORITHM:
Step 1: Read a number num using readline().
This will be used to compute factorial.
Step 2: Initialize fact = 1 and compute factorial using a for loop.
Multiply each number from 1 to num to get factorial.
Step 3: Display the factorial using print().
Shows the result of the for loop calculation.
Step 4: Read another number num for sum calculation.
Initialize x=1 and s=0.
Step 5: Use a while loop to sum numbers from 1 to num-1.
Increment x in each iteration.
Step 6: Display the sum using print().
Shows the sum of the first N numbers.
Step 7: Stop the program.
RESULT:
Thus the program working with looping statements in R executed
successfully.
7. GRAPHS IN R

AIM:
Write a program to create graphs in R.

ALGORITHM:
Step 1: Create data for a pie chart (rain and labels).
Calculate percentages using round(100*rain/sum(rain),1).
Step 2: Plot a pie chart using pie() with colors and labels.
Add a legend using legend().
Step 3: Create data for a bar chart (v matrix, student names, and series labels).
Use barplot() to plot marks for students.
Step 4: Add legend to the bar chart using legend().
Shows which color corresponds to which series.

Step 5: Stop the program.


RESULT:
Thus the program graphs in R executed successfully.
8. 3D PLOTS IN R

AIM:
Write a program using 3D plots in R.

ALGORITHM:
Step 1: Define a function cone(x,y) to calculate height.
Create sequences x and y and calculate z = outer(x,y,cone).
Step 2: Plot 3D surface using persp() with labels, colors, and angles.
This generates a 3D cone plot.
Step 3: Install and load plotrix library for 3D pie chart.
Create data and labels for the chart.
Step 4: Plot 3D pie chart using pie3D() with colors, explode, and labels.
Add title and visualize chocolate sales in 3D.
Step 5: Stop the program.
RESULT:
Thus the program 3D plots in R executed successfully.

You might also like