简体   繁体   English

将Rdata文件转换为CSV - data.frame参数中的错误意味着行数不同

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

I'm trying to use the R code from this answer to convert a bunch of rdata files to CSV. 我正在尝试使用此答案中的R代码将一堆rdata文件转换为CSV。

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')

However on one of the files 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

I tried searching for the error message but I can't really make heads or tails of it. 我试着搜索错误信息,但我无法真正做出它的正面或反面。

Was that rdata file created improperly somehow? 该rdata文件是否以某种方式不正确地创建了?

Is there a better way I should convert arbitrary Rdata files to CSV? 有没有更好的方法将任意Rdata文件转换为CSV? (I Don't know the names of the objects inside the files ahead of time.) (我提前知道文件中对象的名称。)

Update: 更新:

Here's what I'm seeing in that rdata file. 这是我在rdata文件中看到的内容。 If it's any help?? 如果有任何帮助? (Keep in mind I can't really edit the rdata files so I'm trying to figure out something that will convert them to CSV as is.) (请记住,我无法真正编辑rdata文件,因此我正在尝试找出将它们按原样转换为CSV的内容。)

> load("indiv8-hmmprob.RData")
> ls()
[1] "dataa"
> write.csv(dataa, file="greg.csv")
Error in data.frame(`2` = list(pos = c(6506L, 6601L, 21801L, 21811L, 21902L,  : 
  arguments imply differing number of rows: 7670, 9729, 114, 2422
> names(dataa)
[1] "2" "3" "4" "X"
> str(dataa)
List of 4
 $ 2:'data.frame':  7670 obs. of  23 variables:
  ..$ pos              : int [1:7670] 6506 6601 21801 21811 21902 21931 22487 24071 26674 26713 ...
  ..$ ref              : chr [1:7670] "C" "A" "G" "A" ...
  ..$ cons             : chr [1:7670] "T" "T" "A" "G" ...
  ..$ reads            : chr [1:7670] "ttt" "tttt" "AAAAA" "GGGGG" ...
  ..$ quals            : chr [1:7670] "FBB" "IIIB" "IFIII" "FFIII" ...
  ..$ A                : int [1:7670] 0 0 5 0 0 0 1 0 0 1 ...
  ..$ C                : int [1:7670] 0 0 0 0 0 0 0 0 2 0 ...
  ..$ G                : int [1:7670] 0 0 0 5 11 0 0 0 0 0 ...
  ..$ T                : int [1:7670] 3 4 0 0 0 10 0 2 0 0 ...
  ..$ N                : int [1:7670] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ bad              : chr [1:7670] NA NA NA NA ...
  ..$ par1ref          : chr [1:7670] "C" "A" "G" "A" ...
  ..$ par2ref          : chr [1:7670] "T" "T" "A" "G" ...
  ..$ read             : Factor w/ 8397 levels "1","2","3","4",..: 2 2 3 3 3 3 4 7 9 9 ...
  ..$ count            : int [1:7670] 3 4 5 5 11 10 1 2 2 1 ...
  ..$ read_allele      : chr [1:7670] "T" "T" "A" "G" ...
  ..$ Pr(y| par1/par1 ): num [1:7670] 9.30e-04 5.69e-04 3.47e-04 1.42e-04 1.90e-08 ...
  ..$ Pr(y| par1/par2 ): num [1:7670] 4.58e-02 1.64e-02 2.41e-03 4.09e-03 8.89e-07 ...
  ..$ Pr(y| par2/par2 ): num [1:7670] 1.61e-01 8.40e-02 8.94e-03 2.09e-02 3.29e-06 ...
  ..$ est              : int [1:7670] 3 3 3 3 3 3 3 3 3 3 ...
  ..$ Pr( par1/par1 |y): num [1:7670] 4.67e-25 2.25e-27 1.98e-31 2.93e-32 2.82e-34 ...
  ..$ Pr( par1/par2 |y): num [1:7670] 2.95e-11 2.86e-11 2.49e-14 1.98e-14 1.08e-14 ...
  ..$ Pr( par2/par2 |y): num [1:7670] 1 1 1 1 1 ...
  ..- attr(*, "badpos")= int [1:11386] 21900 21905 22840 24029 27149 27170 28024 42187 46927 46990 ...
 $ 3:'data.frame':  9729 obs. of  23 variables:
  ..$ pos              : int [1:9729] 6001 22537 25304 27228 28817 28842 30540 48903 48938 48943 ...
  ..$ ref              : chr [1:9729] "A" "A" "A" "C" ...
  ..$ cons             : chr [1:9729] "A" "G" "T" "C" ...
  ..$ reads            : chr [1:9729] "," "GGG" "TTTTT" "," ...
  ..$ quals            : chr [1:9729] "F" "BBB" "BFFFF" "B" ...
  ..$ A                : int [1:9729] 1 0 0 0 0 0 0 0 0 0 ...
  ..$ C                : int [1:9729] 0 0 0 1 1 0 0 0 0 1 ...
  ..$ G                : int [1:9729] 0 3 0 0 0 0 0 0 0 0 ...
  ..$ T                : int [1:9729] 0 0 5 0 0 1 1 1 1 0 ...
  ..$ N                : int [1:9729] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ bad              : chr [1:9729] NA NA NA NA ...
  ..$ par1ref          : chr [1:9729] "A" "A" "A" "C" ...
  ..$ par2ref          : chr [1:9729] "G" "G" "T" "T" ...
  ..$ read             : Factor w/ 10640 levels "1","2","3","4",..: 1 3 4 5 7 7 8 10 10 10 ...
  ..$ count            : int [1:9729] 1 3 5 1 1 1 1 1 1 1 ...
  ..$ read_allele      : chr [1:9729] "A" "G" "T" "C" ...
  ..$ Pr(y| par1/par1 ): num [1:9729] 0.969856 0.002707 0.000372 0.969639 0.969856 ...
  ..$ Pr(y| par1/par2 ): num [1:9729] 0.48995 0.0567 0.00228 0.48988 0.48995 ...
  ..$ Pr(y| par2/par2 ): num [1:9729] 0.01005 0.26071 0.00798 0.01012 0.01005 ...
  ..$ est              : int [1:9729] 1 3 3 1 1 1 1 3 1 3 ...
  ..$ Pr( par1/par1 |y): num [1:9729] 2.18e-10 2.82e-11 2.67e-11 2.65e-11 2.63e-11 ...
  ..$ Pr( par1/par2 |y): num [1:9729] 0.688 0.688 0.688 0.688 0.688 ...
  ..$ Pr( par2/par2 |y): num [1:9729] 0.312 0.312 0.312 0.312 0.312 ...
  ..- attr(*, "badpos")= int [1:13707] 25259 27250 27810 27880 27888 28836 30507 48975 55998 58734 ...
 $ 4:'data.frame':  114 obs. of  23 variables:
  ..$ pos              : int [1:114] 21119 21194 42177 64136 64146 74463 74465 74521 79860 79884 ...
  ..$ ref              : chr [1:114] "T" "T" "C" "C" ...
  ..$ cons             : chr [1:114] "C" "A" "Y" "Y" ...
  ..$ reads            : chr [1:114] "cCCCCCCCCCCCCCcc" "aa" "T" "T" ...
  ..$ quals            : chr [1:114] "IBFFBFBFFFFFFBBF" "FF" "F" "I" ...
  ..$ A                : int [1:114] 0 2 0 0 0 0 0 0 2 0 ...
  ..$ C                : int [1:114] 16 0 0 0 1 0 1 1 0 0 ...
  ..$ G                : int [1:114] 0 0 0 0 0 0 0 0 0 2 ...
  ..$ T                : int [1:114] 0 0 1 1 0 1 0 0 0 0 ...
  ..$ N                : int [1:114] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ bad              : chr [1:114] NA NA NA NA ...
  ..$ par1ref          : chr [1:114] "T" "T" "C" "C" ...
  ..$ par2ref          : chr [1:114] "C" "A" "T" "T" ...
  ..$ read             : Factor w/ 130 levels "1","2","3","4",..: 3 3 6 8 8 10 10 10 14 14 ...
  ..$ count            : int [1:114] 16 2 1 1 1 1 1 1 2 2 ...
  ..$ read_allele      : chr [1:114] "C" "A" "T" "T" ...
  ..$ Pr(y| par1/par1 ): num [1:114] 9.34e-12 4.99e-03 1.00e-02 1.00e-02 1.00e-02 ...
  ..$ Pr(y| par1/par2 ): num [1:114] 4.56e-10 2.33e-01 4.90e-01 4.90e-01 4.90e-01 ...
  ..$ Pr(y| par2/par2 ): num [1:114] 9.04e-10 8.61e-01 9.70e-01 9.70e-01 9.70e-01 ...
  ..$ est              : int [1:114] 3 3 3 3 3 3 3 3 3 3 ...
  ..$ Pr( par1/par1 |y): num [1:114] 6.50e-24 4.49e-24 1.10e-26 2.53e-31 1.51e-31 ...
  ..$ Pr( par1/par2 |y): num [1:114] 1.56e-10 1.54e-10 5.77e-11 6.60e-12 6.59e-12 ...
  ..$ Pr( par2/par2 |y): num [1:114] 1 1 1 1 1 ...
  ..- attr(*, "badpos")= int [1:73] 16621 16638 34177 34180 74448 74464 78954 79664 80045 94170 ...
 $ X:'data.frame':  2422 obs. of  23 variables:
  ..$ pos              : int [1:2422] 34630 45427 70728 70744 166279 189892 207276 207424 213012 232229 ...
  ..$ ref              : chr [1:2422] "T" "G" "G" "C" ...
  ..$ cons             : chr [1:2422] "T" "G" "G" "C" ...
  ..$ reads            : chr [1:2422] "a" "..." "^F." "." ...
  ..$ quals            : chr [1:2422] "<" "IIF" "F" "B" ...
  ..$ A                : int [1:2422] 1 0 0 0 0 0 0 4 0 1 ...
  ..$ C                : int [1:2422] 0 0 0 1 1 0 2 0 0 0 ...
  ..$ G                : int [1:2422] 0 3 1 0 0 1 0 1 1 0 ...
  ..$ T                : int [1:2422] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ N                : int [1:2422] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ bad              : chr [1:2422] NA NA NA NA ...
  ..$ par1ref          : chr [1:2422] "T" "G" "G" "C" ...
  ..$ par2ref          : chr [1:2422] "A" "A" "A" "T" ...
  ..$ read             : Factor w/ 2433 levels "1","2","3","4",..: 1 6 8 8 13 16 18 18 19 20 ...
  ..$ count            : int [1:2422] 1 3 1 1 1 1 2 5 1 1 ...
  ..$ read_allele      : chr [1:2422] "A" "G" "G" "C" ...
  ..$ Pr(y| par1/par1 ): num [1:2422] 0.0105 0.2732 0.9699 0.9696 0.9699 ...
  ..$ Pr(y| par1/par2 ): num [1:2422] 0.4895 0.0642 0.49 0.4899 0.49 ...
  ..$ Pr(y| par2/par2 ): num [1:2422] 0.96856 0.00134 0.01005 0.01012 0.01005 ...
  ..$ est              : int [1:2422] 3 1 1 1 1 1 1 1 1 1 ...
  ..$ Pr( par1/par1 |y): num [1:2422] 1 1 1 1 1 ...
  ..$ Pr( par1/par2 |y): num [1:2422] 3.70e-08 2.00e-08 1.06e-08 1.06e-08 1.59e-09 ...
  ..$ Pr( par2/par2 |y): num [1:2422] 3.70e-18 9.35e-20 2.36e-23 2.23e-23 3.26e-26 ...
  ..- attr(*, "badpos")= int [1:2327] 34776 45619 86591 86607 166220 193151 193159 212997 232221 233552 ...

That answer was designed to handle object of class-'data.frame'. 该答案旨在处理类''data.frame'的对象。 You only have an object of class-'list' which happens to have items that are dataframes. 你只有一个class-'list'的对象恰好有数据帧的项目。 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: 因此,工作区中没有名称为“2”的对象,但“dataa”列表中有一个名为“2”的元素,所有其他元素看起来也都是数据框,所以为什么不使用:

lapply( names(dataa), function(nam) write.csv( data[[nam]], file=paste0(nam, ".Rdata") ) )

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. 哪个有效,但它最后投入了一些奇怪的印刷品,[[1]] NULL等。

2.csv
3.csv
4.csv
X.csv
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 data.frame 中的错误:参数意味着不同的行数 - Error in data.frame: arguments imply differing number of rows data.frame() 参数中的错误意味着不同的行数:1, 0 - Error in data.frame() arguments imply differing number of rows: 1, 0 data.frame 中的错误(...:参数意味着不同的行数:0、60 - Error in data.frame( ... : arguments imply differing number of rows: 0, 60 部署 Shiny 应用程序时出错:警告:data.frame 中的错误:参数意味着不同的行数:2, 20268 - Error deploying Shiny app: Warning: Error in data.frame: arguments imply differing number of rows: 2, 20268 cbind 错误 data.frame 中的错误(...,check.names = FALSE):arguments 暗示不同的行数:0、1 - cbind error Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 1 r-data.frame(data,source = namelist)中的错误:参数暗示行数不同:3,4-在predict()中 - r - Error in data.frame(data, source = namelist) : arguments imply differing number of rows: 3, 4 - in predict() R data.frame(...,check.names = FALSE)中的错误:参数暗示行数不同:5、3 - R Error in data.frame(…, check.names = FALSE) : arguments imply differing number of rows: 5, 3 data.frame(…,check.names = FALSE)中的错误:参数暗示行数不同:0、1 - Error in data.frame(…, check.names = FALSE) : arguments imply differing number of rows: 0, 1 data.frame(…,check.names = FALSE)中的错误:参数表示行数不同:6790、6771 - Error in data.frame(…, check.names = FALSE) : arguments imply differing number of rows: 6790, 6771 闪亮的应用程序:警告:data.frame中的错误:参数暗示行数不同:2,0 - Shiny app: Warning: Error in data.frame: arguments imply differing number of rows: 2, 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM