【发布时间】:2013-11-23 15:17:36
【问题描述】:
我正在尝试使用 data() 函数将数据集加载到 R 中。当我使用数据集名称(例如data(Titanic) 或data("Titanic"))时,它工作正常。对我不起作用的是使用变量而不是其名称加载数据集。例如:
# This works fine:
> data(Titanic)
# This works fine as well:
> data("Titanic")
# This doesn't work:
> myvar <- Titanic
> data(myvar)
**Warning message:
In data(myvar) : data set ‘myvar’ not found**
为什么 R 寻找名为“myvar”的数据集,因为它没有被引用? 而且由于这是默认行为,难道没有办法加载存储在变量中的数据集吗?
为了记录,我要做的是创建一个使用“arules”包并使用 Apriori 挖掘关联规则的函数。因此,我需要将数据集作为参数传递给该函数。
myfun <- function(mydataset) {
data(mydataset) # doesn't work (data set 'mydataset' not found)
rules <- apriori(mydataset)
}
编辑 - sessionInfo() 的输出:
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] arules_1.0-14 Matrix_1.0-12 lattice_0.20-15 RPostgreSQL_0.4 DBI_0.2-7
loaded via a namespace (and not attached):
[1] grid_3.0.0 tools_3.0.0
我得到的实际错误(例如,使用示例数据集“xyz”):
xyz <- data.frame(c(1,2,3))
data(list=xyz)
Warning messages:
1: In grep(name, files, fixed = TRUE) :
argument 'pattern' has length > 1 and only the first element will be used
2: In grep(name, files, fixed = TRUE) :
argument 'pattern' has length > 1 and only the first element will be used
3: In if (name %in% names(rds)) { :
the condition has length > 1 and only the first element will be used
4: In grep(name, files, fixed = TRUE) :
argument 'pattern' has length > 1 and only the first element will be used
5: In if (name %in% names(rds)) { :
the condition has length > 1 and only the first element will be used
6: In grep(name, files, fixed = TRUE) :
argument 'pattern' has length > 1 and only the first element will be used
...
...
32: In data(list = xyz) :
c("data set ‘1’ not found", "data set ‘2’ not found", "data set ‘3’ not found")
【问题讨论】:
-
请注意,既然您已经认识到
data("Titanic")或data(Titanic)可以工作,那么data(myvar)尝试加载名称为“myvar”的数据集也就不足为奇了。 -
能否添加
sessionInfo()的输出。其他解决方案有效,所以我想知道您为什么会遇到错误。您“接受”的解决方法远非理想...... -
myvar