将数据集命名为 R/Lattice 中的变量



Lattice中使用的当前数据集的文件名(例如在xyplot()中)是否可以作为变量名?

我想将数据集名称作为图的脚注包含在图形中,并编写一个采用该名称的通用函数。

谢谢

你的意思是"如何将变量名变成字符串"吗?

如果是这样,请使用魔法咒语deparse(substitute(my_variable))

drawplot <- function(x, data)
{
  dataname <- deparse(substitute(data))
  xyplot(
    x,
    data = data,
    main = dataname
  )  
}
drawplot(Sepal.Width ~ Sepal.Length, iris)

仔细想想,这可能是想多了。 另一种方式更容易一些:从数据集名称开始,然后使用 get 检索数据。

dataname <- "iris"
xyplot(
  Sepal.Width ~ Sepal.Length,
  data = get(dataname),
  main = dataname
) 

最新更新