Get a list from R string that contains a csv - r
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!
Related
Save text files with a different extension in r
I'm trying to write out an R output to a text file that is not saved as .txt but as some other unique identifier (for example .prt). I know that's possible with matlab, but I don't know how to get that to work with R. I can't find any package to do that, and when I try to specify the extension in the file name it give me an error and doesn't save. Any idea would be greatly welcome! Thank you.
Unless you are using some specialized package, a lot of standard R functions for writing data to files have a file= parameter (or similar) to let you specify whatever the filename (and extension) you want. For example: dummy.data <- matrix(rnorm(25),ncol=5) ### in reality you could just write file="dummyfile.prt" as one string ### but for demonstration purposes, you can use paste0() or paste(,sep='') ### to create a new file name using some variable prefix and your extension ### ".prt" ### sep='\t' makes the output tab-delimited write.table(dummy.data,file=paste0("dummyfile",".prt"),sep='\t')
Importing data from Excel to vector in R
I am a novice in R and I have been having some trouble trying to get R and Excel to cooperate. I have written a code that makes it able to compare two vectors with each other and determine the differences between them: data.x<-read.csv(file.choose(), header=T) data.y<-read.csv(file.choose(), header=T) newdata.x<-grep("DAG36|G379",data.x,value=TRUE,invert=TRUE) newdata.x newdata.y<-grep("DAG36|G379",data.y,value=TRUE,invert=TRUE) newdata.y setdiff(newdata.x,newdata.y) setdiff(newdata.y,newdata.x) The data I want to transfer from Excel to R is a long row of numbers placed as so: “312334-2056”, “457689-0932”, “857384-9857”,…., There are about 350 of these numbers placed in their own separate cell along a single row. I used the command: = """" & A1 & """" To put double quotes around every number in order for R to read it properly. At first I tried to simply copy/paste the data directly into a vector in R, but it's as if R won’t read it as a single row of data and therefore splits it up. I also tried to save the excel file as a CSV file but that didn’t work either. Lastly I tried to open it directly in to R using the command: data.x<- read.csv(file.choose(), header=T) But as I type in: data.x and press enter it simply says: <0 rows> (or 0-lenghts row.names) I simply can’t figure out what I’m doing wrong. Any help would be greatly appreciated.
It's hard to access without a reproducible example, but you should be able to transpose the Excel file into a single column. Then import using read_csv from the readr package. Take a look at the tidyverse package, which will contain some great tools to import and work with this type of data.
I use https://github.com/tidyverse/readxl/. It makes it easy to maintain formatting from excel into type safe tibbles. If you can share some sample data a working solution can be generated.
Saving R dataframe from script
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.
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.
how to read a file to data frame and print some colums in R
I got a question about reading a file into data frame using R. I don't understand "getwd" and "setwd", do we must do these before reading the files? and also i need to print some of the columns in the data frame, and only need to print 1 to 30,how to do this? Kinds regards
getwd tells you what your current working directory is. setwd is used to change your working directory to a specified path. See the relevant documentation here or by typing ? getwd or ? setwd in your R console. Using these allows you to shorten what you type into, e.g., read.csv by just specifying a filename without specifying its full path, like: setwd('C:/Users/Me/Documents') read.csv('myfile.csv') instead of: read.csv('C:/Users/Me/Documents/myfile.csv')