0% found this document useful (0 votes)
10 views4 pages

CT Queston Solution

RStudio consists of four main panes: Source Pane for writing scripts, Console Pane for running commands interactively, Environment/History Pane for displaying variables and command history, and Files/Plots/Packages/Help/Viewer Pane for managing files and visualizations. Functions in R are used for code reusability, modularity, readability, and avoiding repetition, with an example function provided to calculate variance. The document also covers basic data types in R and provides commands to create a 3x2 matrix and a data frame with specific vectors.

Uploaded by

zidanbinkayome
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)
10 views4 pages

CT Queston Solution

RStudio consists of four main panes: Source Pane for writing scripts, Console Pane for running commands interactively, Environment/History Pane for displaying variables and command history, and Files/Plots/Packages/Help/Viewer Pane for managing files and visualizations. Functions in R are used for code reusability, modularity, readability, and avoiding repetition, with an example function provided to calculate variance. The document also covers basic data types in R and provides commands to create a 3x2 matrix and a data frame with specific vectors.

Uploaded by

zidanbinkayome
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
You are on page 1/ 4

Question: How many windows/panes in R studio? Briefly explain their main task?

Answer: RStudio has four main panes (or windows).They are: Source Pane (Top Left),Console Pane (Bottom
Left),Environment/History Pane (Top Right) ,Files/Plots/Packages/Help/Viewer Pane (Bottom Right).Here are the
main tasks of each pane in RStudio:

Source Pane (Top Left) : Used for writing and editing scripts, R Markdown, and other code files. It allows you
to save and run code efficiently.

Console Pane (Bottom Left) : Runs R commands interactively. You can type commands directly and see
immediate results.

Environment/History Pane (Top Right):

 Environment Tab: Displays stored variables, data frames, and loaded objects.
 History Tab: Keeps a record of previously executed commands.

Files/Plots/Packages/Help/Viewer Pane (Bottom Right)

 Files: Manages project files.


 Plots: Displays generated graphs.
 Packages: Manages installed R packages.
 Help: Provides documentation for functions and packages.
 Viewer: Renders web content (e.g., interactive visualizations).

Question: When should we use function? Write a unction in R to find out the variance of a
vector?

Answer:

We should use a function when:

 Code Reusability – If you need to perform the same task multiple times.
 Modularity – To break down complex tasks into smaller, manageable parts.
 Readability & Maintenance – Functions make your code cleaner and easier to understand.
 Avoiding Repetition – Instead of rewriting the same code, use a function to simplify the process

R Function to Calculate Variance of a Vector: Here’s a simple function to compute variance in R:

calculate_variance <- function(x) {

n <- length(x)

mean_x <- mean(x)

variance <- sum((x - mean_x)^2) / (n - 1) # Sample variance formula

return(variance)

}
Question: write down the name of different data types and their uses in R. Write a syntax
to input a 3 by 2 metrix where elements will go by rows?

Answer:

Data Types: In programming, data type is an important concept. R has a variety of data types and object
classes. Basic data types in R can be divided into the following types:

 Numeric: A numeric data type is the most common type in R, and contains any number with or without
a decimal. Example: 10.5, 55, 787
 Integer: Integers are numeric data without decimals. This is used when you are certain that you will
never create a variable that should contain decimals. To create an integer variable, you must use the
letter L after the integer value. Example: 1000L, 55L.
 Complex: A complex number is written with an "i" as the imaginary part. Example: (9 + 3i, where "i" is
the imaginary part)
 Character: These are string which are single or double quotes. Example: "k", "R is exciting", ‘FALSE’,
‘11.5’.
 Logical (boolean): TRUE or FALSE.

Matrix: In R, you can use the matrix() function to create a 3×2 matrix where elements are filled by rows using
the byrow = TRUE argument. Here’s the syntax:

# Creating a 3x2 matrix with elements filled by rows

my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2, byrow = TRUE)

# Print the matrix

print(my_matrix)

Question: To crate a following data frame, write down the appropriate commands in R.
Give a name of this data frame. Create four vectors of height, weight, gender and religion
of 5 pupils. Make a data frame naming as data_set.

Answer:

To create a data frame in R with four vectors (height, weight, gender, and religion) for 5 pupils, use the
following commands:

height <- c(150, 160, 155, 165, 170) # Heights in cm

weight <- c(45, 55, 50, 60, 65) # Weights in kg

gender <- c("Male", "Female", "Male", "Female", "Male") # Gender

religion <- c("Islam", "Hindu", "Buddhist", "Christian", "Islam") # Religion

data_set <- data.frame(Height = height, Weight = weight, Gender = gender, Religion = religion)

# Printing the data frame

print(data_set)
Explanation:

 Created four vectors: height, weight, gender, religion.


 Used data.frame() to combine these vectors into a data frame named data_set.
 Printed the data frame to check its contents.

Example Output:

Height Weight Gender Religion

1 150 45 Male Islam

2 160 55 Female Hindu

3 155 50 Male Buddhist

4 165 60 Female Christian

5 170 65 Male Islam

You might also like