Mapply过滤两个列表中的分组功能

时间:2017-09-28 17:07:06

标签: r mapply

我有以下数据(简化):

data1 <-  data.frame(cool_A = c(5,10,15,20, 5,10,15,20),
                    cool_B = c(10,20,25,30, 10,20,25,30),
                    group = c('group1','group1','group1','group1','group2','group2','group2','group2'))

data2 <- data.frame(A = c(0.2),
                    B = c(0.5))

我试图对数据运行stats过滤器函数,如下所示:

function2 <- function(x, y) {as.numeric(stats::filter(x, filter=y, method="recursive"))}
test2 <- mapply(function2, data1[,c(grep("^cool", names(data1)))], data2)
test2

这在某种程度上有效,但我想通过&#39; group&#39;对过滤功能进行分组/拆分。在data1。我的想法是将data1拆分成一个列表并将其应用到函数中,但这不起作用。我也尝试过一个&#39; by&#39;函数进入代码。有什么建议吗?

data1b <- split(data1, data1$group)

test2 <- mapply(function2, data1b[,c(grep("^cool", names(data1b)))], data2)
test2

0 个答案:

没有答案