Is there a way to create a function that uses multiple argument, and run the same function independently for each one of them? - r

I'm pretty new in programming, but I'm trying to learn through practice. I'm coding a function which accepts a variable number of arguments, with the ... function. Here's an example of what I've done:
decyph_test = function(...) {
decript = letters[...]
return(decript)
}
If I enter only one argument (in this case, the required argument is a number, 'cause its used to locate that position in the letter constant) there's no big problem. But, what I want to do is to enter multiple numbers, so each one of them is located to its respective letter, and then is compiled into a vector, which would be the return of the function. Is there any specific way in which I can make any individual argument run the same process of locating its letter counterpart?

Related

R: Not to look for variables outside a function if they do not exist within it

This function is OK in R:
f <- function(x) {
x + y
}
Because if the variable y is not defined inside the function f(), R will look for it outside the environment of the function, in its parent environment.
Apart from the fact that this behavior can be a bug generator, what is the point of functions having input parameters? Anyway, all the variables inside a function can be searched outside of it.
Is there any way not to look for variables outside a function if they do not exist within the function?
Some reasons for using parameters that came to my mind:
Without parameters, users have to define variables before using the function, and these variable names need to match the variable names used within the function -- this is impractical.
How is anyone supposed to know/remember the names of the variables within a function? How do I know which variables within a function are purely local, and which variables have to exist outside of the function?
Input parameters can be passed directly as values or as a variable (and the variable name does not matter).
Input parameters communicate the intended usage of the function; it is clear what data is needed to operate it (or at the very least: how many values need to be inserted by the user of the function)
Input parameters can be documented properly using Rd files (or roxygen syntax)
I am sure there are many other reasons to use input parameters.
M. Papenberg provides a very good explanation.
Here's a quick addendum how to make a function not look for objects in parental environments:
Just provide them in the parameter list! This might sound stupid, but that's what you should always do unless you have good reason to do otherwise. In your example only x is passed to the function. So, if the idea here is that x should be returned if y doesn't exist, you can go for default parameters. In this case this could be done as
f <- function(x, y = 0) {
x + y
}

In help files, is there any universal meaning or logic behind the "..." arguments?

I notice in different packages it sometimes refers to a variable, a column to sort by, an object, etc. Like in dplyr it usually refers to a variable, but in ggplot, it's not even used.
Is there any logic behind this? Is there any universality? Or can it just be anything.
It can accept an arbitrary number of formal parameters. In base R these parameters are often arguments to other functions. The Arguments description in the function's help page should indicate which subsequent functions will be getting these arguments. It can be the only argument, the first, the last, or it can be intercalated in the parameter list. The items in the list(...) generally should be named. You can access the items in the "dots" list in a couple of different ways: ...() and list(...) are two that I've seen. Generally, it is the R-cognoscenti that will be designing functions with these forms. When it is intercalated (or the first), the named parameters that follow it in the formals must be named when the function is called and cannot be assigned positionally. If you type ?'...' you will be shown the Reserved page, which in turn has a link to dotsMethods {methods}.
I found it difficult to search for [r] with "..." using a SO search panel but searching on "[r] dotsMethods" brought up 10 hits.

R in BERT won't use na.rm=TRUE in sum function

I installed BERT (R-language to Excel interface). In the functions.R file that is included, i modified the included Add function to use the na.rm argument, as follows.
Add <- function( ... ) {
sum(..., na.rm=TRUE);
}
However, it appears that the na.rm arugment is ignored. That is, the Add() function works fine in Excel if all values in the range are present.
[that is =R.Add(A1:A5) in Excel works fine if all of cells A1:A5 contain values]
But if I delete any value in the range (so the Excel cell is blank), I get #NULL! returned.
Is it possible to utilize the na.rm argument, using BERT so that for R-language functions that have the na.rm argument, it is taken into account and blank cells within the Excel range still compute on the remaining values and do not return #NULL!?
This is a little complicated because the behavior is different if you are passing in one argument (a range) or multiple arguments (individual cells). But in the case of a single argument, if you pass in a range that has empty cells, this will be passed as a list. In that case, you will need to call unlist, e.g.
Add <- function( ... ) {
sum(unlist(...), na.rm=TRUE);
}
Excel can have ranges that include different types (e.g. strings and numbers), but R can't. So when BERT passes data from Excel to R that has mixed types, it uses a list of lists.
There is a hint about this in the console when it runs, which is a good place to start -- it says that the argument is an invalid type (list).
As I said it's complicated because the three-dots argument could refer to multiple arguments, each of which could be a list or a single value (a scalar), each of which could be different types. In that case you'd need to use one of the apply functions to unlist different arguments. But try the above first.

Naming columns of coefficient matrix in a VAR

I am searching for a fast and simple way to give comprehensible names to the columns of a VAR-coefficient matrix.
What I would like to use is the function VAR.names, which is used in the function VAR.est() in the VAR.etp-package. When I use the function VAR.est(), this works perfectly, but as soon as I modify VAR.est (by adding another element to the list of values which are returned), I receive an error message stating "could not find function VAR.names".
I could not find any information on the function VAR.names.
Example:
library(VAR.etp)
data(dat)
M=VAR.est(dat,p=2,type="const")
M$coef
Another possibility would be to use a loop as in the function VAR() from the vars package, but if VAR.names would actually work, this would be a lot more elegant!

Determining argument descriptions within R

I need a way to determine the description of an argument within R.
For example, if I'm using the function qplot() from the package ggplot2, I need to be able to extract a description of each argument in qplot(). From ggplot2's reference manual, it's easy to see that there are several arguments to consider. One of them is called "data", which is described as: "data frame to use (optional). If not specified, will create one, extracting vectors from the current environment."
Is there a way to get this information from within an R session, rather than by reading a reference manual? Maybe an R function similar to packageDescription(), but for a function's arguments?
Thanks!
edit: I found a variant on my question answered here:
How to access the help/documentation .rd source files in R?
Reading the .Rd files seems like the safest way to get the information I need. For anyone interested, the following code returns a list of arguments and their descriptions, where "package_name" can be any package you want:
db <- Rd_db("package_name")
lapply(db, tools:::.Rd_get_metadata, "arguments")
Thank you for your help, everyone.
From the R console in the Mac GUI R.app ... When I look at the text output from help'seq', help_type="text") (which goes to a temporary file) I see that the beginning of hte descriptions you want are demarcated by:
_A_r_g_u_m_e_n_t_s: # Those underscores were ^H's before I pasted
And then the arguments appear in are name:description pairs:
...: arguments passed to or from methods.
from, to: the starting and (maximal) end values of the sequence. Of
length ‘1’ unless just ‘from’ is supplied as an unnamed
argument.
by: number: increment of the sequence.
length.out: desired length of the sequence. A non-negative number,
which for ‘seq’ and ‘seq.int’ will be rounded up if
fractional.
along.with: take the length from the length of this argument.
When I use a Terminal session to get that same output it appears in the same window but as a Unix help page like:
Arguments:
...: arguments passed to or from methods.
from, to: the starting and (maximal) end values of the sequence. Of
length ‘1’ unless just ‘from’ is supplied as an unnamed
argument.
by: number: increment of the sequence.
length.out: desired length of the sequence. A non-negative number,
which for ‘seq’ and ‘seq.int’ will be rounded up if
fractional.
along.with: take the length from the length of this argument.
I believe these are displayed by whatever system program is called by the value of options("pager"). In my case, that is the program "less".

Resources