How to convert .Rdata format into text file format

2019-02-12 11:20发布

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?

2条回答
何必那么认真
2楼-- · 2019-02-12 11:26
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")
查看更多
小情绪 Triste *
3楼-- · 2019-02-12 11:27

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.

查看更多
登录 后发表回答