[英]Trying to produce a function that separates a data frame, based on values within a column, and then produces new data frames from that separated data
I am trying to produce a function in R that allows me to produce a new series of data frames based on values contained with a specified column in another dataframe.我正在尝试在 R 中生成一个函数,该函数允许我根据另一个数据帧中指定列包含的值生成一系列新的数据帧。 I have been using the palmer penguins package to try and sort this out.我一直在使用 Palmer penguins 包来尝试解决这个问题。 Essentially I am trying to create a function that does this:本质上,我正在尝试创建一个执行此操作的函数:
penguins%>%
group_by(species)%>%
group_split(species)
Allowing me to have all the original values separated by a value of interest.允许我用感兴趣的值分隔所有原始值。 However, when I try to create a function I end up with an error:但是,当我尝试创建一个函数时,我最终遇到了一个错误:
variable_isolation <- function(x){
group_by(df[{{x}}])%>%
group_split(df[{{x}}])
}
Instead of df[{{x}}]
use only {{x}}
and pass the dataframe and variable to the function.而不是df[{{x}}]
只使用{{x}}
并将数据帧和变量传递给函数。
library(palmerpenguins)
library(dplyr)
variable_isolation <- function(data, x){
data %>% group_split({{x}})
}
penguins %>% variable_isolation(species)
#Or
#variable_isolation(penguins, species)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.