I have the following problem: I'm writing a function which first constructs a long character string which stands for a mathematical function, e.g. "1/(1+exp(-x1+4x3))". I now want to maximize this function, but unfortunately I cannot do so because the mathematical function is only saved as a character string and not as an R-function. How can I solve this problem? Thanks in advance!
Maximizing mathematical function which is saved as character string
141 views Asked by lbf_1994 At
2
There are 2 answers
0
Roland
On
I'm writing a function which first constructs a long character string which stands for a mathematical function
Don't do that. I'm sure there is a better approach.
because the mathematical function is only saved as a character string and not as an R-function
You'd need to parse the string (after making it valid R syntax):
expr <- parse(text = gsub("((?<=\\d)[[:alpha:]])", "\\*\\1","1/(1+exp(-x1+4x3))", perl = TRUE))
Then you can use this expression to "find the maximum" with whatever method you'd like to use.
However, as fortune 106 says:
If the answer is parse() you should usually rethink the question.
Related Questions in R
- How to make an R Shiny app with big data?
- How do I keep only specific rows based on whether a column has a specific value?
- Likert scale study - ordinal regression model
- Extract a table/matrix from R into Excel with same colors and stle
- How can I solve non-conformable arguments in R netmeta::discomb (Error in B.matrix %*% C.matrix)?
- Can raw means and estimated marginal means be the same ? And when?
- Understanding accumulate function when .dir is set to "backwards"
- Error in if (nrow(peaks) > 0) { : argument is of length zero Calls: CopywriteR ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous> Execution ha
- How to increase quality of mathjax output?
- Convert the time intervals to equal hours and fill in the value column
- How to run an R function getpoints() from IPDfromKM package in an R shiny app which in R pops up a plot that utilizes clicks to capture coordinates?
- Replace NA in list of dfs in certain columns and under certain conditions
- R and text on Cyrillic
- The ts() function in R is returning the correct start and frequency but not end value which is 1 and not 179
- TROUBLING with the "DROP_NA" Function
Related Questions in FUNCTION
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- Function is returning undefined but should be returning a matched object from array in JavaScript
- How do you import functions from one page to another in Jetpack Compose?
- Adding Modules to a Namespace using IIFE
- How to convert mathematical expression to lambda function in C++?
- Custom Bash functions & custom statements - Need some advice
- Why my code is working on everything except one instance?
- Getting a function to call an equation
- Create Symbolic Function from Double Vector MATLAB
- Recursive calls to function passed as a parameter of another method via Consumer interface
- How can I replace a word in SQL but only if it is the last word in the string for a scalar-valued function?
- iterating through raster bands to perform calculation
- How to make this sensor keep taking readings once its when_in_range function has been activated?
- TypeError: indice_delete() takes 0 positional arguments but 3 were given
- How to modify HTML in WordPress core file
Related Questions in CHARACTER
- Notepad++ Remove Empty Spaces or characters after the specific LAST character
- ABAP convert Database char to lowercase
- Iterating through a string of long characters R
- Character and Numeric vectors, preserve decimal points in R
- Why do some non-ASCII Unicode symbols appended to strings disappear in Delphi 12?
- Count num of occurences of every 26 characters for every word in numpy
- Handwritten Tigrigna Character Recognition
- Get data from BIEN database using R for species names including characters like "-" and "x"
- for issuing in cbt CLI 'cbt deleterow <rowkey>', how can i escape space character in a rowkey?
- How to preserve midnight timestamp in R when converting from MDY-HMS to YMD-HMS
- Standards in char array declaration in C
- Converting characters to dates in R
- Godot - Character Animation looping repeatedly (constantly being rerun), bypassing any attempt to wait for it to finish
- What is this character format and how to decode to normal text?
- why doesn't the compiler convert character array to my custom-made String class?
Related Questions in MAXIMIZE
- Consumer Maximization Problem Doesn't Work in Wolfram Mathematica
- Optimizing weighted substitutions
- How do I get the best values of certain parameters that will give me the highest velocity
- python: Optimise groups of similar products for similarity in groups and for similarity over groups collectively?
- profit maximization problem (optimization)
- Problem in Maximization using genetic algorithm
- I am trying to maximize the following function
- Matlab code for lifetime optimization in wireless body area networks
- Getting solution as nan for mixed integer non linear programming problem with objective function as a fraction
- R, maximization problem, MIPModel - some questions
- Optimization with multiple inequality constraints
- Finding the minimum value of a non-linear function over a closed interval in Matlab
- My application runs fine, but when I toggle to full screen the screen maximizes but the design stays the same size
- Find the smallest range that contains a given percentage of values
- In R, given y = f(x), how do I find the global max of y/x?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
If we know what the arguments are ahead of time then (1) would be preferred as it is simpler (4 lines of code) but if we don't then (2) covers generating them as well (8 lines of code).
1) dynamic body This will convert the string
sinto a functionf2of 2 arguments which we can call fromf1having one argument as required byoptim:2) dynamic body + dynamic args In the above we dynamically created the body from the string assuming we knew the arguments but if you want to dynamically create both the body and arguments then try this. Here
f2no longer necessarily has 2 arguments but hasnvarguments and what they are is derived from the inputs.