Bird
Raised Fist0
R Programmingprogramming~20 mins

read.csv and write.csv in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this R code reading a CSV?

Consider the CSV file data.csv with content:

name,age
Alice,30
Bob,25

What will be the output of this R code?

R Programming
df <- read.csv('data.csv')
print(df$age[2])
A25
B30
CNULL
DError: object 'data.csv' not found
Attempts:
2 left
💡 Hint

Remember that read.csv reads the file into a data frame and $ accesses a column.

❓ Predict Output
intermediate
2:00remaining
What does this code write to a CSV file?

Given this R code:

df <- data.frame(name=c('Tom','Jerry'), score=c(88, 92))
write.csv(df, 'scores.csv', row.names=FALSE)

What will the content of scores.csv be?

A
"name","score"
"Tom",88
"Jerry",92
B
"name","score"
Tom,88
Jerry,92
C
name,score
Tom,88
Jerry,92
DError: row.names argument invalid
Attempts:
2 left
💡 Hint

By default, write.csv adds quotes around strings and includes header.

🔧 Debug
advanced
2:00remaining
Why does this read.csv call fail?

What error does this code produce and why?

df <- read.csv('data.csv', header=FALSE)
print(df$age)

Assume data.csv has a header row with columns name and age.

R Programming
df <- read.csv('data.csv', header=FALSE)
print(df$age)
APrints the age column correctly
BError: object 'age' not found
CNULL because no column named 'age' exists without header
DSyntaxError due to missing comma
Attempts:
2 left
💡 Hint

When header=FALSE, column names default to V1, V2, ....

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error when writing CSV?

Which of these write.csv calls is syntactically invalid?

Awrite.csv(df, 'output.csv', row.names=FALSE)
Bwrite.csv(df, file='output.csv')
Cwrite.csv(df, file='output.csv', row.names=TRUE)
Dwrite.csv(df, file='output.csv' row.names=FALSE)
Attempts:
2 left
💡 Hint

Check commas between arguments.

🚀 Application
expert
2:00remaining
How many rows will this read.csv produce?

Given a CSV file data.csv with 5 rows of data plus 1 header row, what is the number of rows in df after this code?

df <- read.csv('data.csv', skip=2)
R Programming
df <- read.csv('data.csv', skip=2)
A4
B3
C5
D6
Attempts:
2 left
💡 Hint

The skip argument skips lines before reading header.

Practice

(1/5)
1. What does the read.csv function do in R?
easy
A. Loads data from a CSV file into a data frame
B. Saves a data frame to a CSV file
C. Creates a new CSV file with default data
D. Deletes a CSV file from the disk

Solution

  1. Step 1: Understand the purpose of read.csv

    The read.csv function is designed to read data stored in CSV files and load it into R as a data frame.
  2. Step 2: Differentiate from write.csv

    write.csv saves data frames to CSV files, so it does the opposite of read.csv.
  3. Final Answer:

    Loads data from a CSV file into a data frame -> Option A
  4. Quick Check:

    read.csv loads CSV data [OK]
Hint: Remember: read.csv reads files, write.csv writes files [OK]
Common Mistakes:
  • Confusing read.csv with write.csv
  • Thinking read.csv creates new files
  • Assuming read.csv deletes files
2. Which of the following is the correct syntax to save a data frame named df to a CSV file called output.csv?
easy
A. read.csv(df, file = "output.csv")
B. write.csv(df, file = "output.csv")
C. write.csv(file = df, "output.csv")
D. write.csv("output.csv", df)

Solution

  1. Step 1: Recall the argument order of write.csv

    The first argument is the data frame to save, the second is the file name with the parameter file=.
  2. Step 2: Check each option's argument order

    write.csv(df, file = "output.csv") correctly uses write.csv(df, file = "output.csv"). read.csv(df, file = "output.csv") uses read.csv which is for reading, not writing. Options B and D have wrong argument order.
  3. Final Answer:

    write.csv(df, file = "output.csv") -> Option B
  4. Quick Check:

    write.csv(data, file=filename) syntax [OK]
Hint: write.csv(data, file=filename) saves data frame [OK]
Common Mistakes:
  • Swapping arguments order
  • Using read.csv instead of write.csv
  • Omitting the file= parameter
3. What will be the output of the following R code?
data <- data.frame(Name = c("Anna", "Bob"), Age = c(25, 30))
write.csv(data, file = "people.csv", row.names = FALSE)
read_data <- read.csv("people.csv")
print(read_data)
medium
A. Error: file not found
B. Name Age 0 Anna 25 1 Bob 30
C. Name Age 1 Anna 25 2 Bob 30 3 NA NA
D. Name Age 1 Anna 25 2 Bob 30

Solution

  1. Step 1: Understand the data frame and write.csv call

    The data frame has two rows with names and ages. The write.csv saves it without row names because row.names = FALSE.
  2. Step 2: Read the CSV and print

    The read.csv reads the file back, creating a data frame with default row numbers 1 and 2. Printing shows the two rows with correct data.
  3. Final Answer:

    Name Age 1 Anna 25 2 Bob 30 -> Option D
  4. Quick Check:

    write.csv without row.names keeps data clean [OK]
Hint: Use row.names=FALSE to avoid extra row numbers in CSV [OK]
Common Mistakes:
  • Forgetting row.names=FALSE adds extra column
  • Expecting zero-based row numbers
  • Assuming file not found error
4. Identify the error in this code snippet:
df <- data.frame(x = 1:3, y = 4:6)
write.csv(df, "data.csv")
new_df <- read.csv(data.csv)
print(new_df)
medium
A. print() cannot display data frames
B. write.csv should not have data frame as first argument
C. Missing quotes around file name in read.csv
D. read.csv cannot read CSV files

Solution

  1. Step 1: Check the write.csv usage

    write.csv(df, "data.csv") is correct syntax to save the data frame.
  2. Step 2: Check the read.csv usage

    The code uses read.csv(data.csv) without quotes around the file name, which causes an error because R treats data.csv as a variable, not a string.
  3. Final Answer:

    Missing quotes around file name in read.csv -> Option C
  4. Quick Check:

    File names must be strings in quotes [OK]
Hint: Always put file names in quotes in read.csv and write.csv [OK]
Common Mistakes:
  • Omitting quotes around file names
  • Confusing argument order
  • Thinking print() can't show data frames
5. You have a CSV file with missing values represented as empty cells. Which read.csv argument helps you automatically convert these empty cells to NA in R?
hard
A. na.strings = ""
B. stringsAsFactors = TRUE
C. header = FALSE
D. row.names = NULL

Solution

  1. Step 1: Understand missing values in CSV

    Empty cells in CSV files often represent missing data, which R can mark as NA if told how to interpret them.
  2. Step 2: Use na.strings argument

    The na.strings = "" argument tells read.csv to treat empty strings as NA values automatically.
  3. Final Answer:

    na.strings = "" -> Option A
  4. Quick Check:

    na.strings="" converts empty cells to NA [OK]
Hint: Use na.strings="" to read blanks as NA [OK]
Common Mistakes:
  • Ignoring na.strings argument
  • Using stringsAsFactors which affects factor conversion
  • Confusing header or row.names with missing data handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard