R:在函数内使用聚合,不起作用

编程入门 行业动态 更新时间:2024-10-28 07:19:52
本文介绍了R:在函数内使用聚合,不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对 R 还很陌生,如果我无法找到这个问题的答案,我深表歉意.

我无法用我自己的数据集复制我遇到的确切错误,但由于仍然产生了错误,所以我们继续.我想要做的是创建一个函数来计算几列的平均值,条件是其他列的值.让我们说

d1 <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)d2 <- c(1:12)d3 <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2)df <- cbind(d1, d2, d3)indicator_plotting1 <- function(a, b = d2, c = 1) {temp_data <-df%>%过滤器(d3 = c)for_plot <- 聚合(b ~ a, df, mean)情节(for_plot$b,for_plot$a)}indicator_plotting1(a = d1)

如果我不包含 %>% filter(d3 = c) 位,效果会很好.当我包含它时,我得到UseMethod("filter_") 中的错误:没有适用于 'filter_' 的方法应用于类 "c('matrix', 'double', 'numeric')".

当我使用我在这里建议的数据集时.当我用我自己的更大的过滤器和四个过滤器来做这件事时,我得到的是在 eval(predvars, data, env) 中出错:找不到对象 'd1'.

有什么想法吗?提前致谢

解决方案

考虑在 aggregate() 和 plot 中使用带有 reformulate 的动态公式进行基本 R 调整().对于以下解决方案,您需要将列引用作为字符串传递.以下适用于命名矩阵或数据框:

indicators_plotting1 <- function(a, b = "d2", c = 1) {temp_data <- 子集(df,d3 == c)for_plot <-聚合(重新制定(a,b),temp_data,平均值)情节(重新制定(b,a),for_plot)}indicator_plotting1(a = "d1")

或者单行函数:

indicators_plotting1 <- function(a, b = "d2", c = 1) {情节(重新制定(b,a),聚合(重新制定(a,b),子集(df,d3 == c),平均值))}

I am fairly new to R so apologies if an answer to this already exists that I am unable to find.

I cannot replicate the exact error I have with my own dataset, but since an error is produced nonetheless here we go. What I am trying to do is to create a function to calculate the average of several columns conditionnal on the values of other ones. Let's say that

d1 <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4) d2 <- c(1:12) d3 <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2) df <- cbind(d1, d2, d3) indicators_plotting1 <- function(a, b = d2, c = 1) { temp_data <- df %>% filter(d3 = c) for_plot <- aggregate(b ~ a, df, mean) plot(for_plot$b, for_plot$a) } indicators_plotting1(a = d1)

It works well if I do not include the %>% filter(d3 = c) bit. When I include it I get Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('matrix', 'double', 'numeric')".

That when I do it with the dataset I propose here. When I do it with my own larger one and with four filters, what I get is Error in eval(predvars, data, env) : object 'd1' not found.

Any thoughts? Thanks in advance

解决方案

Consider following base R adjustment using dynamic formula with reformulate in aggregate() and plot(). For below solution, you need to pass column reference as strings. And below works on named matrices or data frames:

indicators_plotting1 <- function(a, b = "d2", c = 1) { temp_data <- subset(df, d3 == c) for_plot <- aggregate(reformulate(a, b), temp_data, mean) plot(reformulate(b, a), for_plot) } indicators_plotting1(a = "d1")

Alternatively a one-line function:

indicators_plotting1 <- function(a, b = "d2", c = 1) { plot(reformulate(b, a), aggregate(reformulate(a, b), subset(df, d3 == c), mean)) }

Online Demo (click Run it (F8) at bottom for plot)

更多推荐

R:在函数内使用聚合,不起作用

本文发布于:2023-11-22 07:11:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   不起作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!