R Func Write Csv
# R write.csv() Function - Write CSV File
[ R Language Examples](https://example.com/r/r-examples.html)
The R write.csv() function is used to save a data frame as a CSV file.
CSV format can be opened directly in tools like Excel, and is the most commonly used format for data output.
The write.csv() function syntax is as follows:
write.csv(x, file, row.names = TRUE, fileEncoding = "")
**Parameter Description:**
* **x** The data frame or matrix to write.
* **file** The output file path.
* **row.names** Whether to save row names, default is TRUE. It is recommended to set to FALSE to avoid extra columns.
## Example
# Create processed data
df<-data.frame(
product =c("productA", "productB", "productC"),
sales =c(120, 85, 150),
price =c(9.9, 15.8, 5.6)
)
# Write to CSV (without row names)
write.csv(df, "temp_products.csv", row.names= FALSE)
# Verify: read back and display
data<-read.csv("temp_products.csv")
print("Saved and read back data:")
print(data)
# View the raw content written
lines<-readLines("temp_products.csv")
print("CSV content:")
print(lines)
Executing the above code outputs:
"Saved and read back data:" product sales price1 productA 120 9.92 productB 85 15.83 productC 150 5.6 "CSV content:" "\"product\",\"sales\",\"price\"" "\"productA\",120,9.9" "\"productB\",85,15.8" "\"productC\",150,5.6"
[ R Language Examples](https://example.com/r/r-examples.html)
YouTip