Foundations of R in Data Science Quiz Quiz

Explore essential concepts of R programming for data science, from data types and basic syntax to core functions and data visualization principles. Challenge your understanding of foundational R skills needed for effective data analysis and statistical computing.

  1. Identifying R Syntax

    Which of the following is the correct way to assign the value 10 to a variable called ‘count’ in R?

    1. count := 10
    2. count =: 10
    3. count u003C- 10
    4. 10 -u003E count

    Explanation: The correct assignment operator in R is the left arrow 'u003C-', so 'count u003C- 10' assigns the value properly. 'count =: 10' and 'count := 10' use invalid operators and are not recognized in R. '10 -u003E count' is valid but less commonly used and reverses the typical left-to-right assignment.

  2. Understanding Data Types

    What is the class of the following R object: x u003C- c(TRUE, FALSE, TRUE)?

    1. numeric
    2. character
    3. logical
    4. factor

    Explanation: The c(TRUE, FALSE, TRUE) vector creates a logical vector, which contains boolean values. 'numeric' would be for numbers, while 'character' is used for text, and 'factor' is for categorical variables. Only 'logical' matches the type in this context.

  3. Vectors in R

    If you want to create a sequence of numbers from 1 to 5 in R, which expression is correct?

    1. 1..5
    2. seq{1,5}
    3. [1:5]
    4. 1:5

    Explanation: The expression '1:5' produces a sequence of integers from 1 to 5. '1..5' is not valid R syntax, '[1:5]' resembles subsetting but not sequence generation, and 'seq{1,5}' uses incorrect brackets and syntax for the function.

  4. Factors and Categories

    Suppose you have survey responses stored as: c('yes', 'no', 'yes'). Which R data type should you use to represent these as categorical values?

    1. numeric
    2. logical
    3. matrix
    4. factor

    Explanation: Factors are used to represent categorical data such as survey responses. 'Numeric' applies to numbers, 'logical' is for TRUE/FALSE values, while 'matrix' is a multi-dimensional structure unsuitable for categorical variables.

  5. Data Frames Creation

    What is the primary characteristic that distinguishes a data frame from a matrix in R?

    1. A data frame allows multiple data types in columns, but a matrix does not
    2. A matrix can contain lists in its elements
    3. Data frames require equal-length rows and columns
    4. A data frame can only contain numeric values

    Explanation: Data frames can store different data types in different columns, while matrices require all elements to be of the same type. A data frame is not limited to numeric values. Matrices cannot contain lists directly, and both data structures need elements of equal length.

  6. Function Usage

    To see the first six rows of a data frame named 'df' in R, which function would you use?

    1. head(df)
    2. first(df)
    3. start(df)
    4. view(df)

    Explanation: The 'head' function displays the first part of a data object, typically the first six rows. 'start(df)' and 'first(df)' are not standard R functions. 'view(df)' may open an interactive view in some environments but does not output rows directly.

  7. Subsetting Data

    How would you select only the second column from a data frame 'data' using R?

    1. data[,2]
    2. data$2
    3. data[2]
    4. data[2,]

    Explanation: The correct way to select the second column is with 'data[,2]'. 'data[2,]' selects the second row. 'data$2' is invalid syntax because column names, not positions, follow the dollar sign. 'data[2]' returns the second column as a data frame but in a different structure.

  8. Reading Data Files

    Which R function is used to import a CSV file into a data frame?

    1. readfile()
    2. read.csv()
    3. import.csv()
    4. csv.read()

    Explanation: The 'read.csv()' function reads comma-separated CSV files into data frames. 'csv.read()', 'import.csv()', and 'readfile()' are not built-in R functions for this purpose. Only 'read.csv()' matches the standard data import syntax.

  9. Basic Data Visualization

    Which function would you use in R to create a basic scatter plot of variables x and y?

    1. graph(x, y)
    2. chart(x, y)
    3. map(x, y)
    4. plot(x, y)

    Explanation: 'plot(x, y)' creates a scatter plot of two variables in base R. 'graph', 'map', and 'chart' are not base R graphics functions or their syntax is incorrect. Only 'plot' directly produces the intended visualization.

  10. Missing Values Handling

    Which symbol does R use to denote missing values in a vector or data frame?

    1. BLANK
    2. NULL
    3. NA
    4. NaN

    Explanation: 'NA' is the standard symbol for missing values in R data structures. 'NULL' represents the absence of an object, while 'NaN' denotes 'Not a Number' from invalid calculations. 'BLANK' is not a standard missing value indicator in R.