Imagine you want to print a user-friendly message based on the value of a variable in R. How would you approach it?

  • None of the above
  • Use the cat() function with switch statements
  • Use the echo() function with for loops
  • Use the print() function with if-else statements
You can use conditional statements like if-else in combination with the print() function to display different messages based on the value of a variable. This allows you to make the output more user-friendly and informative.

Imagine you need to determine the data type of a variable in R. How would you do this?

  • Use the mode() function on the variable
  • Use the typeof() function on the variable
  • Use the class() function on the variable
  • Use the str() function on the variable
To determine the data type of a variable in R, you would use the typeof() function on the variable. The typeof() function returns a character string representing the data type of the object.

In R, the ______ function can be used to list all the variables in the global environment.

  • ls()
  • vars()
  • objects()
  • globals()
In R, the ls() function can be used to list all the variables in the global environment. It returns the names of all the objects or variables defined in the global environment, allowing you to inspect and access the global variables present in your program.

Can you explain how R handles 'AND' and 'OR' operations with NA values?

  • In 'AND' operations, if either operand is 'NA', the result is 'NA'. In 'OR' operations, if either operand is 'NA', the result is 'NA'.
  • In 'AND' operations, if either operand is 'NA', the result is 'FALSE'. In 'OR' operations, if either operand is 'NA', the result is 'TRUE'.
  • In 'AND' operations, if either operand is 'NA', the result is 'TRUE'. In 'OR' operations, if either operand is 'NA', the result is 'FALSE'.
  • In 'AND' operations, if either operand is 'NA', an error is thrown. In 'OR' operations, if either operand is 'NA', an error is thrown.
When performing 'AND' and 'OR' operations in R, if either operand is 'NA', the result will be 'NA' for both 'AND' and 'OR' operations. This is because the presence of 'NA' indicates that the value is missing or unknown, resulting in an unknown outcome for the logical operation.

How would you customize the appearance of an R scatter plot, including changing colors, markers, and sizes?

  • By using the col, pch, and cex parameters in the plot() function
  • By using the legend() function
  • By using the theme() function from the ggplot2 package
  • By using the par() function and graphical parameters
To customize the appearance of an R scatter plot, including changing colors, markers, and sizes, you can use the col parameter to change colors, the pch parameter to change markers, and the cex parameter to change the size of the points. These graphical parameters can be specified within the plot() function.

How can you handle situations where your calculations result in 'Inf' or 'NaN'?

  • Both of these methods
  • None of the above
  • Use ifelse() function to handle such situations
  • Use is.finite() function to check the result
One way to handle this is by using the is.finite() function which checks whether the value is finite or not. This function returns FALSE if the value is Inf or NaN and TRUE otherwise. Depending on the use case, you can then decide how to handle these non-finite values.

How does R handle lists that contain elements of different data types?

  • R allows lists to contain elements of different data types without coercion
  • R coerces the elements to the most flexible type within the list
  • R assigns each element a unique data type within the list
  • R throws an error if a list contains elements of different data types
R allows lists to contain elements of different data types without coercing them. Unlike vectors, where elements are coerced to a common type, lists retain the individual data types of their elements. This means you can have a list with elements that are numeric, character, logical, etc., all coexisting without being coerced.

To calculate the median of each column in a data frame in R, you would use the ______ function.

  • apply()
  • colMedian()
  • median()
  • colMeans()
To calculate the median of each column in a data frame in R, you would use the apply() function. By specifying the appropriate margin argument (2 for columns), you can apply the median() function across each column of the data frame.

How does the ifelse() function in R differ from the if-else statement?

  • The ifelse() function allows vectorized conditional operations, while the if-else statement only works with scalar conditions
  • The ifelse() function can only handle logical conditions, while the if-else statement can handle any type of condition
  • The if-else statement is more efficient than the ifelse() function for large datasets
  • The ifelse() function and the if-else statement are functionally equivalent
The ifelse() function in R allows vectorized conditional operations, which means it can process entire vectors of conditions and return corresponding values based on those conditions. In contrast, the if-else statement in R works with scalar conditions and can only evaluate one condition at a time.

What function is commonly used to create a basic plot in R?

  • plot()
  • barplot()
  • hist()
  • scatterplot()
The plot() function is commonly used to create a basic plot in R. It can be used to create a wide range of plots such as scatter plots, line plots, bar plots, and more.