本文介绍了使用dplyr进行勘探的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 一个简单的例子: 我经常使用d_ply来生成探索性的地块。 code> require(plyr) plot_species< - function(species_data){p< - qplot(data = species_data,x = Sepal.Length,y = Sepal.Width print(p) } d_ply(.data = iris, .variables =Species 函数(x)plot_species(x)) 其中生成三个单独的图,一个用于每个物种。 我想使用dplyr中的函数重现此行为。 这似乎需要通过总结调用的函数中的data.frame的重新组合,这通常是不切实际的。 require(dplyr) iris_by_species< - group_by(iris,Species) plot_species< - function(Sepal.Length,Sepal.Width){ species_data p< - qplot(data = species_data,x = Sepal.Length,y = Sepal.Width) print(p) } 总结(iris_by_species,plot_species(Sepal.Length,Sepal.Width)) 可以部分data.frame传递给直接通过summary的函数,而不是传递列?解决方案我相信你可以使用 do 为您在 d_ply 中使用的功能相同的功能。它将直接打印到绘图窗口,而且还可以在生成的 data.frame 中将图形保存为列表 if您使用命名参数(请参阅帮助页面,这实质上就像使用 dlply )。我不完全掌握所有 do 可以做的,但是如果我不使用命名参数,我会收到一条错误消息,但是绘图仍然打印到绘图窗口在RStudio中) plot_species< - function(species_data){p< - qplot(data = species_data, x = Sepal.Length,y = Sepal.Width) print(p) } group_by(iris,Species)% >% do(plot = plot_species(。)) I regularly used d_ply to produce exploratory plots.A trivial example:require(plyr)plot_species <- function(species_data){ p <- qplot(data=species_data, x=Sepal.Length, y=Sepal.Width) print(p)}d_ply(.data=iris, .variables="Species", function(x)plot_species(x))Which produces three separate plots, one for each species.I would like to reproduce this behaviour using functions in dplyr.This seems to require the reassembly of the data.frame within the function called by summarise, which is often impractical.require(dplyr)iris_by_species <- group_by(iris,Species)plot_species <- function(Sepal.Length,Sepal.Width){ species_data <- data.frame(Sepal.Length,Sepal.Width) p <- qplot(data=species_data, x=Sepal.Length, y=Sepal.Width) print(p)}summarise(iris_by_species, plot_species(Sepal.Length,Sepal.Width))Can parts of the data.frame be passed to the function called by summarise directly, rather than passing columns? 解决方案 I believe you can work with do for this task with the same function you used in d_ply. It will print directly to the plotting window, but also saves the plots as a list within the resulting data.frame if you use a named argument (see help page, this is essentially like using dlply). I don't fully grasp all that do can do, but if I don't use a named argument I get an error message but the plots still print to the plotting window (in RStudio).plot_species <- function(species_data){ p <- qplot(data=species_data, x=Sepal.Length, y=Sepal.Width) print(p)}group_by(iris, Species) %>% do(plot = plot_species(.)) 这篇关于使用dplyr进行勘探的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-09 21:05