Consider this made up function:
Made_up <- function(x) {
one <- x
two <- x + 1
three <- x + 2
}
How can I have the function only print the result of the object three and store the other variables to be called when I write
answer <- Made_up(1)
answer$....
Similar to Vasily A's answer, you could use return and store the results from the function in answer.
Made_up <- function(x) {
one <- x
two <- x + 1
three <- x + 2
print(three)
return(list(one=one, two=two, three=three))
}
answer <- Made_up(1)
answer$one
[1] 1
how about this:
Made_up <- function(x) {
one <- x
two <- x + 1
three <- x + 2
print(three)
invisible(list(one=one, two=two, three=three))
}
Related
I would like to remove from the vector wine below the vector b=c(1,0).
The result should be d=c(1,1,0).
library(gtools)
wine=c(1,1,1,0,0)
x=combinations(5,2,v=wine,set=FALSE,repeats.allowed=FALSE)
y=matrix(NA,nrow(x),3)
I want to find the complementary matrix y of x.
Thanks for your time.
The following uses a function I have posted here. The function finds where in y the vector x occurs returning an index vector into y.
First, get where b occurs in wine. Then the location is used to remove the found vector.
occurs <- function(x, y) {
m <- length(x)
n <- length(y)
candidate <- seq.int(length = n - m + 1L)
for (i in seq.int(length = m)) {
candidate <- candidate[x[i] == y[candidate + i - 1L]]
}
candidate
}
wine <- c(1,1,1,0,0)
b <- c(1,0)
i <- occurs(b, wine)
d <- wine[-(i + seq(b) - 1L)]
d
#[1] 1 1 0
I am trying to save the output of the code below. I know "print" is the problem, but I do not know what works instead.
I generally wonder if there is not another way instead of the for-loop: For each value in the vector (x), I want to draw a new random number (here with runif) and match it to a given value (here for example 0.5). Depending on the result, a new value for x should be stored in a vector x2 (similar to the if-else example below). Waiving the for-loop, I could not find a way to always draw a new random number for each value in vector x.
I would be very grateful for any help!
x <- c(2,2,2,3,3,3)
for(i in x){
if(runif(1) <= 0.5){
print(i + 1)
} else {
print(i)
}
}
Or you could use lapply, then you don't have to modify an object outside your loop each step.
x <- c(2,2,2,3,3,3)
x2 <- unlist(lapply(x, function(x){
if(runif(1) <= 0.5) return(x +1)
return(x)
}))
x2
Try this code:
x <- c(2,2,2,3,3,3)
x2<-NULL
for(i in 1:length(x)){
if(runif(1) <= 0.5){
x2[i]<-1
} else {
x2[i]<-2
}
}
Your output
x2
[1] 1 2 2 1 2 1
In x2 you have random numbers with given values (1 and 2) related to the runif probability.
This is the same thing in a single row:
ifelse(runif(n = length(x))<=0.5,1,2)
[1] 1 2 2 2 1 1
I would like to create a list of functions in R where values from a for loop are stored in the function definition. Here is an example:
init <- function(){
mod <- list()
for(i in 1:3){
mod[[length(mod) + 1]] <- function(x) sum(i + x)
}
return(mod)
}
mod <- init()
mod[[1]](2) # 5 - but I want 3
mod[[2]](2) # 5 - but I want 4
In the above example, regardless of which function I call, i is always the last value in the for loop sequence, I understand this is the correct behavior.
I'm looking for something that achieves this:
mod[[1]] <- function(x) sum(1 + x)
mod[[2]] <- function(x) sum(2 + x)
mod[[3]] <- function(x) sum(3 + x)
You can explicitly ensure i is evaluated at it's current value in the for loop by using force.
init <- function(){
mod <- list()
f_gen = function(i) {
force(i)
return(function(x) sum(i + x))
}
for(i in 1:3){
mod[[i]] <- f_gen(i)
}
return(mod)
}
mod <- init()
mod[[1]](2)
# [1] 3
mod[[2]](2)
# [1] 4
More details are in the Functions/Lazy Evaluation subsection of Advanced R. Also see ?force, of course. Your example is fairly similar to the examples given in ?force.
Using a single-function generator function (f_gen in my code above) seems to make more sense than a list-of-functions generator function. Using my f_gen your code code be simplified:
f_gen = function(i) {
force(i)
return(function(x) sum(i + x))
}
mod2 <- lapply(1:3, f_gen)
mod2[[1]](2)
# [1] 3
mod2[[2]](2)
# [1] 4
## or alternately
mod3 = list()
for (i in 1:3) mod3[[i]] <- f_gen(i)
mod3[[1]](2)
mod3[[2]](2)
This is what I want to obtain, but avoiding to add "e$" before all my variables inside the function. Thanks
e <- globalenv()
f <- function(P) {
e$c <- P + 1
e$d <- c + 2
}
f(2)
It is a very bad idea, but you can do
f <- function(P) {
x <<- P + 1
y <<- x + 2
}
f(2)
I am not using variable c because that is also a (rather important) function name. And it would fail, indeed.
If you need to pass the multiple values returned by the first function (f1) to another function (f2), what you should do is something like:
f1 <- function(P) {
x <- P + 1
y <- x + 2
list(x=x, y=y)
}
f2 <- function(x, y) {
x + y
}
r <- f1(2)
f2(r$x, r$y)
or
r <- f1(2)
do.call(f2, r)
I have multiple user defined functions written in R. I usually source the code and then print the output in R console. My problem is I have 3 function written in one file and all three functions have similar output( here I have z which is common in all three function).. Is there any solution in R where I do not have to type print(z) at the end of every function but after sourcing my code I should be able to print z specific to function?
harry<-function(i){
for(i in 3:5) {
z <- i + 1
print(z)
}
}
harry1<-function(i){
for(i in 1:5) {
z <- i + 1
print(z)
}
}
harry2<-function(i){
for(i in 1:5) {
z <- i + 5
print(z)
}
}
harry <- function(i){
z <- 1 # initialize
for(i in 3:5) {
z[i] <- i + 1 # save to vector
}
return(z) # returns the object z
}
Now you can go:
harry(100)
z <- harry(100)
print(z)
z
To access the same information.
Might I suggest a more general way of doing things?
harry<-function(i,sq){
sapply(sq, function(s,i) {
s + i
}, i=i )
}
harry(i=1,sq=3:5)
harry(i=1,sq=1:5)
harry(i=5,sq=1:5)