user13608970
user13608970

Reputation: 197

Export R data to csv

I've read a .csv file into R, ran some code to edit it and now want to export this data frame as .csv, to read it in later again or whatsoever. How can I accomplish this in "clean code"?

Upvotes: 11

Views: 108011

Answers (4)

Melanie Shebel
Melanie Shebel

Reputation: 2917

The other answers didn't work for me so I played around with it. This worked for me. I get frustrated by paths, so I set a variable to my path so I can call it repeatedly:

# This defines the path to the My Documents folder
my_documents_path <- file.path(Sys.getenv("USERPROFILE"), "Documents", "df.csv")


# This creates the file and saves it to the above path
write.csv(df, my_documents_path, row.names = FALSE)

Here it is in action: How to export R data to csv.

Upvotes: 1

Jose Rojas
Jose Rojas

Reputation: 1

I like creating a project file with RStudio, inside a specific folder, and in that case, you can set a "data" folder, and save it by using

write.csv(dataframe,"./data/filename.csv", row.names = T)

This way your data will be contained to each specific project.

Best regards!

Upvotes: 0

Julia
Julia

Reputation: 117

write.csv(dataframe,"~/Downloads/filename.csv", row.names = FALSE)

Different computers use different directions for slashes (\or/)and on a mac, I typically have to do the "~/ " at the beginning for windows it is typically "C:"

Upvotes: 8

Ahorn
Ahorn

Reputation: 3876

Assuming your data is called df

write.csv(df, "specify_path_and_file_name.csv")

Upvotes: 7

Related Questions