有没有人试图将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格式?
答案 0 :(得分: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")