Create a function within a function [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to make a function within a function?
My goal is to insert the function inside the function.

Here is an example of function inside a function
f <- function(n) {
g <- function(x) {
x**n
}
}
such that
> f(3)(4)
[1] 64
But I have no idea what you are exactly after...

Related

How to use the result of lp() as a value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using the lp() function, but would like to use the optimal parameter as an input for following functions. Is there a way to get them?
You could also use missing() to test whether or not the argument b was supplied:
myFunc <- function(a,b){
if(missing(b)) {
a
} else {
a + b
}
}
myFunc(3,1.5)
# [1] 4.5
myFunc(3)
# [1] 3

Creating multiple files in R with a content inside [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm learning R right now but I can't find a solution for my problem. I'd like to create 100 files with a text line inside, let's say "Hello world!". Does anyone know how to do it?
Use a for loop and write_lines for example:
for(i in 1:100) {
write_lines("Hello World", path = sprintf("file%s.txt", i))#
}
or use mapply
mapply(write_lines, path = sprintf("file%s.txt", 1:100), x = "Hello World")

R: Vector Group by Defined group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a vector c("A","B","C",......) and a list list(c("A"),c("B","C"))
I want to get a vector c(1,2,2....)
Is there any function in some basic packages?
we can use merge
merge(stack(setNames(lst, seq_along(lst))), data.frame(values=v1))$ind

Variable path in a loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to get this loop to work. I am trying to save different dataframes with different names at once. I don't know how to get the path part of the code to vary with i:
for (i in specific) {
write.dta(get(paste0("degreeS", i)), "D:/Educacion/PeerEffects/",paste0( "degreeS", i,".dta"))
}
I needed to use the command file.path(). This solved the problem
for (i in specific) {
write.dta(get(paste0("degreeS", i)), file.path("D://Educacion//PeerEffects//", paste0("degreeS", i,".dta")))
}

R: Barplot, add value directly next to bar and a description in a separate column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I achieve somethign like this in R:
like this?
vec <- runif(5)
plot(0,0,type="n",xlim=c(0,1.5),ylim=c(0,7),bty="n",axes="off")
at<-barplot(vec,horiz=TRUE,add=TRUE)
text(vec,at,signif(vec,1),pos=4)
text(rep(1,5),at,c("mn","lu","co","-","my"))

Resources