I am trying to load a matrix and a list into Scilab's environment. The matrix and the list are created by a function(F1). To do this, I know that the matrix and list are defined as output at the beginning/first line of the function. Then, there is another function, F2 that takes these matrix and list for computation. BUT, it seems that the matrix and list are not loaded into the Scilab's environment. Does F1 not outputting the values or F2 which is not accepting the values? I'm I doing it correctly? Please help me out. Thanks for your support.
Is that what you are trying to do ?
function [a,b]= f1()
a=rand(2,2);
b=list(2,"foo");
endfunction
function f2(a,b)
disp(a)
disp(b)
endfunction
[a,b]=f1();
f2(a,b);
Related
I think this should be a simple problem, but I am struggling to find a solution elsewhere. I have a function with several inputs. I would like to be able to find the value of a single input that results in the function returning 0 given constants for the other inputs.
simplified version:
funct = function(x,y) {x+y
}
Given a value of 10 for x, I would like R to solve for what y would need to be to return the value of 0 for the function. Is there a simple way to solve these optimization/solver challenges?
This is the code in R and I'm having trouble understanding the role of function(x) and qdata[[x]] in this line of code. Can someone elaborate me this piece by piece? I didn't write this code. Thank you
outs=lapply(names(qdata[,12:35]), function(x)
hist(qdata[[x]],data=qdata,main="Histogram of Quality Trait",
xlab=as.character(x),las=1.5)$out)
This code generate a series of histograms, one for each of columns 12 to 35 of dataframe qdata. The lapply function iterates over the columns. At each iteraction, the name of the current column is passed as argument "x" to the anonymous function defined by "function(x)". The body of the function is a call to the hist() function, which creates the histogram. qdata[[x]] (where x is the name of a column) extracts the data from that column. I am actually confused by "data=qdata".
We don't have the data object named qdata so we cannot really be sure what will happen with this code. It appears that the author of this code is trying to pass the values of components named outs from function calls to hist. If qdata is an ordinary dataframe, then I suspect that this code will fail in that goal, because the hist function does not have an out component. (Look at the output of ?hist. When I run this with a simple dataframe, I do get histogram plots that appear in my interactive plotting device but I get NULL values for the outs components. Furthermore the 12 warnings are caused by the lack of a data parameter to hte hist function.
qdata <- data.frame(a=rnorm(10), b=rnorm(10))
outs=lapply(names(qdata), function(x)
hist(qdata[[x]],data=qdata,main="Histogram of Quality Trait",
xlab=as.character(x),las=1.5)$out)
#There were 12 warnings (use warnings() to see them)
> str(outs)
List of 2
$ : NULL
$ : NULL
So I think we need to be concerned about the level of R knowledge of the author of this code. It's possible I'm wrong about this presumption. The hist function is generic and it is possible that some unreferenced package has a function designed to handle a data object and retrun an outs value when delivered a vector having a particular class. In a typical starting situation with only the base packages loaded however, there are only three hist.* functions:
methods(hist)
#[1] hist.Date* hist.default hist.POSIXt*
#see '?methods' for accessing help and source code
As far as the questions about the role of function and [[x]]: the keyword function returns a language object that can receive parameter values and then do operations and finally return results. In this case the names get passed to the anonymous function and become, each in turn, the local name, x and the that value is used by the '[['-function to look-up the column in what I am presuming is the ‘qdata’-dataframe.
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.
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 new to R and mostly working with old code written by someone else. And I am trying to create my own R functions.
I found some of the following code used for eigenvalue decomposition.
eigenMatrix = eigen(myMatrix)[[2]]
eigenVals = eigen(myMatrix)[[1]]
Here there is single function that can output 2 different data structures, being, a vector and a matrix depending of the value in the brackets.
When I search of functions with multiple outputs, they usually use lists to output multiple variables at once which does not work, possibly because of different types.
I don't understand why there are two setts of brackets and how the underlying function would work.
The posted code takes the eigen function, which returns a list with 2 values.
Then the [[]] are use to extract the first and second items from the list.
The [[]] is needed to return the underlying structure, and is better explained here: How to Correctly Use Lists in R?
Also, since the eigen function is run twice the code in the question is inefficient.
resultList = eigen(myMatrix)
eigenMatrix = resultList[[2]]
eigenVals = resultList[[1]]
This code is better since eigen is run only once and saves the result of the function as a list and then reads the values from the list.
For the function itself can be coaded as any function with multiple outputs such as here: https://stat.ethz.ch/pipermail/r-help/2007-March/126851.html or here: How to assign from a function with multiple outputs?
The list values can hold any structure and [[]] can be used to return the underlying structure of each value.