Converting Rdata files to CSV - Error in data.frame arguments imply differing number of rows

前端 未结 2 1375
孤独总比滥情好
孤独总比滥情好 2021-01-16 05:40

I\'m trying to use the R code from this answer to convert a bunch of rdata files to CSV.

resave <- function(file){
  e <- new.env(parent = emptyenv())
         


        
相关标签:
2条回答
  • 2021-01-16 06:06

    That answer was designed to handle object of class-'data.frame'. You only have an object of class-'list' which happens to have items that are dataframes. So there isn't an object with the name "2" in you workspace but there is an element in the 'dataa'-list that is named "2" and all of the other elements appear to also be dataframes, so why not use:

    lapply( names(dataa), function(nam) write.csv( data[[nam]], file=paste0(nam, ".Rdata") ) )
    
    0 讨论(0)
  • 2021-01-16 06:20

    I'll vote for the other answer, but here's some almost working code:

    resave <- function(file){
      e <- new.env(parent = emptyenv())
      load(file, envir = e)
      obj <- get('dataa', envir =e)
      lapply( names(obj), function(nam) {
        write.csv( obj[[nam]], file=paste(nam, ".csv", sep="") )
        cat(sprintf('%s.csv
    ', nam) )
        }
       )
    }
    resave("indiv8-hmmprob.RData")
    

    Here's the output. which works but it's throwing in some wierd printed stuff at the end, the [[1]] NULL, etc.

    2.csv
    3.csv
    4.csv
    X.csv
    [[1]]
    NULL
    
    [[2]]
    NULL
    
    [[3]]
    NULL
    
    [[4]]
    NULL
    
    0 讨论(0)
提交回复
热议问题