Finding minimum and maximum within a function - vector

I want to create a function that takes in a vector and returns the maximum and minimum values.
x<-c(1,2,3,4,5,6,7,8,9)
minmax<- function(x){
minimum<- min(x)
maximum<- max(x)
return(c(min(x),max(x))
}
why does my code say
Error in return(minimum, maximum) :
multi-argument returns are not permitted

If you are using stl you can traverse till the end of the vector by initialsing min_till_now=INT_MAX and max_till_now=INT_MIN

Related

Subscript out of bounds, stopping for loop at the last value

I keep getting the error:
Error in locoh[[i + 1]] : subscript out of bounds
I'm almost certain that its because my its looking for the additional value so length(locoh) +1, but that value does not exist. How could I stop this for loop at the max extent of my object locoh?
# Estimate overlap between LoCoHs for each 10-day interval
for (i in 1:(length(locoh))){
hr_overlap(locoh[[i]], locoh[[i+1]], type = 'hr')
}
For example, I would my for loop to start from the first value in locoh, then locoh+1, locoh + 2, ..... then stop once it reaches locoh_max value. What would be the best way to do this?
Instead of loop to the length, it should be to length(locoh)-1 because locoh[[i+1]] at the last i i.e. length will be out of bounds
for (i in 1:(length(locoh)-1)){
hr_overlap(locoh[[i]], locoh[[i+1]], type = 'hr')
}

How to set a numeric vector of length 0 to just 0?

I have a function where in some iterations I come into situation where I have to return a value of a function at 0 position which returns
funX[0] = numeric(0) `
I understand that, R indexing starts from 1. However, if I could convert these outputs to just simply zero, life would have been easier. I do not find a way around.
Is there any way where such returns would simply be converted as 0?
Addition: I tried to set funX[0] <- 0L in the beginning of the function but it doesn't work.

an error in integrating a function in R

The following code chunk is for defining and integrating a function f1 involving matrix exponentials.
library(expm)
Lambdahat=rbind(c(-0.57,0.21,0.36,0,0),
c(0,-7.02,7.02,0,0),
c(1,0,-37.02,29,7.02),
c(0.03,0,0,-0.25,0.22),
c(0,0,0,0,0));
B=rbind(c(-1,1,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0))
f1<-function(tau1)
{
A=(expm(Lambdahat*tau1)%*%B%*%expm(Lambdahat*(5-tau1)));
return(A[1,5]);
}
out=integrate(f1,lower=0,upper=5)#integration of f1
The integration in the above line gives the following error:
Error in integrate(f1, lower = 0, upper = 5) :
evaluation of function gave a result of wrong length
In addition: Warning messages:
1: In Lambdahat * tau1 :
longer object length is not a multiple of shorter object length
2: In Lambdahat * (t[i] - tau1) :
longer object length is not a multiple of shorter object length
To check for if the function outputs and inputs are of function f1 different length, 10 evenly spaced inputs and corresponding outputs of f1 are reported below. Input and output length for all the test cases were recorded as equal to 1.
sapply(X=seq(from=0,to=5,by=0.5),FUN=f1)
[1] 2.107718e-01 1.441219e-01 0.000000e+00 2.023337e+06 1.709569e+14
[6] 1.452972e+22 1.243012e+30 1.071096e+38 9.302178e+45 8.146598e+53
[11] 7.197606e+61
If anyone could share any hint or directions where the code may be going erroneous, it would be very helpful. Thanks very much!
The problem is that the function passed to integrate need to be vectorized, i.e. it should be able to receive a vector of input values and to return a vector of output values. I think f1 <- Vectorize(f1) could solve your problem.

outer function error: Error in outer function

I need to draw a perspective plot for a homework problem. My function uses two vectors x_dartbrd, y_dartbrd and does a bunch of operations to return another vector of the same length as the arguments. Now when I am trying to run the outer function to generate the z's for the perspective plot it is giving me the following error:
Error in outer(x_dartbrd, y_dartbrd, function(x_dartbrd, y_dartbrd) mapply("DartBoard", :
dims [product 10201] do not match the length of object [1030301]
.
My code is as below:
x_dartbrd = linspace(-170,170,101) #vector of 1st argument
y_dartbrd = linspace(-170,170,101) #vector of 2nd argument
DartBoard = function(x_dartbrd,y_dartbrd)
{
Some specific operations as per the problem
return(Score) #Score is of the same length as 1st and 2nd arguments
}
z <- outer(x_dartbrd, y_dartbrd,function(x_dartbrd,y_dartbrd)mapply("DartBoard",x_dartbrd,y_dartbrd))
This outer function just does not seem to be working.
Appreciate all your suggestions on this.

Error with custom aggregate function for a cast() call in R reshape2

I want to use R to summarize numerical data in a table with non-unique rownames to a result table with unique row-names with values summarized using a custom function. The summarization logic is: use the mean of values if the ratio of the maximum to the minimum value is < 1.5, else use median. Because the table is very large, I am trying to use the melt() and cast() functions in the reshape2 package.
# example table with non-unique row-names
tab <- data.frame(gene=rep(letters[1:3], each=3), s1=runif(9), s2=runif(9))
# melt
tab.melt <- melt(tab, id=1)
# function to summarize with logic: mean if max/min < 1.5, else median
summarize <- function(x){ifelse(max(x)/min(x)<1.5, mean(x), median(x))}
# cast with summarized values
dcast(tab.melt, gene~variable, summarize)
The last line of code above results in an error notice.
Error in vapply(indices, fun, .default) :
values must be type 'logical',
but FUN(X[[1]]) result is type 'double'
In addition: Warning messages:
1: In max(x) : no non-missing arguments to max; returning -Inf
2: In min(x) : no non-missing arguments to min; returning Inf
What am I doing wrong? Note that if the summarize function were to just return min(), or max(), there is no error, though there is the warning message about 'no non-missing arguments.' Thank you for any suggestion.
(The actual table I want to work with is a 200x10000 one.)
Short answer: provide a value for fill as follows
acast(tab.melt, gene~variable, summarize, fill=0)
Long answer:
It appears your function gets wrapped as follows, before being passed to vapply in the vaggregate function (dcast calls cast which calls vaggregate which calls vapply):
fun <- function(i) {
if (length(i) == 0)
return(.default)
.fun(.value[i], ...)
}
To find out what .default should be, this code is executed
if (is.null(.default)) {
.default <- .fun(.value[0])
}
i.e. .value[0] is passed to the function. min(x) or max(x) returns Inf or -Inf on when x is numeric(0). However, max(x)/min(x) returns NaN which has class logical. So when vapply is executed
vapply(indices, fun, .default)
with the default value being is of class logical (used as template by vapply), the function fails when starting to return doubles.
dcast() tries to set the value of missing combination by default value.
you can specify this by fill argument, but if fill=NULL,
then the value returned by fun(0-lenght vector) (i.e., summarize(numeric(0)) here) is used as default.
please see ?dcast
then, here is a workaround:
dcast(tab.melt, gene~variable, summarize, fill=NaN)

Resources