Assess your understanding of data import and export processes in R, including reading and writing various file types, handling data frames, and choosing the right functions for different scenarios. This quiz covers essential R commands and common practices to help you efficiently manage data flow in your projects.
Which R function should you use to import a simple comma-separated values (CSV) file named 'data.csv' into a data frame?
Explanation: The correct function is read.csv which is specifically designed to read CSV files into R as data frames. readTable is not a valid R function for importing CSV files. importCSV and load.csv do not exist in base R. Using the correct function ensures the file is read properly and data types are interpreted as expected.
To save a data frame called df to a text file with tab-separated values, which function should you use in R?
Explanation: write.table with the sep=' ' argument saves data frames as tab-separated text files, which is a standard approach in R. saveData, write.df, and storeTable are not recognized base R functions for this purpose. Using write.table gives you flexibility in choosing delimiters and additional export options.
If you need to import an Excel '.xlsx' file into R, which of the following approaches is correct?
Explanation: Importing Excel files usually requires a package in R, such as using read.xlsx() which is not available in base R. read.table() is for plain text files like CSV or tab-separated files, not Excel workbooks. read.excel and loadExcel are not standard R functions. Packages designed for Excel make handling spreadsheet data much easier.
Which function stores a data frame as an R-specific binary file for later use, preserving data types and structure?
Explanation: The save function writes R objects in a binary format that maintains their structure and types for later use with load. exportCSV, write.bin, and dumpCSV are either incorrect or non-existent in R for this purpose, and would not properly save a data frame for R-specific loading.
When importing a CSV file where the first line contains column names, which argument ensures column names are read correctly?
Explanation: Setting header=TRUE tells the import function to use the first line of the file as column names. names=TRUE, col.names=TRUE, and labels=TRUE are not correct arguments for this purpose in standard import functions like read.csv. Using header=TRUE helps avoid mismatches between data and column names.