开发者

A function that returns a dataset

开发者 https://www.devze.com 2023-01-10 01:08 出处:网络
I want to create a function that takes a dataset name and a package name and returns the dataset as data.frame. Here is my try

I want to create a function that takes a dataset name and a package name and returns the dataset as data.frame. Here is my try

loadDataSet <- function(name, pkg) {
      varname <- data(name, package=pkg)
      return(get(varname[[1]]))
    }
loadDataSet("acme", "boot")

However, this function fails. The problem seems to be, that the call to data() does not look up the value of the name variable, but rather "name".

I already know how to go from a variable to its name, via deparse(substitute(var)). But how do I go the other开发者_如何学Python way, from "var" to var?

Any hint appreciated!


Give this a try

loadDataSet <- function(name, pkg) {
      do.call("data", list(name,package=pkg))
      return(get(name))
    }

loadDataSet("acme", "boot")
0

精彩评论

暂无评论...
验证码 换一张
取 消