如何运行每个返回向量的函数,不是单个值? [英] How to a run function per group that return vector, not single value?

查看:104
本文介绍了如何运行每个返回向量的函数,不是单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集 data.frame(x = rnorm(100),group = c(rep('a',40),rep('b',60)))我想用dplyr分析每个组。例如,我想使用 fft 。如何在每个返回一个序列的组中产生一个函数,而不仅仅是一个值。如果这只是我可以总结的一个价值,这在这里不起作用。我希望像 data.frame(y = c(21,62,83,4,35,46,17,28),group = c(rep('a',5)),rep 'b',3)))作为输出。我想用dplyr来做,因为我认为它提高了可读性。

I have a dataset data.frame(x=rnorm(100),group=c(rep('a',40),rep('b',60))) that I want to analyse per group with dplyr. For example I want to use a fft. How can I produce run a function over each group which returns a sequence, not just value. If it was just a value I could summarize, which does not work here. I expect something like data.frame(y=c(21,62,83,4,35,46,17,28), group=c(rep('a',5),rep('b',3))) as output. I want to do it with dplyr if possible, because I think it improves readability.

推荐答案

我们可以用<$ c $创建列表输出与总结后,c> unnest 。更容易使用

We can do with unnest after creating a list output with summarise. It would be more easier to work with

library(tidyverse)
df1 %>%
    group_by(group) %>%
    summarise(value = list(fft(x))) %>%
    unnest()

这篇关于如何运行每个返回向量的函数,不是单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆