3

How do I export a data frame from R (the file is in Global Environment) to some folder in desktop? I have created some data frames in R and need to export to the linux operating system. That's why I want to export the data frame to desktop/documents and later export to the Linux.

user438383
  • 5,716
  • 8
  • 28
  • 43
user28725
  • 103
  • 1
  • 2
  • 8
  • What do you mean by "file"? Do you mean a data frame? A matrix? A `spatialPointsDataFrame`? A different R object? What do you want it saved as, a PDF, CSV, jpg, rdata file? – Gregor Thomas Mar 18 '16 at 18:05
  • I have bunches (more than 500) files in RSTUDIO. I just want to export the file to some folder in desktop/documents? – user28725 Mar 18 '16 at 18:12
  • It's a data frame. – user28725 Mar 18 '16 at 18:15
  • Do you want to export all your data frames combined into one file, or do you want separate files for each data frame? What format do you want them in: common options for data frames would be RDS files (for R only), CSV files (very common), TSV files (not as common, but more flexible), Excel files... – Gregor Thomas Mar 18 '16 at 18:20
  • 2
    And please don't call objects in R "files". Files are on your computer. Your goal seems to be to create files. Data frames in R are R objects of the class data frame - they are not files. – Gregor Thomas Mar 18 '16 at 18:21
  • Ok, I will call object and want separate file for each data frame. – user28725 Mar 18 '16 at 18:26
  • I just want to get those objects to some folder in desktop. – user28725 Mar 18 '16 at 18:28
  • 2
    Can you read my question and read your comment and see that you didn't answer at all? **QUESTION 1** In this folder on your desktop how many files do you want? **(a)** 1 file with all your data. **(b)** Many files with 1 data frame saved per file. **QUESTION 2** What *kind* of file do you want to save? **(a)** A file readable pretty much only by R (e.g., an RDS file). **(b)** A commonly supported file such as a CSV or TSV/TXT file. **(c)** An Excel file. **(d)** Some other file. – Gregor Thomas Mar 18 '16 at 18:33
  • a) separate file ,b)I want txt file – user28725 Mar 18 '16 at 18:47

3 Answers3

0

The Save function can do that, just specify the path you want to export the data. Change your directory in Rstudio to that folder in the desktop or else save it somewhere and do a cp linux command.

Save Function Information

Save Data Frame (Stack Overflow)

saving a data file in R

Community
  • 1
  • 1
Cody Glickman
  • 514
  • 1
  • 8
  • 30
  • i already generated the files (like 1000 files-data frame). I can see the file in global environment. I just want to get those files to some folder in desktop. – user28725 Mar 18 '16 at 18:20
0

To write a data frame to a text file, the command is write.table. I'll let you read the help (?write.table) to see all the options, but a sample usage is

write.table(x = mtcars, file = "C:/exported_mtcars.txt")

This will write the data frame called mtcars to a file called exported_mtcars.txt on my C drive. The default is to use spaces to separate columns. If you want tab-separations, specify sep = "\t".

You may want to simply set your working directory to the folder you've created (setwd("C:/Users/...your filepath.../Desktop/your_folder")). Then you can just specify, e.g., file = "file1.txt" for the file names in write.table.

As far as writing multiple data frames to multiple files, I strongly recommend working with lists of data frames. I'll refer you to my answer here on the subject. See especially the section I didn't put my data in a list :( I will next time, but what can I do now?. You can then pretty easily use a for loop to save all your data frames to files using write.table.

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • I do have a data frame as : quan_2009_3_1_6 , quan_2009_3_1_12, quan_2009_3_1_18, quan_2009_3_1_24,........quan_2009_3_1_168. Could you please help me in putting the loop and extracting these files? – user28725 Mar 19 '16 at 20:53
-1

You have to specify here the route where you want to export a complete image of your current working environment:

save.image("~/User/RstudioFiles/dataset.RData")

Hope it works!

adrian1121
  • 904
  • 2
  • 9
  • 21