Currently I'm working on a R project which includes following code.
vec <- 1:25
fib <- function(x)
{ if (x==0) return (0)
if (x==1) return (1)
if (x==2) return(2)
return(fib(x-1)+fib(x-2))
}
lapply(vec,fib)
I just want to know that, how does R compute the fibonacci function in a code like this? More simply, when it comes to number 25 in vector "vec" does R compute the whole function, or can R compute the fib(25) using the values of fib(24) and fib(23) since they have been computed already?
It will compute all the recursive values one by one by default, but you can use an external package like memoise to cache previous values, or do it yourself. Have a look at the following blog which shows this using a Fibonacci function as well.
Related
I am trying to make a function in R that calculates the mean of nitrate, sulfate and ID. My original dataframe have 4 columns (date,nitrate, sulfulfate,ID). So I designed the next code
prueba<-read.csv("C:/Users/User/Desktop/coursera/001.csv",header=T)
columnmean<-function(y, removeNA=TRUE){ #y will be a matrix
whichnumeric<-sapply(y, is.numeric)#which columns are numeric
onlynumeric<-y[ , whichnumeric] #selecting just the numeric columns
nc<-ncol(onlynumeric) #lenght of onlynumeric
means<-numeric(nc)#empty vector for the means
for(i in 1:nc){
means[i]<-mean(onlynumeric[,i], na.rm = TRUE)
}
}
columnmean(prueba)
When I run my data without using the function(), but I use row by row with my data it will give me the mean values. Nevertheless if I try to use the function so it will make all the steps by itself, it wont mark me error but it also won't compute any value, as in my environment the dataframe 'prueba' and the columnmean function
what am I doing wrong?
A reproducible example would be nice (although not absolutely necessary in this case).
You need a final line return(means) at the end of your function. (Some old-school R users maintain that means alone is OK - R automatically returns the value of the last expression evaluated within the function whether return() is specified or not - but I feel that using return() explicitly is better practice.)
colMeans(y[sapply(y, is.numeric)], na.rm=TRUE)
is a slightly more compact way to achieve your goal (although there's nothing wrong with being a little more verbose if it makes your code easier for you to read and understand).
The result of an R function is the value of the last expression. Your last expression is:
for(i in 1:nc){
means[i]<-mean(onlynumeric[,i], na.rm = TRUE)
}
It may seem strange that the value of that expression is NULL, but that's the way it is with for-loops in R. The means vector does get changed sequentially, which means that BenBolker's advice to use return(.) is correct (as his advice almost always is.) . For-loops in R are a notable exception to the functional programming paradigm. They provide a mechanism for looping (as do the various *apply functions) but the commands inside the loop exert their effects in the calling environment via side effects (unlike the apply functions).
I'm on a project in remote sensing running on R. I've got a RasterBrick(x) with the raster for all the dates I'm interested in, a Time Serie with the dates corresponding (called time in the function), and a function which works as I want it when processed manually (z is the pixel I want) :
function(x,z)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt)
}
The bfastts function is used to create a ts object containing the values of one pixel along the time serie, the bfast is another processing some statisticals of which I only want one result (this is the third line)? None of this two functions are mine, and they are stable and foundable in the R package repository.
So, I would like to add "another level" of function (sorry for my vocabulary which may not be very precise) which would allow to run this function automatically. My expected result would be a list of the result of the function above, so in other words a list of each pixel's time series.
I've tried this (x is still the RasterBrick) :
function(x)
{
z<-nrow(x)*ncol(x)
j<-last(z[[1]])
l<-vector('list',length = j)
index<-function(x)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt) # this is to add the newly created element to the list
}
lapply(x, FUN='index')
}
but I'm getting an answer that it is not possible to coerce a S4 object to a vector, I guess the problem is in lapply who doesn't like the RasterBrick class... Furthermore I want a list of list in output, and not a list of RasterBrick (I think I understood lapply returns a list of object with the same class as x).
I've tried different workaround, none succesfully, which is not surprising giving my low level in programming, and this one seems to me the closest to what I need. I don't think I fully understand neither how lapply works nor the use of a function in a function.
Thank you very much if you can help me.
Cheers
Guillaume
So, in case it could be useful to someone, here is how I solved this problem (it seems rather very simple finally), the "brick" object is the RasterBrick:
pixelts<- as.list(as.data.frame(t(as.data.frame(brick))))
I am using integrate function (in R) to numerically compute integrals. I have an univariate function with one argument f(x,a) like this (just for example purpose):
test = function(x,a) 1/sqrt(2*pi)*exp(-(x-a)^2/2)
I want to define new univariate function, which is a function of a after integrate the above function:
testa = function(a) integrate(test,0,Inf,a=a)$value #this works
Now my question is that is it possible to use integrate function on function testa ? For example:
integrate(testa,0,1) # not working
I tried and it is not working (got error message evaluation of function gave a result of wrong length). I already know that one can apply multivariate integration procedure directly on test (for example use adaptIntegrate function from cubature package). But that is not my purpose!
So does anyone know how to apply successively integrate function like the example above? Or confirm that this is not permitted in R?
Thank in advance
integrate needs a vectorized function. You can use Vectorize:
integrate(Vectorize(testa),0,1)
#0.6843731905 with absolute error < 0.00000000000022
Disclaimer: I haven't checked the result for correctness.
Is it possible to wrap an R function to amend its functionality?
Here's a toy example to explain what I mean. Consider this function sum2:
sum2 <- function (x) if (length(x) == 1) { cat(x); sum(x) } else sum(x)
It does what sum does, with a tiny modification. Suppose I'd like to redefine sum itself to do what sum2 does here. How can I do this in a general way, without knowing anything about the internals of the function I'm wrapping?
I would like to do this to temporarily "fix" a package function without having to modify and -reinstall the package. I would like to check for its inputs and return a special value in case the input satisfies some condition.
(For those who are deeply familiar with Mathematica, I'm looking for something similar to the Gayley-Villegas trick.)
You need to be careful with this. All packages now have Namespaces and will call the other functions within the same namespace. Your approach will probably work when you call functions from main command prompt. But functions in the package will call the original function, not your modification.
Look at the help for assignInNamespace and related functions for ways to make the changes within the Namespace. The trace function is another way to modify a function in place, adding some additional code to the existing function.
Something along these lines has worked:
sum2 <- sum
sum <- function (x) if (length(x) == 1) { cat(x); sum2(x) } else sum2(x)
What I did not realize is that I could just store the original definition of sum in sum2 so I can call it from the redefined sum.
As Matthew notes this won't override sum when it is called as base::sum.
Most looping code looks like this
retVal=NULL
for i {
for j {
result <- *some function of vector[i] and vector[j]*
retVal = rbind(retVal,result)
}
}
Since this is so common, is there a systematic way of translating this idiom?
Can this be extended to most loops?
The plyr package provides a set of general tools for replacing looping constructs when you're work with a big data structure by breaking it into pieces, processing each piece independently and then joining the results back together.
The first goal should to get working code. You are there. Then try some simple optmizations. E.g.
retVal <- matrix(NA, ni, nj) # assuming your result is scalar
for (i in 1:ni)
for (j in 1:nj)
retVal[i][j] <- *some function of yours*
will already run much faster as you do not reallocate memory for each i,j combination.
As for the looping, you can start by replacing the inner loop with something from the apply family. I am not aware of something fully general to answer your question -- it depends what arguments your function takes and what type of return object it produces.