r - 使用带有数据表名称和 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))
解决方案
使用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
推荐阅读
- eiffel - 两个不同的配置文件共享相同的 UUID。在配置中
- android - 第一个元素在 GridView 适配器的列表末尾弹出
- pygame - 我想在我的飞鸟游戏中添加一个炸弹,但它不会检测到碰撞。这是代码
- r - 根据其他列值替换 For 循环以填充列
- python - Pyspark从现有数组列创建一定长度的数组列
- google-chrome - 在 url 更改时重新加载内容脚本
- flutter - Flutter Gradle 构建时出现错误异常
- node.js - 优雅启动 Node.js 网络服务器应用程序的好方法是什么
- c - 在 c 中解析 argc 和 argv[] 参数
- reactjs - 了解应用程序中的事件驱动架构和状态管理