I am a novice in R and I am trying to convert .Rdata format file into comma delimited text file format. Can someone help me out regarding this?
Asked
Active
Viewed 5.0k times
2 Answers
21
load("yourData.RData")
ls() #returns a list of all the objects you just loaded (and anything else in your environment)
write.csv(theItemOfInterestFromYourDRadataFileAsThereMayBeMoreThanOneThingInthere,
file="yourCSV.csv")

Chase
- 67,710
- 18
- 144
- 161
-
1`write.csv` will have to use the name of the variable that you want to write to csv in the call. It may not necessarily be "yourData" – Brandon Bertelsen Nov 02 '12 at 04:59
-
Hey its writing only one record into the csv file.Just the name of the data set. – Teja Nov 02 '12 at 05:00
-
@SOaddict; what does `str(yourData)` return? – IRTFM Nov 02 '12 at 05:40
10
An .RData
file can contain more than 1 object of any class.
If your file contains more than 1 object of data.frame
-like class, then the following should work
resave <- function(file){
e <- new.env(parent = emptyenv())
load(file, envir = e)
objs <- ls(envir = e, all.names = TRUE)
for(obj in objs) {
.x <- get(obj, envir =e)
message(sprintf('Saving %s as %s.csv', obj,obj) )
write.csv(.x, file = paste0(obj, '.csv'))
}
}
resave('yourData.RData')
You can change the call to write.csv
to do what you want. If your objects won't behave nicely with write.csv
, then you shouldn't be trying this.

mnel
- 113,303
- 27
- 265
- 254
-
I'm getting this error: `Error in data.frame(`2` = list(pos = c(6506L, 6601L, 21801L, 21811L, 21902L, : arguments imply differing number of rows: 7670, 9729, 114, 2422 Calls: resave ... as.data.frame -> as.data.frame.list -> eval -> eval -> data.frame` – Greg May 01 '14 at 18:00
-
I asked here too: http://stackoverflow.com/questions/23413728/converting-rdata-files-to-csv-error-in-data-frame-arguments-imply-differing-nu – Greg May 01 '14 at 18:08