简体   繁体   English

如何在物理RData中直接使用data.frame?

[英]How to directly work with a data.frame in physical RData?

Do I have to 我一定要吗

1) load a data.frame from the physical RData to the memory, 1)从物理RData加载data.frame到内存,

2) make changes, 2)做出改变,

3) save it back to the physical RData, 3)将其保存回物理RData,

4) remove it from the memory to avoid conflicts? 4)将其从内存中删除以避免冲突?

Is there anyway I can skip the load/save steps and make permanent changes to the physical RData directly? 反正我是否可以跳过加载/保存步骤并直接对物理RData进行永久性更改? Is there a way to work with data.frame like the way working with a SQLite/MySQL database? 有没有办法使用data.frame,就像使用SQLite / MySQL数据库一样? Or should I just use SQLite/MySQL (instead of data.frame) as the data storage? 或者我应该只使用SQLite / MySQL(而不是data.frame)作为数据存储?

More thoughts: I think the major difference is that to work with SQLite/MySQL you establish a connection to the database, but to work with data.frame from RData you make a copy in the memory. 更多想法:我认为主要区别在于,使用SQLite / MySQL建立与数据库的连接,但是为了使用RData中的data.frame,您可以在内存中复制。 The later approach can create conflicts in complex programs. 后一种方法可能会在复杂程序中产生冲突。 To avoid potential conflicts you have to save the data.frame and immediately remove it from the memory every time you change it. 为了避免潜在的冲突,您必须保存data.frame并在每次更改时立即将其从内存中删除。

Thanks! 谢谢!

Instead of using load you may want to consider using attach . 您可能需要考虑使用attach而不是使用load This can attach the saved data object to the search path without loading all the objects in it into the global environment. 这可以将保存的数据对象附加到搜索路径,而无需将其中的所有对象加载到全局环境中。 The data frame would then be available to use. 然后可以使用数据框。

If you want to change the data frame then you would need to copy it to the global environment (will happen automatically for most editing) and then you would need to save it again (there would not be a simple way to save it into a .Rdata file that contains other objects). 如果你想更改数据框,那么你需要将它复制到全局环境(大多数编辑会自动发生),然后你需要再次保存它(没有一种简单的方法可以将它保存到。包含其他对象的Rdata文件)。

When you are done you can use detach (but if you have made a copy in the global environment then you will still need to delete that copy). 完成后,您可以使用detach (但如果您在全局环境中制作了副本,则仍需要删除该副本)。

If you don't like typing the load / save commands (or attach / detach ) each time then you could write your own function that goes through all the steps for you (and if the copy is only in the environment of the function then you don't need to worry about deleting it). 如果你不喜欢每次都输入load / save命令(或attach / detach ),那么你可以编写自己的函数来完成所有的步骤(如果副本只在函数的环境中,那么你不必担心删除它)。

You may also want to consider different ways of storing your data. 您可能还想考虑存储数据的不同方法。 The typical .Rdata file works well for an all or nothing approach. 典型的.Rdata文件适用于全有或全无的方法。 The saveRDS and readRDS functions will save and read a single object (and do not force you to use the same name when reading it back in). saveRDSreadRDS函数将保存并读取单个对象(并且在读取时不要强制使用相同的名称)。 The interfacing with a database approach is probably the best if you are making frequent changes to tables and want them stored outside of R. 如果您经常更改表并希望它们存储在R之外,那么与数据库方法的接口可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM