将文件加载到R中

时间:2013-04-03 19:54:22

标签: r

我第一次使用R我正在尝试保存数据并再次加载它,我使用此

> a <- 1:10
> save(a, file = "desktop/Data.Rdata")
> rm(a)
> load("desktop/Data.Rdata")

警告:

 cannot open compressed file 'desktop/Data.Rdata', probable reason 'No such file or directory'

请帮帮我。

这是有用的人

> a <- 1:10
> getwd()
[1] "C:/Users/saramachandran/Documents"
> save(a,file="Documents/sai.R")
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
  cannot open compressed file 'Documents/sai.R', probable reason 'No such file or directory'
> save(a,file="/Documents/sai.R")
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
  cannot open compressed file '/Documents/sai.R', probable reason 'No such file or directory'
> save(a,file="C:/Users/saramachandran/Documents/sai.R")
> rm(a)
> load("C:/Users/saramachandran/Documents/sai.R")
> print(a)
 [1]  1  2  3  4  5  6  7  8  9 10
> 

2 个答案:

答案 0 :(得分:6)

尝试使用:

a <- 1:10
save(a, file = path.expand("~/desktop/Data.Rdata"))
rm(a)
load(path.expand("~/desktop/Data.Rdata"))

修改

您发布了以下内容......

> a <- 1:10
> getwd()
[1] "C:/Users/saramachandran/Documents"
> save(a,file="Documents/sai.R")
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
  cannot open compressed file 'Documents/sai.R', probable reason 'No such file or directory'
> save(a,file="/Documents/sai.R")

问题在于工作目录已经存在于Documents中。所以你要保存到:

 C:/Users/saramachandran/Documents/Documents

这不存在。

改为使用它(或明确说明你发现的路径):

a <- 1:10
save(a, file = "Data.Rdata")
rm(a)
load("Data.Rdata")

答案 1 :(得分:1)

我有几次同样的错误。我通过设置项目作为工作目录解决了这个问题。单击您的项目 - 更多 - 下拉菜单,然后选择&#34;设置为工作目录。&#34;希望这有帮助,祝你好运。