首页 > 解决方案 > 使用带有数据表名称和 utils::data 向量的 map

问题描述

我想将mlbench包中的两个数据集加载到我的环境中。我知道我可以使用该data(x)功能手动执行此操作,但我想使用purrr:map. 但是,当我尝试这样做时,我不断收到以下错误:

data set ‘.x’ not founddata set ‘.x’ not found[[1]]
[1] ".x"

[[2]]
[1] ".x"

这可能吗?

## libraries
library("mlbench")
library("dplyr")
library("purrr")

## load data sets into environment manually
data(BostonHousing)
data(BostonHousing2)

## load data sets into environment using map
data_names <- c("BostonHousing", "BostonHousing2")

## generates error
data_names %>% map(~data(.x))

标签: r

解决方案


使用data,有两个参数用于加载数据,...检查文字值和字符串list向量。character所有,我们要做的就是代替默认选项...,指定list从字符串向量加载数据集,然后使用mget将这些数据集加载到一个list

library(dplyr)
library(purrr)
library(mlbench)
data(list = data_names)
mget(data_names)  %>%
     map(head, 5)

-输出

#$BostonHousing
#     crim zn indus chas   nox    rm  age    dis rad tax ptratio      b lstat medv
#1 0.00632 18  2.31    0 0.538 6.575 65.2 4.0900   1 296    15.3 396.90  4.98 24.0
#2 0.02731  0  7.07    0 0.469 6.421 78.9 4.9671   2 242    17.8 396.90  9.14 21.6
#3 0.02729  0  7.07    0 0.469 7.185 61.1 4.9671   2 242    17.8 392.83  4.03 34.7
#4 0.03237  0  2.18    0 0.458 6.998 45.8 6.0622   3 222    18.7 394.63  2.94 33.4
#5 0.06905  0  2.18    0 0.458 7.147 54.2 6.0622   3 222    18.7 396.90  5.33 36.2

#$BostonHousing2
#        town tract     lon     lat medv cmedv    crim zn indus chas   nox    rm  age    dis rad tax ptratio      b lstat
#1     Nahant  2011 -70.955 42.2550 24.0  24.0 0.00632 18  2.31    0 0.538 6.575 65.2 4.0900   1 296    15.3 396.90  4.98
#2 Swampscott  2021 -70.950 42.2875 21.6  21.6 0.02731  0  7.07    0 0.469 6.421 78.9 4.9671   2 242    17.8 396.90  9.14
#3 Swampscott  2022 -70.936 42.2830 34.7  34.7 0.02729  0  7.07    0 0.469 7.185 61.1 4.9671   2 242    17.8 392.83  4.03
#4 Marblehead  2031 -70.928 42.2930 33.4  33.4 0.03237  0  2.18    0 0.458 6.998 45.8 6.0622   3 222    18.7 394.63  2.94
#5 Marblehead  2032 -70.922 42.2980 36.2  36.2 0.06905  0  2.18    0 0.458 7.147 54.2 6.0622   3 222    18.7 396.90  5.33

推荐阅读