gpt4 book ai didi

R将矩阵保存到csv,并加载为矩阵

转载 作者:行者123 更新时间:2023-12-01 10:38:44 25 4
gpt4 key购买 nike

我有一个非常大的矩阵,想保存到本地以备后用。这是我的矩阵:

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 "

如您所见,我在这里有多个格式问题,包括标题和引号放错了位置。关于如何通过该过程保留此矩阵的任何见解?谢谢!

最佳答案

可能适合您需要的一个选项是使用 save() 函数,它允许您将矩阵存储到磁盘而无需担心格式问题:

save(copy_fourgram, file = "copy_fourgram.RData")

当你想再次加载这个矩阵时,你可以使用 load() 和你创建的数据文件的名字:

load("copy_fourgram.RData")

关于R将矩阵保存到csv,并加载为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com