为什么使用.RData,而.R就足够了

8 浏览
0 Comments

为什么使用.RData,而.R就足够了

如果我们可以从.R文件中保存和加载完全相同的数据,那么为什么还需要.RData呢?我试图从[R] foo.RData or foo.r?找到一些解释。所以,我遇到了一些问题:\n

    \n

  • .RData只保存最终结果还是完整的代码,就像.R脚本一样?
  • \n

  • 它们的确切关联是什么?在什么情况下应该选择其中之一?
  • \n

0
0 Comments

在.R文件中,我们可以保存R代码;而在.RData文件中,我们可以保存R中的数据结构,如向量、矩阵、数据框或线性模型。

The reason for the emergence of the question "Why .RData when .R is sufficient" is that sometimes we may only need to save the R code without the need to save the data structures. In such cases, using the .R extension for the file would be sufficient.

出现“为什么使用.RData文件,而.R文件已经足够”的问题的原因是,有时我们只需要保存R代码,而不需要保存数据结构。在这种情况下,使用.R扩展名的文件就足够了。

However, there are situations where we not only need to save the R code but also want to save the data structures for future use. In such cases, using the .RData extension would be more appropriate.

然而,有些情况下,我们不仅需要保存R代码,还希望保存数据结构以供将来使用。在这种情况下,使用.RData扩展名更合适。

To save R code in an .R file, we can simply use a text editor and save the file with the .R extension. This allows us to easily access and modify the code in the future.

要将R代码保存在.R文件中,我们可以使用文本编辑器,并将文件保存为.R扩展名。这样,我们就可以轻松地在将来访问和修改代码。

To save data structures in an .RData file, we can use the "save" function in R. This function allows us to specify the objects we want to save and the name of the .RData file.

要将数据结构保存在.RData文件中,我们可以使用R中的“save”函数。该函数允许我们指定要保存的对象和.RData文件的名称。

Here is an example of how to save a vector named "my_vector" and a data frame named "my_data" in an .RData file named "my_data.RData":

下面是一个示例,演示了如何将名为“my_vector”的向量和名为“my_data”的数据框保存在名为“my_data.RData”的.RData文件中:

my_vector <- c(1, 2, 3, 4, 5)

my_data <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))

save(my_vector, my_data, file = "my_data.RData")

To load the saved data structures from an .RData file, we can use the "load" function in R. This function allows us to load the objects saved in the .RData file into our R environment.

要从.RData文件中加载保存的数据结构,我们可以使用R中的“load”函数。该函数允许我们将.RData文件中保存的对象加载到我们的R环境中。

Here is an example of how to load the saved data structures from the "my_data.RData" file:

下面是一个示例,演示了如何从“my_data.RData”文件中加载保存的数据结构:

load("my_data.RData")

In conclusion, the question "Why .RData when .R is sufficient" arises because sometimes we only need to save the R code without the data structures. However, when the data structures need to be saved for future use, using the .RData extension is more appropriate. We can save R code in .R files and data structures in .RData files using the respective functions in R.

0
0 Comments

为什么使用.RData文件,而.R文件已经足够?

在R中,.RData文件保存的是对象,而不是脚本,如果你加载它,你加载的是环境中的对象,而不是包含产生这些对象的代码。

而.R文件是一个没有任何对象的脚本,如果你打开它,你将看到代码,你需要使用source()函数来获取.R文件产生的对象。

我建议按照以下方式使用它们:

- .R文件:存储函数和用于创建对象的脚本(例如,用于包中的/data-raw文件夹的可重现性)

- 使用.RData文件存储以后会用到的对象

这基本上是一个包的工作方式:一个包含函数的/R文件夹,以及包所需的数据对象的/data文件夹。

所以,这两者互相补充吗?

是的。总结一下:.RData文件用于保存对象,.R文件用于保存代码。

我猜你可以使用任何你想要的扩展名,但提到的扩展名是最常见的。.RData文件使用load()函数加载到你的R会话中,而.R文件使用source()函数加载到你的会话中。

0