I've read a .csv file into R, ran some code to edit it and now want to export this data frame as a .csv This so I can read it into R again for some different editing. How do I do this the correct way (meaning clean code)
Export R data to csv
Asked Answered
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:"
Assuming your data is called df
write.csv(df, "specify_path_and_file_name.csv")
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
df.csv(total, my_documents_path, row.names = FALSE)
Fantastic. I like the way you handled this. I always get frustrated with the path especially when it's long and contains goofy characters. –
Conlan
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!
The answer is correct. But essentially repeats a previous one. –
Festoonery
© 2022 - 2025 — McMap. All rights reserved.