如何在单个调用中有效地删除(分配NULL)具有相似名称的许多对象

时间:2022-08-15 19:18:53

I have a bunch of data tables in R (say: dt1, dt2, dt3, etc.) that I want to remove from memory to clear up space. I have often used the following:

我在R中有一堆数据表(比方说:dt1,dt2,dt3等)我想从内存中删除以清理空间。我经常使用以下内容:

dt1 <- NULL
dt2 <- NULL
dt3 <- NULL

However, if I have many data tables this becomes a lot of lines. Is there a way to do this in one line?

但是,如果我有许多数据表,这将变成很多行。有没有办法在一行中做到这一点?

Side question: I believe using NULL is a faster way to clear the space but still leaves the object in memory versus using rm(dt1) which actually clears the memory assignment of the object but does take longer.

旁边的问题:我认为使用NULL是一种更快的方式来清除空间但是仍然将对象留在内存中而不是使用rm(dt1),它实际上清除了对象的内存分配但确实需要更长的时间。

1 个解决方案

#1


4  

This will remove all dataframes that start with 'dt'.

这将删除所有以'dt'开头的数据帧。

If you use table naming convention strategically, you can insert this wherever you'd like and clear your space.

如果您策略性地使用表命名约定,则可以将其插入任何您想要的位置并清除空间。

rm(list = ls(pattern = "^dt"))

#1


4  

This will remove all dataframes that start with 'dt'.

这将删除所有以'dt'开头的数据帧。

If you use table naming convention strategically, you can insert this wherever you'd like and clear your space.

如果您策略性地使用表命名约定,则可以将其插入任何您想要的位置并清除空间。

rm(list = ls(pattern = "^dt"))