2

有没有人尝试将 R 对象转换为文本文件?我有从 Seqmeta 包创建的 R 对象,并试图将其转换为文本文件

load("variant.Rdata")
write.csv(variants, file="variants.csv")
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
  cannot coerce class ""seqMeta"" to a data.frame

然后我尝试了

dump(variant, file="variant_file")
Error in FUN(X[[1L]], ...) : invalid first argument

如何将数据转换为 csv 格式?

4

1 回答 1

1

如果您需要保存通用 R 对象,您通常会使用saveRDS(这比使用save单个对象更可取)。如果文件必须是 ASCII,, saveRDS, save, 并且serialize都带有参数ascii=TRUE, 并serialize产生最易读的输出。

如果您需要文件是人类可读的 ASCII,那么您在正确的轨道上使用dump. 您需要的唯一更改是将对象dump名称作为其参数:

dump('variant', file="variant_file")

以下是随机神经网络的每个选项的输出类型示例:

> serialize(model,connection = (con<-file('nn','w')),ascii = TRUE)
NULL
> close(con)
> writeLines(readLines('nn',n=10))
A
2
197122
131840
787
13
14
3
11
2  


> dump('model',file='nn2')
> writeLines(readLines('nn2',n=20))
model <-
structure(list(n = c(11, 2, 1), nunits = 15L, nconn = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 38), conn = c(0, 1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 0, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), nsunits = 14, 
    decay = 0.3, entropy = FALSE, softmax = FALSE, censored = FALSE, 
    value = 198533.158122471, wts = c(19.0054841585536, 2.23487502441887, 
    4.18125434527288, -6.18031807633724, 1.74046449146894, -12.6452914163319, 
    4.36482412477615, -13.8535055297175, -2.86352652003103, -8.26639784261127, 
    -12.6981294199594, 4.6795318281078, 21.4576975495026, 2.3237431117074, 
    6.11977661730472, -4.72698798667361, -0.850474104016898, 
    -13.6710933522033, 4.09687948057462, -12.53360483838, -4.62974366307091, 
    -10.2276751641509, -11.5384259593477, 0.393593676803197, 
    11.9212104035313, -23.389751188319, 20.3888183920005, -0.900624040655219, 
    1.0351758513862, -0.343608722841881, -1.66543302778472, -1.29803652137646, 
    0.242127071364901, 0.994889406131462, -2.30836053849945, 
    0.190090309268229, -0.35494347864244, -0.201148348574871), 
    convergence = 0L), .Names = c("n", "nunits", "nconn", "conn", 
"nsunits", "decay", "entropy", "softmax", "censored", "value", 
"wts", "convergence"), class = "nnet")
于 2016-07-14T19:41:36.510 回答