【问题标题】:Load a dataset into R with data() using a variable instead of the dataset name使用变量而不是数据集名称使用 data() 将数据集加载到 R 中
【发布时间】: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

标签: r dataset apriori arules


【解决方案1】:

使用list 参数。见?data

data(list=myvar)

您还需要 myvar 作为字符串。

myvar <- "Titanic"

请注意,myvar &lt;- Titanic 仅起作用(我认为)是因为泰坦尼克号数据集的延迟加载。包中的大多数数据集都是以这种方式加载的,但对于其他类型的数据集,您仍然需要data 命令。

【讨论】:

  • 尝试了数据(list=myvar),但它产生了 32 条“在数据中(list = myvar):未找到数据集‘0’”的警告。尝试将另一个 arules 数据集(“Groceries”)存储到 myvar 中,但这根本没有加载。 (“as.character.default(pattern) 中的错误:没有将这个 S4 类强制为向量的方法”)。除了 list=myvar 之外,也许我需要在 data() 中指定更多参数?
  • @DWin 看到了您的问题;你需要 myvar 是一个字符串。
  • 对不起,我的回答迟了。也不起作用:(它一直产生32个警告。奇怪的是它为我尝试使用的每个数据集产生32个警告——即使是有10个左右事务的小数据集。data()函数真的需要在之前执行吗运行apriori?我的意思是,如果我直接运行apriori()函数,而不先运行data(),结果会不会出错?
【解决方案2】:

使用变量作为字符。否则,您将处理“泰坦尼克号”的内容而不是其名称。您可能还需要使用 get 将字符值转换为对象名称。

myvar <- 'Titanic'

myfun <- function(mydataset) {
    data(list=mydataset)   
    str(get(mydataset))
}

myfun(myvar)

【讨论】:

  • 对不起,我的回答迟了。也不起作用:(它一直产生32个警告。奇怪的是它为我尝试使用的每个数据集产生32个警告——即使是有10个左右事务的小数据集。data()函数真的需要在之前执行吗运行apriori?我的意思是,如果我直接运行apriori()函数,而不先运行data(),结果会不会出错?
【解决方案3】:

如果包已经加载,可以使用get()函数将数据集赋值给一个局部变量:

data_object = get(myvar, asNamespace('<package_name>'))

或者简单地说:

data_object = get(myvar)

【讨论】:

    【解决方案4】:

    我正在回答我自己的问题,但我终于找到了解决方案。引用 R 帮助:

    “在所有当前加载的包中搜索数据集,然后在当前工作目录的‘data’目录(如果有的话)中搜索。”

    因此,只需将数据集写入文件并将其放入名为“data”的目录并位于工作目录中。

    > write.table(mydataset,file="dataset.csv",sep=",",quote=TRUE,row.names=FALSE)  # I intend to create a csv file, so I use 'sep=","' to separate the entries by a comma, 'quote=TRUE' to quote all the entries, and 'row.names=F to prevent the creation of an extra column containing the row names (which is the default behavior of write.table() )
    
    # Now place the dataset into a "data" directory (either via R or via the operating system, doesn't make any difference):
    > dir.create("data")  # create the directory
    > file.rename(from="dataset.csv",to="data/dataset.csv")  # move the file
    
    # Now we can finally load the dataset:
    > data("mydataset")  # data(mydataset) works as well, but quoted is preferable - less risk of conflict with another object coincidentally named "mydataset" as well
    

    【讨论】:

    • 啊,这解释了很多。通常人们会在这种情况下使用read.csvdata 通常仅在从包中加载数据文件时使用,如您在问题中给出的示例所示。将来,如果您提供完整的可重现示例,您将获得更好的答案。
    • 是的,看来我只是误解了 data() 的用法——我认为这是从数据集中挖掘规则之前的必要步骤。
    • 应该告知人们@pazof 并不真正知道他/她在做什么,并犯了一堆不必要的错误。此外,他关于如何引发错误的例子是不完整的。然后他的“答案”基本上是错误的。 (当然只是我的看法,但我认为他给自己打勾可能会误导人们。)
    • @DWin:这不是“错误”,只是不是解决问题的最佳解决方法——当然不是,恰恰相反——但它仍然有效。复选标记是因为它是解决问题的答案中唯一的解决方案。关于你提到的不必要的错误,请你指出来吗?
    【解决方案5】:

    Assign_Name

    这行代码打开你的本地机器,只需选择你要加载的数据集R环境

    【讨论】:

    • 感谢您的参与,但我认为这不能解决问题。问题具体是关于如何使用data() 函数和存储在character 字符串中的变量加载包中提供的数据。这个答案是关于如何从 CSV 文件中加载数据不使用完全不使用名称。
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 2014-01-27
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多