What will be the output of read.table("scores.csv", sep=",", header=TRUE)?
medium
A. An error because sep="," is incorrect for CSV files
B. A data frame with columns Name and Score and 3 rows
C. A data frame with one column containing all text
D. A data frame with no rows because header=TRUE skips all data
Solution
Step 1: Understand the file format and arguments
The file is comma-separated with a header row. Using sep="," and header=TRUE matches this format.
Step 2: Predict the output
The function will read the file into a data frame with columns "Name" and "Score" and three rows for Alice, Bob, and Carol.
Final Answer:
A data frame with columns Name and Score and 3 rows -> Option B
Quick Check:
CSV + sep="," + header=TRUE = correct data frame [OK]
Hint: CSV files use sep="," and header=TRUE if first row has names [OK]
Common Mistakes:
Assuming sep="," causes error
Expecting one column instead of two
Thinking header=TRUE skips all data
4. You try to read a semicolon-separated file with read.table("data.txt", sep=",", header=TRUE) but get wrong columns. What is the likely problem?
medium
A. The file path is incorrect
B. The file does not have headers, so header=TRUE is wrong
C. The separator is incorrect; should be sep=";"
D. The file is empty
Solution
Step 1: Analyze the separator mismatch
The file uses semicolons to separate columns, but the code uses comma separator, causing wrong parsing.
Step 2: Correct the separator
Changing sep="," to sep=";" matches the file format and fixes the issue.
Final Answer:
The separator is incorrect; should be sep=";" -> Option C
Quick Check:
Separator mismatch causes wrong columns [OK]
Hint: Match sep to actual file delimiter [OK]
Common Mistakes:
Changing header instead of sep
Assuming file path error without checking sep
Ignoring file content format
5. You have a file data.txt where columns are separated by a pipe character (|) and the first row is not a header. Which read.table call correctly reads this file?
hard
A. read.table("data.txt", sep="\\|", header=FALSE)
B. read.table("data.txt", sep="\|", header=TRUE)
C. read.table("data.txt", sep="|", header=TRUE)
D. read.table("data.txt", sep="|", header=FALSE)
Solution
Step 1: Identify the correct separator for pipe
The pipe character is used as is in sep without escaping in R strings, so sep="|" is correct.
Step 2: Set header argument correctly
The first row is not a header, so header=FALSE must be set to treat all rows as data.
Final Answer:
read.table("data.txt", sep="|", header=FALSE) -> Option D
Quick Check:
Pipe separator + no header = read.table("data.txt", sep="|", header=FALSE) [OK]
Hint: Use sep="|" and header=FALSE if no header row [OK]
Common Mistakes:
Escaping pipe incorrectly in sep
Setting header=TRUE when no header exists
Using double backslashes unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.