0% found this document useful (0 votes)
2 views3 pages

Sample Questions

The document contains a series of programming tasks and questions related to R programming, focusing on data structures, statistical analysis, and visualization techniques. It includes exercises on creating vectors, matrices, lists, data frames, arrays, and various types of plots using both base R and ggplot2. Each section provides specific coding requirements and examples to guide users in performing data manipulation and visualization tasks.

Uploaded by

mahek.chauhan
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)
2 views3 pages

Sample Questions

The document contains a series of programming tasks and questions related to R programming, focusing on data structures, statistical analysis, and visualization techniques. It includes exercises on creating vectors, matrices, lists, data frames, arrays, and various types of plots using both base R and ggplot2. Each section provides specific coding requirements and examples to guide users in performing data manipulation and visualization tasks.

Uploaded by

mahek.chauhan
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

Section A: Data Structures & Basic Programming

Q1. Working with Vectors

Write an R program to create a sequence of numbers from 5 to 15 using the colon operator.
Then, create a second vector using seq() to generate values from 2.2 to 8.2. Print both
vectors.

Q2. Matrix Creation and Indexing

Create a matrix named M with values from 2 to 13, having 3 rows, arranged by row (byrow =
TRUE). Write the code to access and print the element located at the 3rd row and 2nd
column.

Q3. Manipulating Lists

Create a list named student_data containing the elements: "Rahul", 95, and TRUE. Update
the second element (the marks) to 98 and print the updated list.

Q4. Data Frames and Summary

Write the R code to create a data frame named [Link] with the following columns:
emp_id (1 to 5), emp_name ("Rick", "Dan", "Michelle", "Ryan", "Gary"), and age (23, 21, 15,
11, 28). Display the statistical summary of this data frame using a built-in function.

Q5. Creating Arrays

Write a program to create an array named result taking two vectors as input. The array
should have dimensions (3, 3, 2). Print the resulting array.

Section B: Statistical Analysis & Visualization


Q6. Handling Missing Values in Statistics

Given the vector y <- c(13, 8, 4, NA, 17, NA, 43), write the R code to calculate the mean of
this vector while ignoring the missing (NA) values.

Q7. Base R Visualization: Bar Chart

Write the R code to draw a horizontal bar chart for the following temperature data:
Temperature <- c(7, 12, 28, 31, 40) and Months <- c("Jan", "Feb", "Mar", "Apr", "May").
Ensure the chart has the title "Temperature chart".

Q8. Base R Visualization: Histogram

Write a code to draw a histogram for the vector k <- c(9, 13, 21, 8, 36, 22, 12, 41, 31, 33,
19). The histogram should have "yellow" bars and a "blue" border.

Q9. Data Visualization with ggplot2


Assuming the ggplot2 library is loaded and the mtcars dataset is available, write the code to
create a scatter plot mapping wt to the x-axis and mpg to the y-axis. Use geom_point() for
the visualization.

Q10. Base R Visualization: Line Chart

Write the R code to plot a line chart for the vector Child_weight <- c(5, 8, 14, 20). The plot
should display both points and lines (type = "o"), and the y-axis label should be "Weight in
Kilograms".

Section C: Base R Visualization


Q1. Scatter Plot Axis Limits

Write the R code to draw a scatter plot using the plot() function for columns disp (x-axis) and
hp (y-axis) from the mtcars dataset. Explicitly set the x-axis limits to c(50, 500) and the y-axis
limits to c(40, 320).

Q2. Grouped Box Plot

Using the built-in airquality dataset, write the command to create a box plot that shows Temp
grouped by Month (i.e., Temp~Month). Label the x-axis as "Month Number" and the y-axis as
"Degree Fahrenheit".

Q3. Line Chart Types

When using the plot() function to create a line chart, which value must be assigned to the
type parameter to draw both points and lines connecting them? Write a sample code snippet
using a vector v.

Q4. Side-by-Side Bar Chart

By default, R creates stacked bar charts when using a matrix. Which argument and value
must be added to the barplot() function to display bars juxtaposed (side-by-side) instead of
stacked? Write the parameter syntax.

Q5. Pie Chart Direction

Write the syntax for a pie() function that includes the parameter to draw the slices in a
clockwise direction.

Q6. Histogram Bin Width

In the hist() function, which parameter is used to mention or control the width of each bar
(interval)?

Q7. Adding Legends

After plotting a chart with multiple categories (e.g., a stacked bar chart), write the R code to
add a legend at the "topleft" position with regions c("Delhi", "Mumbai") and fill colors
c("violet", "green").
Section D: Visualization with ggplot2
Q8. ggplot2 Basic Template

Fill in the blanks to complete the basic template used for generating plots in ggplot2:

ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) + <___________>()

Q9. ggplot2 Line Chart

Write a code snippet using ggplot2 to create a line chart for a data frame df. Map Country to
the x-axis and wc_win to the y-axis. You must include group = 1 in the aesthetics and add
the geom_line() function.

Q10. ggplot2 Box Plot

Write the R code to create a box plot using ggplot for a data frame named dat. Map setting
to the x-axis, rating to the y-axis, and add the appropriate geometry function for a box plot.

Additional Questions :

Q.9- Using the 'mtcars' dataset in R Studio, conduct a univariate analysis by generating
scatter plots for “mpg”.

You might also like