r - 错误: 'shuffle' is not an exported object from 'namespace:merTools'

标签 r shuffle

我正在尝试按行对数据集进行洗牌,并且我在 merTools 包中找到了函数 shuffle。代码如下所示:

data = merTools::shuffle(my_data)  

但我仍然有这个错误:

Error: 'shuffle' is not an exported object from 'namespace:merTools'

即使我尝试加载包:

library(merTools)
data = shuffle(my_data)

它给出了这个错误:

Error in shuffle(my_data) : could not find function "shuffle"

但我可以在帮助中看到它存在。

感谢您的帮助。

最佳答案

可能是未导出的函数

getAnywhere("shuffle")$where
[1] "namespace:merTools"

不同的是,当有导出函数时,它也会显示包

getAnywhere("mutate")$where
#[1] "package:dplyr"   "namespace:plyr"  "namespace:dplyr"

因此,我们可以从命名空间获取函数并应用于数据

out <- getFromNamespace("shuffle", "merTools")(mtcars)
head(out, 5)
#                mpg cyl  disp  hp drat   wt qsec vs am gear carb
#Merc 240D      24.4   4 146.7  62 3.69 3.19 20.0  1  0    4    2
#Ferrari Dino   19.7   6 145.0 175 3.62 2.77 15.5  0  1    5    6
#Ford Pantera L 15.8   8 351.0 264 4.22 3.17 14.5  0  1    5    4
#Porsche 914-2  26.0   4 120.3  91 4.43 2.14 16.7  0  1    5    2
#Merc 280C      17.8   6 167.6 123 3.92 3.44 18.9  1  0    4    4

或者正如@RuiBarradas在评论中建议的那样,为了重复使用,最好将导出的函数分配给标识符

shuffle <- getFromNamespace("shuffle", "merTools")

然后直接应用

shuffle(mtcars)

关于r - 错误: 'shuffle' is not an exported object from 'namespace:merTools' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56699083/

相关文章:

c++ - `shuffle_order_engine` 是做什么用的?

python - 如何 random.shuffle 切片或对象列表的子集?

python - 打乱数据框以计算 Pandas 中 P 值的最快方法

R Shiny - 输出汇总统计

r - Package development : How can I import data from a package, 对其进行改造,并作为数据集导出?

r - 使用插入符号调整参数

r - 在 clusterApply 或 clusterMap 中将进度打印到 windows cmd

r - 为每一行打乱(随机化)数据

java - 为什么 Collections.shuffle(List) 之后会出现空行?

r - purrr 语法和map_深度