The “write.csv” Function in R
Package: Base R (No specific package)
Purpose: To write a data frame to a CSV file.
General Class: Data Output
Required Argument(s):
x: A data frame to be written to the CSV file.
file: The file path or connection where the CSV file should be written.
Notable Optional Arguments:
row.names: Logical value indicating whether the row names should be included in the output. The default is TRUE.
Example:
# Example usage
# Create a sample data frame
data <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 22),
Score = c(95, 88, 75)
)
# Write the data frame to a CSV file. Don’t forget the “.csv”!
write.csv(data, file = "output.csv", row.names = FALSE)In this example, the write.csv function from the base R package is used to write the data frame data to a CSV file named “output.csv”. The row.names argument is set to FALSE to exclude row names in the output.