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