We can export a DataFrame to csv file in R by using the following script format:
- write.csv(DataFrame, "Path_to_csv_file", row.names = FALSE)
To include the row names, we simply change row.names to TRUE.
Example
Let's create a DataFrame in R with three columns named Names, Age, and Location:
- df <- data.frame(Names = c("Sunta", "Gili", "Sanje", "Allu", "Suru", "Mbighii"), Age = c(34, 25, 50, 32, 45, 39), Location = c("Dar es Salaam", "Singida", "Tabora", "Ruvuma", "Manyara", "Pwani"))
We then convert the DataFrame to csv file as follows:
- write.csv(df, "Path_to_csv_file", row.names = FALSE)
Combining all together, we have the following complete script:
- df <- data.frame(Names = c("Sunta", "Gili", "Sanje", "Allu", "Suru", "Mbighii"), Age = c(34, 25, 50, 32, 45, 39), Location = c("Dar es Salaam", "Singida", "Tabora", "Ruvuma", "Manyara", "Pwani"))
- write.csv(df, "Records.csv", row.names = TRUE)