简体   繁体   English

尝试生成一个函数,该函数根据列中的值分隔数据框,然后从该分隔数据生成新的数据框

[英]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.

相关问题 通过根据 R 中的分位数分配值在数据框中生成新列? - Produce new column in data frame by assigning values based on quantiles in R? 使用逻辑从现有的多列数据帧生成新的多列数据帧 - Produce new multi-column data frame from existing multi-column data frames with logic R:根据条件(不同大小的数据框)从另一个数据框的列中为列分配值 - R: Assign values to column, from a column from another data frame, based on a condition (different sized data frames) 循环遍历数据框以生成新列 - Loop through data frames to produce new column 根据列名、ID 号和其他数据框中的键值替换数据框中的值 - Replacing values in a data frame based on column names, ID numbers, and key values from other data frames 有没有办法根据在 R 中用逗号分隔值的列对数据进行分组? - Is there a way to group data based on a column that separates values with commas in R? 根据其他数据框的功能创建新的数据框 - create new data frame from a function of other data frames 根据条件从汇总的列值创建新数据框 - Create new data frame from summed column values based on conditions 根据另一个数据框中的值创建新数据框 - Create new data frame based on values from another data frame 根据来自另一个数据帧的值在一个数据帧中应用函数 - apply a function in a data frame based on values from another data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM