CT Queston Solution
CT Queston Solution
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 Tab: Displays stored variables, data frames, and loaded objects.
History Tab: Keeps a record of previously executed commands.
Question: When should we use function? Write a unction in R to find out the variance of a
vector?
Answer:
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
n <- length(x)
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:
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:
data_set <- data.frame(Height = height, Weight = weight, Gender = gender, Religion = religion)
print(data_set)
Explanation:
Example Output: