WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

R · Basic · question 11 of 100

How do you read and write data from/to a CSV file in R?

📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

In R, CSV files are a common data format for storing and exchanging data. Reading and writing data from/to a CSV file is a common task in data analysis and modeling, and R provides several functions for handling CSV files.

Reading data from a CSV file: You can read data from a CSV file in R using the read.csv() function. The read.csv() function takes the name of the CSV file as input, and returns a data frame containing the data in the CSV file. Here’s an example:

    # Reading data from a CSV file
    my_data <- read.csv("my_data.csv")
    
    # Displaying the data frame
    my_data

In this example, the read.csv() function reads the data from the CSV file "my_data.csv" and stores it in a data frame called my_data.

Writing data to a CSV file: You can write data to a CSV file in R using the write.csv() function. The write.csv() function takes the data to be written and the name of the CSV file as input, and writes the data to the specified file. Here’s an example:

    # Writing data to a CSV file
    my_data <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
    write.csv(my_data, file = "my_data.csv", row.names = FALSE)

In this example, we create a data frame called my_data containing two columns, and write it to a CSV file called "my_data.csv" using the write.csv() function. The row.names = FALSE argument tells R not to include row names in the CSV file.

Specifying options for reading and writing CSV files: Both the read.csv() and write.csv() functions have several optional arguments that can be used to customize their behavior. Some of the commonly used options include:

Here’s an example that demonstrates how to use some of these options:

    # Reading data from a CSV file with custom options
    my_data <- read.csv("my_data.csv", header = TRUE, sep = ";", colClasses = c("numeric", "factor"))
    
    # Writing data to a CSV file with custom options
    write.csv(my_data, file = "my_data.csv", row.names = FALSE, quote = "'", na = "")

In this example, we use the header, sep, and colClasses options to specify that the CSV file has a header row, the field separator is ;, and the first column should be interpreted as numeric and the second column as a factor. We also use the quote and na options to specify that single quotes should be used as the quote character, and that missing values should be represented as empty strings.

In summary, reading and writing data from/to a CSV file in R is a common task in data analysis and modeling. R provides several functions for handling CSV.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic R interview — then scores it.
📞 Practice R — free 15 min
📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

All 100 R questions · All topics