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
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=.
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.
Final Answer:
write.csv(df, file = "output.csv") -> Option B
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?
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
Step 1: Check the write.csv usage
write.csv(df, "data.csv") is correct syntax to save the data frame.
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.
Final Answer:
Missing quotes around file name in read.csv -> Option C
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
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.
Step 2: Use na.strings argument
The na.strings = "" argument tells read.csv to treat empty strings as NA values automatically.
Final Answer:
na.strings = "" -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.