0
0
R Programmingprogramming~20 mins

read.table and delimiters in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delimiter Mastery in read.table
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of read.table with comma delimiter
What is the output of the following R code when reading a CSV string with read.table using comma as delimiter?
R Programming
text <- "name,age,score\nAlice,30,85\nBob,25,90"
data <- read.table(text = text, sep = ",", header = TRUE)
print(data)
AError in read.table: no header found
B
  V1  V2  V3
1 name age score
2 Alice 30 85
3 Bob 25 90
C
  name age score
1 Alice  30    85
2   Bob  25    90
D
  name age score
1 Alice 30 85
2 Bob 25 90
3 NA NA NA
Attempts:
2 left
💡 Hint
Check the separator and header arguments in read.table.
Predict Output
intermediate
2:00remaining
Effect of wrong delimiter in read.table
What will be the output of this R code when reading a tab-separated string but using comma as delimiter?
R Programming
text <- "name\tage\tscore\nAlice\t30\t85\nBob\t25\t90"
data <- read.table(text = text, sep = ",", header = TRUE)
print(data)
A
  V1
1 name\tage\tscore
2 Alice\t30\t85
3 Bob\t25\t90
B
  name age score
1 Alice 30 85
2 Bob 25 90
CError: sep must be a single character
D
  name\tage\tscore
1 Alice\t30\t85
2 Bob\t25\t90
Attempts:
2 left
💡 Hint
What happens if the separator does not match the actual delimiter in the data?
🧠 Conceptual
advanced
1:30remaining
Understanding read.table default delimiter
Which delimiter does read.table use by default if sep is not specified?
AAny whitespace (spaces or tabs)
BTab ("\t")
CComma (",")
DSemicolon (";")
Attempts:
2 left
💡 Hint
Think about how read.table handles spaces and tabs by default.
Predict Output
advanced
2:00remaining
Reading data with multiple delimiters
What will be the output of this R code that tries to read data with mixed delimiters using read.table?
R Programming
text <- "name;age,score\nAlice;30,85\nBob;25,90"
data <- read.table(text = text, sep = ";", header = TRUE)
print(data)
AError in read.table: more columns than header
B
  name age score
1 Alice 30 85
2 Bob 25 90
C
  V1
1 name;age,score
2 Alice;30,85
3 Bob;25,90
D
  name age,score
1 Alice 30,85
2 Bob 25,90
Attempts:
2 left
💡 Hint
What happens when sep matches only part of the delimiter pattern?
Predict Output
expert
2:30remaining
Handling missing values with read.table and delimiters
Given this R code reading data with missing values represented by empty fields, what is the output?
R Programming
text <- "name,age,score\nAlice,30,\nBob,,90"
data <- read.table(text = text, sep = ",", header = TRUE, na.strings = "")
print(data)
A
  name age score
1 Alice 30 ""
2 Bob "" 90
B
  name age score
1 Alice 30 NA
2 Bob NA 90
CError: missing values not allowed
D
  name age score
1 Alice 30 0
2 Bob 0 90
Attempts:
2 left
💡 Hint
How does na.strings argument affect empty fields?