Suppose you're given a factor in R and asked to calculate its mode. How would you do this?
- Convert the factor to a character vector and calculate the mode
- Apply the mode() function directly on the factor
- Use the levels() function to identify the most frequent level
- Convert the factor to a numeric vector and calculate the mode
To calculate the mode of a factor in R, you would use the levels() function to identify the most frequent level among the distinct levels present in the factor.
In R, a function nested inside another function has access to the variables in the ________ of the outer function.
- environment
- global environment
- parent environment
- child environment
In R, a function nested inside another function has access to the variables in the parent environment of the outer function. This allows the nested function to access and manipulate variables defined in the outer function, even after the outer function has finished executing. The scoping rules in R facilitate this access to variables from higher-level environments.
In R, the ______ function can be used to check if an object is a list.
- is.list()
- is.vector()
- is.data.frame()
- is.matrix()
In R, the is.list() function can be used to check if an object is a list. It returns TRUE if the object is a list and FALSE otherwise. This function is useful for verifying the type of an object before applying operations specific to lists.
In R, to match a literal period in a regular expression, you would use the escape sequence ________.
- .
- *
- /
- ?
In R, to match a literal period (dot) in a regular expression, you would use the escape sequence . . For example, "abc.def" would match the string "abc.def".
How do you convert a numeric variable to a string in R?
- as.character()
- convert_to_string()
- str()
- to_string()
In R, the as.character() function is used to convert a numeric variable to a string. For example, as.character(123) would return "123".
Can variables in R hold more than one data type at a time?
- No, variables in R can hold only one data type at a time
- None of the above
- Yes, if the variable is a list
- Yes, if the variable is a vector
In R, a variable can hold more than one data type at a time if it is a list. Lists in R can contain elements of different types (e.g., numbers, strings, vectors, and other lists). However, other common R data structures, such as vectors and matrices, can hold only one data type at a time.
If you're using a for loop in R to modify the elements of a vector, it's often more efficient to first create a copy of the vector using the ______ function.
- copy()
- duplicate()
- clone()
- rep()
If you're using a for loop in R to modify the elements of a vector, it's often more efficient to first create a copy of the vector using the duplicate() function. This way, you avoid modifying the original vector during the loop, which can be costly for larger vectors.
Can you nest different types of loops in R, like for inside while and vice versa?
- Yes, different types of loops can be nested in R
- No, only loops of the same type can be nested
- It depends on the R version being used
- It depends on the operating system
Yes, different types of loops, such as for inside while and vice versa, can be nested in R. This means you can have a loop of one type inside a loop of another type, allowing for more flexible control flow and iteration.
In R, the data type of an object is returned by the ______ function.
- mode()
- typeof()
- class()
- str()
In R, the typeof() function is used to determine the data type of an object. It returns a character string representing the data type of the object.
Suppose you have a character variable that contains a number, and you want to convert it to a numeric variable. How would you do that?
- Use as.character() function
- Use as.numeric() function
- Use toNumeric() function
- Use toString() function
To convert a character variable that contains a number to a numeric variable in R, we use the as.numeric() function. For example, as.numeric("123") would return 123.