1. Which of the following variable names are INVALID in R?
- 1_variable
- variable_1
- _variable
- variable@
Answer :- a. 1_variable d. variable@ In R, variable names must follow certain rules and conventions. The valid variable names in R can only contain letters (a-z, A-Z), numbers (0-9), and dots (.) or underscores (_). Variable names cannot start with a number or contain special characters other than dots or underscores.
2. The function ls() in R will
- set a new working directory path
- list all objects in our working environment
- display the path to our working directory
- None of the above
Answer :- b. list all objects in our working environment ls() stands for "list objects" and is used to display the names of objects (variables, functions, etc.) that are currently present in the working environment in R. It does not change the working directory or display the path to the working directory. Instead, it lists the objects available in the current R session.
Consider a following code snippet. Based on this, answer questions 3 and 4.
3. Which of the following command is used to access the value “Shyam”?
- print(patient list[3][2])
- print(patient list[[3]][1])
- print(patient list[[3]][2])
- print(patient list[[2]][2])
Answer :- c. print(patient_list[[3]][2]) In R, double square brackets [[ ]] are used to extract elements from a list. The first index [[3]] selects the third element of the list, which is itself another list. Then, the second index [2] selects the second element from this inner list, which is "Shyam". Therefore, patient_list[[3]][2] will give you the value "Shyam".
4. The output of the code given below is
d. Code will throw an error.
Answer :- c.
5. What is the output of following code?
- double
- integer
- list
- None of the above
Answer :- a. double
6. State whether the given statement is True or False.
The library reshape2 is based around two key functions named melt and cast.
- True
- False
Answer :- True. The statement is true. The library reshape2 in R is built around two key functions: melt and dcast (formerly known as acast). These functions are used for reshaping data from wide to long format and vice versa. The melt function is used to transform data from a wide format to a long format, while dcast is used to transform data from a long format to a wide format.
7. What is the output of following code?
- 6
- 4
- 2
- 8
Answer :- b. 4
Create the data frame using the code given below and answer questions 8 and 9.
student data = data.frame(student id=c(1:4), student name=c(‘Ram’,‘Harish’,‘Pradeep’,‘Rajesh’))
8. Choose the correct command to add a column named student_dept to the dataframe student_data.
- student datastudent dept=c(“Commerce”, “Biology”, “English”, “Tamil”)
- student data[“student dept”]= c(“Commerce”,“Biology”, “English”,“Tamil”)
- student dept= student data[c(“Commerce”,“Biology”,“English”,“Tamil”)]
- None of the above
Answer :- a. student_data$student_dept = c("Commerce", "Biology", "English", "Tamil") b. student data[“student dept”]= c(“Commerce”,“Biology”, “English”,“Tamil”)
9. Choose the correct command to access the element Tamil in the dataframe student_data
- student data[[4]]
- student data[[4]][3]
- student data[[3]][4]
- None of the above
Answer :- student_data[[3]][4] In R, double square brackets [[ ]] are used to extract elements from a list or dataframe. In this case, student_data[[3]] extracts the third column (which is a vector containing "Commerce", "Biology", "English", "Tamil"), and then [4] is used to access the fourth element in that vector, which is "Tamil". Therefore, student_data[[3]][4] will give you the value "Tamil".
10. The command to check if a value is of numeric data type is _____.
- typeof()
- is.numeric()
- as.numeric()
- None of the above
Answer :- is.numeric() The is.numeric() function in R is used to determine whether a given value is of numeric data type or not. It returns TRUE if the value is numeric and FALSE otherwise. For example: