Export R data to csv
Asked Answered
K

4

6

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)

Karykaryl answered 25/5, 2020 at 16:5 Comment(0)
B
6

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:"

Bogtrotter answered 26/5, 2020 at 8:38 Comment(0)
O
3

Assuming your data is called df

write.csv(df, "specify_path_and_file_name.csv")
Overton answered 25/5, 2020 at 16:7 Comment(0)
T
0

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)
Tishtisha answered 13/7, 2024 at 5:8 Comment(1)
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
S
0

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!

Straggle answered 3/9, 2024 at 9:26 Comment(1)
The answer is correct. But essentially repeats a previous one.Festoonery

© 2022 - 2025 — McMap. All rights reserved.