📖 Scenario: Imagine you are organizing a small library. You have books with titles, the number of pages, and whether they are currently borrowed or not. To keep track, you use R to store this information.
🎯 Goal: You will create a data structure to hold book information with correct data types, add a filter condition, extract specific data using the right data types, and finally display the filtered result.
📋 What You'll Learn
Create a data frame called library with columns title (character), pages (numeric), and borrowed (logical) with exact values
Create a variable called min_pages set to 200
Use a subset operation to create long_books containing only books with pages greater than min_pages
Print the long_books data frame
💡 Why This Matters
🌍 Real World
Data types help you organize and analyze data correctly, like knowing which books are long or currently borrowed in a library system.
💼 Career
Understanding data types is essential for data analysis, reporting, and making decisions based on data in many jobs like data science, research, and software development.
Progress0 / 4 steps
1
Create the library data frame
Create a data frame called library with these exact columns and values: title with "Data Science", "R Programming", "Statistics", pages with 250, 180, 300, and borrowed with TRUE, FALSE, TRUE.
R Programming
Hint
Use data.frame() with c() to create vectors for each column.
2
Set the minimum pages threshold
Create a variable called min_pages and set it to 200.
R Programming
Hint
Just assign the number 200 to min_pages.
3
Filter books with more than min_pages pages
Create a new data frame called long_books that contains only rows from library where the pages value is greater than min_pages.
R Programming
Hint
Use subset() with the condition pages > min_pages.
4
Display the filtered books
Print the long_books data frame to show the books with more than 200 pages.
R Programming
Hint
Use print(long_books) to display the filtered data frame.
Practice
(1/5)
1. Why is it important to know the data type of a variable in R?
easy
A. Because R uses data types to decide how to store and process data
B. Because data types determine the color of the output in RStudio
C. Because data types tell R which keyboard to use
D. Because data types control the size of the R window
Solution
Step 1: Understand what data types do in R
Data types tell R how to store and process the data, like numbers, text, or logical values.
Step 2: Identify the importance of data types
Knowing data types helps R perform correct operations and avoid errors.
Final Answer:
Because R uses data types to decide how to store and process data -> Option A
Quick Check:
Data types guide processing [OK]
Hint: Data types tell R how to handle data correctly [OK]
Common Mistakes:
Thinking data types affect display colors
Confusing data types with interface settings
Ignoring data types leads to errors
2. Which of the following is the correct way to check the data type of a variable x in R?
easy
A. datatype(x)
B. checktype(x)
C. typeof(x)
D. type(x)
Solution
Step 1: Recall R functions for data types
In R, the function to check the internal type of an object is typeof().
Step 2: Match the correct function
Among the options, only typeof(x) is a valid R function to check data type.
Final Answer:
typeof(x) -> Option C
Quick Check:
Use typeof() to check data type [OK]
Hint: Use typeof() to find variable data type in R [OK]
Common Mistakes:
Using non-existent functions like checktype()
Confusing typeof() with type()
Trying datatype() which is not an R function
3. What will be the output of this R code?
x <- c(1, 2, 3) typeof(x)
medium
A. "double"
B. "vector"
C. "numeric"
D. "integer"
Solution
Step 1: Understand the vector creation
The vector x is created with numbers 1, 2, 3 without specifying integer type explicitly.
Step 2: Check typeof() behavior for numeric vectors
In R, numeric values without L suffix are stored as double precision numbers, so typeof(x) returns "double".
Final Answer:
"double" -> Option A
Quick Check:
Numeric vector default type = "double" [OK]
Hint: Numbers without L suffix are double by default [OK]
Common Mistakes:
Assuming numeric means integer
Confusing typeof() output with class() output
Thinking vector is a data type string
4. Identify the error in this R code snippet:
y <- "123" z <- y + 1 typeof(z)
medium
A. typeof() cannot be used on variable z
B. The variable y is not defined
C. The plus sign (+) is missing
D. You cannot add a number to a character string directly
Solution
Step 1: Analyze the operation y + 1
Variable y is a character string "123". Adding 1 to a string is not allowed in R.
Step 2: Understand the error cause
R cannot perform arithmetic on character strings without conversion, so this causes an error.
Final Answer:
You cannot add a number to a character string directly -> Option D
Quick Check:
Adding number to string causes error [OK]
Hint: Cannot add numbers to strings without conversion [OK]
Common Mistakes:
Assuming R auto-converts strings to numbers
Thinking typeof() causes error
Ignoring variable definitions
5. You have a vector v <- c("1", "2", "3") and want to calculate the sum of its values. Which is the correct approach?
hard
A. Use sum(v) directly
B. Convert to numeric first: sum(as.numeric(v))
C. Use sum(as.character(v))
D. Use sum(as.logical(v))
Solution
Step 1: Identify the data type of vector v
Vector v contains character strings representing numbers.
Step 2: Understand sum() behavior
sum() expects numeric input. Using sum(v) directly will cause an error or incorrect result.
Step 3: Convert characters to numeric
Use as.numeric(v) to convert strings to numbers, then sum them.
Final Answer:
Convert to numeric first: sum(as.numeric(v)) -> Option B
Quick Check:
Convert strings to numbers before sum [OK]
Hint: Convert strings to numeric before math operations [OK]
Common Mistakes:
Trying to sum character vectors directly
Converting to logical instead of numeric
Using as.character() which does nothing here
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.