我在Linux中仅使用R命令行。R中已定义变量的列表
一段时间后回到一个项目,我忘记了我使用的变量名,而R命令历史记录中没有包含它们。
我似乎记得有一个命令列出所有用户定义的变量,但不记得它是什么,并且无法在网上找到它。
如何列出R中的所有用户定义变量?
我在Linux中仅使用R命令行。R中已定义变量的列表
一段时间后回到一个项目,我忘记了我使用的变量名,而R命令历史记录中没有包含它们。
我似乎记得有一个命令列出所有用户定义的变量,但不记得它是什么,并且无法在网上找到它。
如何列出R中的所有用户定义变量?
ls()
从帮助页面:
‘ls’ and ‘objects’ return a vector of character strings giving the
names of the objects in the specified environment. When invoked
with no argument at the top level prompt, ‘ls’ shows what data
sets and functions a user has defined. When invoked with no
argument inside a function, ‘ls’ returns the names of the
functions local variables. This is useful in conjunction with
‘browser’.
编辑:我要指出,列出所有的变量,你需要使用
ls(all.names = TRUE)
否则以点开头的变量将不会出现在列表中。
你可能想看看这个链接:
Tricks to manage the available memory in an R session
它有一个很大的函数来显示物体与他们的内存使用情况一起。这是我的启动脚本R的一部分。
# Written by Dirk Eddelbuettel found here: https://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session
# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
obj.size <- napply(names, object.size)
obj.dim <- t(napply(names, function(x)
as.numeric(dim(x))[1:2]))
vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
obj.dim[vec, 1] <- napply(names, length)[vec]
out <- data.frame(obj.type, obj.size, obj.dim)
names(out) <- c("Type", "Size", "Rows", "Columns")
if (!missing(order.by))
out <- out[order(out[[order.by]], decreasing=decreasing), ]
if (head)
out <- head(out, n)
out
}
# shorthand
lsos <- function(..., n=10) {
.ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n)
}
数字。我应该猜到那个。谢谢! – 2012-03-16 19:53:53
@Dason这和'objects()'有区别吗? – frank 2018-03-08 20:24:01
@frank号https://github.com/wch/r-source/blob/51a4342f1b93d85bf6750cded97d8fa013984f46/src/library/base/R/attach.R#L200 – Dason 2018-03-08 20:32:23