Reputation: 195
Has anyone tried to convert R objects to text files ? I have R objects created from Seqmeta package and am trying to convert it to text file
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
Then I tried
dump(variant, file="variant_file")
Error in FUN(X[[1L]], ...) : invalid first argument
How can I convert the data to csv format ?
Upvotes: 2
Views: 6483
Reputation: 4682
If you need to save generic R objects, you would typically use saveRDS
(this is preferable to using save
for a single object). If the file must be ASCII, saveRDS
,save
, and serialize
all have the parameter ascii=TRUE
, with serialize
producing the most readable output.
If you need the file to be human readable ASCII, you were on the right track with dump
. The only change you need is that dump
takes the name of the object as its argument:
dump('variant', file="variant_file")
Here is an example of the sort of output from each of these options for a random neural network:
> 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")
Upvotes: 1