Unused arguments in R
Change the definition of multiply to take additional unknown arguments:
multiply <- function(a, b, ...) {
# Original code
}
R: unused argument in levels
From the help page?as.factor
it shows that the function only takes one argument (in your case the filtered_table$column
), and therefore the error message indicates that there's not another argument to match up with the second one you've specified in the function call. To specify the levels explicitly, you may need to use the factor()
function.
R : Function is returning Unused Arguments
As pointed out in the comments, if you name arguments, you need to name them according to the function definition. Also, the lower and upper bounds are not expressions, but must be constants (i.e. numbers, not names such as random_1
).
Such constraints could be handled for instance through penalties: in the objective function, compute whether a variable is outside its range; if it is, subtract a penalty from the objective function (if you maximise) or add a penalty (if you minimise).
There are optimisation methods in which you could handle such constraints directly when creating new solutions. One such method is Local Search. Here is a (rough) example, in which I assume that you want to maximise. I use the implementation in package NMOF (which I maintain). The algorithm expects minimisation, but that is no problem: just multiply the objective function value by -1. (Note that many optimisation algorithms expect minimisation models.)
The key ingredient to Local Search is the neighbourhood function. It takes a given solution as input and produces a slightly changed solution (a neighbour solution). Local Search then takes a random walk through the space of possible solutions (with steps as defined in the neighbourhood function), accepting better solutions, but rejecting solutions that lead to worse objective function values.
library("NMOF")
nb <- function(x, ...) {
## randomly pick one element in x and a small change
i <- sample.int(length(x), 1)
stepsize <- c(1, 1, 1, 1, 0.5, 0.5, 0.5)
x[i] <- x[i] + runif(n = 1,
min = -stepsize[i],
max = stepsize[i])
## 'repair' the solution
x <- pmin(x, c(120, 120, 120, 120, 1, 1, 1))
x <- pmax(x, c( 80, x[1], 85, x[2], 0, 0, 0))
x
}
An initial solution:
x0 <- c(100, 100, 100, 100, 0.5, 0.5, 0.5)
## [1] 100.0 100.0 100.0 100.0 0.5 0.5 0.5
A random step:
nb(x0)
## [1] 100.000 100.000 100.000 100.000 0.759 0.500 0.500
Running the algorithm:
sol <- LSopt(function(x) -do.call(fitness, as.list(x)),
list(neighbour = nb,
x0 = x0,
nI = 500))
-sol$OFvalue
## 0.998
sol$xbest
## [1] 112.329 112.331 111.777 112.331 1.000 0.771 1.000
If such a method would be acceptable you, perhaps this tutorial on heuristics is useful.
Here would be a solution with PSO, with NMOF::PSopt
.
sol <- PSopt(function(x) -do.call(fitness, as.list(x)),
list(nG = 25, ## number of generations
nP = 20, ## population size
repair = function(x) {
x <- pmin(x, c(120, 120, 120, 120, 1, 1, 1))
x <- pmax(x, c( 80, x[1], 85, x[2], 0, 0, 0))
x
},
min = c( 80, 80, 85, 85, 0, 0, 0),
max = c(120, 120, 120, 120, 1, 1, 1)))
-sol$OFvalue
## 0.998
sol$xbest
## [1] 98.551 98.551 110.750 108.639 1.000 0.312 1.000
Why do I get unused argument (na.action = NULL) error in aggregate?
You don't have to use the column names in aggregate.formula
.na.pass
should solve your na.action
requirements.
setNames(
aggregate( cbind(df[,1], df[,3]) ~ df[,2], df, sum, na.rm=T,
na.action=na.pass ), colnames(df[,c(2,1,3)]) )
group x other_var
1 1 25 -0.7313815
2 2 30 0.3231317
Data
(I added NA
s)
df <- structure(list(x = 1:10, group = c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), other_var = c(-1.79458090358371, 0.295106071151792,
NA, -0.589487588239041, 0.325944874015228, NA, 0.737254570399201,
0.47849317537615, NA, 0.139020009150021)), row.names = c(NA,
-10L), class = "data.frame")
Related Topics
Warning in Install.Packages: Unable to Move Temporary Installation
What Does the Error "Arguments Imply Differing Number of Rows: X, Y" Mean
How to Handle Vectors Without Knowing the Type in Rcpp
Find Consecutive Values in Vector in R
How to Turn Gpclibpermit() to True
Reverse and Change Limit of Axis
Using Dplyr Within a Function, Non-Standard Evaluation
Applying a Function to a Backreference Within Gsub in R
Different Axis Limits Per Facet in Ggplot2
How to Merge Two Data Frames on Common Columns in R with Sum of Others
Remove 'Search' Option But Leave 'Search Columns' Option
Why Is Subsetting on a "Logical" Type Slower Than Subsetting on "Numeric" Type
Change the Number of Breaks Using Facet_Grid in Ggplot2
Applying a Function to Each Row of a Data.Table
Unknown Timezone Name in R Strptime/As.Posixct