我有一个非常大的矩阵,并希望保存到本地以供以后使用。这是我的矩阵:
head(copy_fourgram)
[,1] [,2] [,3] [,4] [,5]
[1,] "\u0097" "a" "moment" "when" "1"
[2,] "\u0096" "and" "of" "support" "1"
[3,] "\u0095" "eli" "lathrop" "yard" "1"
[4,] "\u0095" "james" "brown" "yard" "1"
[5,] "\u0095" "keep" "a" "fire" "1"
[6,] "\u0097" "maybe" "even" "vent" "1"
以下是我保存的代码:
library(MASS)
write.matrix(format(copy_fourgram, scientific=FALSE),
file = paste("./en_US/ngrams/", "copy_fourgram.csv", sep="/"), sep=",")
当我以csv回读:
fourgram_file <- file("./en_US/ngrams/fourgram.csv", "r")
new_copy_fourgram <- read.csv(fourgram_file, header=T)
close(fourgram_file)
new_copy_fourgram <- as.matrix(new_copy_fourgram)
head(new_copy_fourgram)
X. a moment X1 when
[1,] "\u0096 " "and " "of "1" " "support "
[2,] "\u0095 " "eli " "lathrop "1" " "yard "
[3,] "\u0095 " "james " "brown "1" " "yard "
[4,] "\u0095 " "keep " "a "1" " "fire "
[5,] "\u0097 " "maybe " "even "1" " "vent "
[6,] "½ " "years " "old "1" " "now "
正如您所看到的,我在此处存在多个格式问题,包括标题错误和引号错位。有关如何通过该过程保留此矩阵的任何见解?谢谢!
答案 0 :(得分:6)
可能符合您需求的一个选项是使用save()
功能,这样您就可以将矩阵存储到磁盘而无需担心格式问题:
save(copy_fourgram, file = "copy_fourgram.RData")
如果要再次加载此矩阵,可以将load()
与您创建的数据文件的名称一起使用:
load("copy_fourgram.RData")