Function which is inside another function - r

x<-2, y<-4
sum.xy <- function(x){
function(y){x + y}
}
In the above code, I do not figure out why sum.xy() just give a non-numerical value, can anybody explain?

the ouput of any function in R is either inside return or if there is no return (like in your case) then the last "printed object" is returned. In your, the function sum.xy returns a function: namely the function function(y){x+y}.
You can test this like this:
x<-2; y<-4
sum.xy <- function(x){
function(y){x + y}
}
class(sum.xy)
class(sum.xy(7))
sum.xy(7)
So sum.xy(7) is actually a function which for given y returns y + 7.
If you write sum.xy(7)(4) then 7+4 is returned.
I also recommend that you take a look at this chapter of advanced R.

Related

extracting functionality info of a user defined function

mysum <- function(x,y){
#this is my sum function and it is used for..
#john's updated version, x in int, y in double.
k <- sum(x,y)
return(k)
}
say I have a mysum function and wanted to see the functionality and some basic info, how do one extract the function's info?
Instead of looking into the script directly, would like to have something similar like ?mysum or mysum.info that can print out that two line of comment/info from my console. Thanks.

R recursive function call that works without stating arguments to function

The following simple recursion finds duplicated elements in a vector. It's taken from chapter 2 Functional Programming in R: Advanced Statistical Programming for Data Science, Analysis and Finance by Thomas Mailund. I wonder why it works when we call rest inside the function as it is calling a function without stating arguments.
Usually this would just return the function definition, but in the recursive function we don't need to and I wondered why.
I can see how this would work if we replaced rest in the function directly with find_duplicates(x, i + 1), but I am struggling to see why it works calling just the name which the function is attached to.
E.g if we define f<- function (x) x and call f it just returns the code function (x) x.
find_duplicates <- function(x, i = 1) {
if (i >= length(x)) return(c())
rest <- find_duplicates(x, i + 1)
if (x[i] == x[i + 1]) c(i, rest)
else rest
}
rest is not a function, it's the output of the function find_duplicates given arguments x and i+1.
So indeed it's the same to type rest or find_duplicates(x, i + 1) in the if clause, they're both values, not functions.

Can I call a function in R if it's argument is not defined?

Out of pure curiosity, I want to make a function in R that creates a new variable. I don't want the function to ask the user for input while it is running. Suppose x is not currently a variable. Below is an example of what I want the code to do:
def=function(x){
x=NULL
}
def(x)
x
NULL.
substitute allows you to delay evaluation of the variable, as.character turns it into the typed name, if we then assign it in the parent.frame() to the value, we get what you're after.
def <- function(x) {
y <- substitute(x)
assign(as.character(y),NULL,envir = parent.frame())
}
> def(test)
> test
NULL

When passing a function as an argument to another function, how do I loop over that function?

I currently set up a function which takes another function as an argument. I am trying to create this function to loop over the function it took as an argument.
However, with the way I set it up, it seems like it only really runs the function once, as it should be dependent on a random seed but it keeps coming back with the exact same value.
The only way I found to solve this was to not have the function as an argument but to hard code it in to the function, but this is not really what I want to do.
Here is my code:
runModel <- function(model,runs){
b = c()
for (i in 1:runs){
a <- model$lambda.min
b <- append(b,a)
}
return (c(mean(b),sd(b)))
}
cvModel = cv.glmnet(predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20)
runModel(cvModel,20)
I don't know what are your objects 'predictors' or 'outcome' but it looks like your passing the result of your function cv.glmnet rather than the expression of your function to your runModel function.
One way to run the function at each loop could be to rewrite your function to pass function name and arguments:
runModel <-function(fun,runs,predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20){
b = c()
model <- fun(predictors,outcome,family,alpha,nfolds)
for (i in 1:runs){
a <- model$lambda.min
b <- append(b,a)
}
return (c(mean(b),sd(b)))
}
runModel(cv.glmnet,20, predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20)

R code. write a floor function

I want to write a floor function in R, which returns a floating number to its nearest integer. So I tried the below function. It seems that it works if I assign a value to x and run the code inside the function, but it fails when I try to put everything in a function and call the function name later.
Does anyone know how to fix it?
Thanks!
> my_floor <- function(x) {
x <- x-0.5
as.integer(x)
return (x)
}
> y <- 3.1052255
> my_floor(y)
[1] 2.605225
No very sure what you are trying to do but if you simply want the input transformed to the nearest integer towards zero (i.e. floored as you your question goes), the one way to do it would be:
my_floor <- function(x) {
x <- trunc(x)
return (x)
}
This simply discards the non integer part of your input using R's trunc: which you might as well call directly i.e. trunc(y) will still give you the desired result. If you wish to use your function above "as is" then:
my_floor <- function(x) {
x <- x-0.5
x <- as.integer(x) #Store the result of this second step by reassigning x
return (x)
}

Resources