Saving R dataframe from script - r

It should be very simple, but for now cannot figure it out. Say I create a generic dataframe with some data in a loop, let's call it df.
Now I want to assign a specific name to it and want to save it to specific destination. I generate two character variables - filename and file_destination and try to use the following in the script code:
assign(filename, df)
save(filename, file = file_destination)
Of course it save just a string with a name in the file and not the actual data.
How do i save the dataframe created via assign(filename,df)?

Try save(list=filename,file=file_destination). Also, use better names for your variables. filename for an object which is not a file name is very odd.
Put this as answer, to ensure other people find it easily.

Related

How to cope with variable names that include forward slash ("/")?

I am writing a script in R to perform automated analysis of some files we receive as output from a lab instrument.
The final output of my script is a wide .csv file with multiple headers, and to better suit the end user, the headers are properly formatted as they would expect.
For example, one of the parameter is the following: 'dm/ds_max'.
Everything works fine when this parameter is written as variable in the table header or when I have to use this parameter as variable in ggplot (for example to automatically generate a plot title with dm/ds_max), but becomes problematic when I am trying to use the name of the variable in the paste() command to save the graph.
As you can imagine, the / in the paste() is interpreted as directory delimiter and the files are then saved in the wrong folder.
For example, when the variable name is colour, the plot are properly saved in the proper folder as img/colour.jpg. When the variable name is dm/ds_max, the file is saved as img/dm/ds_max.jpg, putting it in a non-existing folder.
Is there a way -beside changing the name of the variable- to bypass this conflict between writing tables in a nice human readable format and making automated plots with the same variable?
Thank you

How to write table on Juliabox?

I define a DataFrame named data and want to write it into .csv file. I used writetable("result_data.csv", data) but it doesn't work.
This is the dataframe
error details
To write a data frame to a disk you should use the CSV.jl package like this (also make sure that you have write right to the directory you want to save the file on Juliabox):
using CSV
CSV.write("result_data.csv", data)
If this fails then please report back in the comment I will investigate it further.

Using names() and make.names() inside a loop

I am reading some files into R with a "for loop" and naming them based on the looping index. I have used the assign fucntion to dynamically name one data frame for each file being read as follows:
for (i in 1:10){
assign(paste0("df",i),read_csv(paste0("file",i,".csv")))
}
This works fine. However, while inside the for loop, I also want to do some other operations to the data frame, but I dont know how to refer to it given that the name is changing as the loop progresses.
I want to add to the loop something along the lines of:
names(paste0("df",i)) <- make.names(names(paste0("df",i)))
I want to execute the above command but I don't know how to properly pass the changing data frame name to the names() function.
Thanks in advance,
MR

Get a list from R string that contains a csv

for one of my projects I will need to import the dataset (csv-File) outside of R and then assign it from the Ruby side of the project in R (this will be done with rinruby and already works).
In my R-Script I now need to create a list out of that csv file.
The variable contains an escaped string that contains the original csv.
data <- "\"\",\"futime\",\"fustat\",\"age\",\"resid.ds\",\"rx\",\"ecog.ps\"\n\"1\",59,1,72.3315,2,1,1\n\"2\",115,1,74.4932,2,1,1\n\"3\",156,1,66.4658,2,1,2\n\"4\",421,0,53.3644,2,2,1\n\"5\",431,1,50.3397,2,1,1\n\"6\",448,0,56.4301,1,1,2\n\"7\",464,1,56.937,2,2,2\n\"8\",475,1,59.8548,2,2,2\n\"9\",477,0,64.1753,2,1,1\n\"10\",563,1,55.1781,1,2,2\n\"11\",638,1,56.7562,1,1,2\n\"12\",744,0,50.1096,1,2,1\n\"13\",769,0,59.6301,2,2,2\n\"14\",770,0,57.0521,2,2,1\n\"15\",803,0,39.2712,1,1,1\n\"16\",855,0,43.1233,1,1,2\n\"17\",1040,0,38.8932,2,1,2\n\"18\",1106,0,44.6,1,1,1\n\"19\",1129,0,53.9068,1,2,1\n\"20\",1206,0,44.2055,2,2,1\n\"21\",1227,0,59.589,1,2,2\n\"22\",268,1,74.5041,2,1,2\n\"23\",329,1,43.137,2,1,1\n\"24\",353,1,63.2192,1,2,2\n\"25\",365,1,64.4247,2,2,1\n\"26\",377,0,58.3096,1,2,1"
And I would like to convert this to a R-List.
So my approach is basically to call read.csv(data_as_string) but unfortunately the signature is read.csv(file_where_data_lies).
How can this be done?
Thanks so much!
As Therkel mentioned above, myfunc(file = textConnection(data)) did exactly what I was about to do. Thanks!

Command to use with easy way the insert of R dataframe

I have a dataframe loaded successfully in R.
I would like to give the data of df to someone else to use them with quick and easy way without need to load again the file into a df.
Which is the command to give the whole data of df (not the str())
You can save the file into a .RData using save or save.image, depending on your needs. First one will save specific objects while the latter will dump the whole workspace to a file. This method has the advantage of working on probably any R object.
Another option is as #user1945827 mentioned, using dput which will produce a string that is parseable into another R session. This will not work for complex (like S4) objects.

Resources