How to Convert a DataFrame to csv File in R

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

We can export a DataFrame to csv file in R by using the following script format:

  1. 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:

  1. 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:

  1. write.csv(df, "Path_to_csv_file", row.names = FALSE)

Combining all together, we have the following complete script:

  1. 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"))
  2. write.csv(df, "Records.csv", row.names = TRUE)

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “R”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 7 guests