我有一个非常大的矩阵,并希望保存到本地以供以后使用.这是我的矩阵:
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"
Run Code Online (Sandbox Code Playgroud)
这是我保存的代码:
library(MASS)
write.matrix(format(copy_fourgram, scientific=FALSE),
file = paste("./en_US/ngrams/", "copy_fourgram.csv", sep="/"), sep=",")
Run Code Online (Sandbox Code Playgroud)
当我回读为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 "
Run Code Online (Sandbox Code Playgroud)
如您所见,我在这里有多个格式问题,包括标题错误和引号错位.有关如何通过该过程保留此矩阵的任何见解?谢谢!
一个可能满足您需求的选项是使用save()允许您将矩阵存储到磁盘的功能,而不必担心格式问题:
save(copy_fourgram, file = "copy_fourgram.RData")
Run Code Online (Sandbox Code Playgroud)
如果要再次加载此矩阵,可以使用load()您创建的数据文件的名称:
load("copy_fourgram.RData")
Run Code Online (Sandbox Code Playgroud)