PL/R - Pass entire column as an argument - r

I am trying to write a simple PL/R function that finds the mean of a column (Greenplum 4.3.4 DB)
CREATE OR REPLACE FUNCTION mean_plr(val numeric[]) RETURNS numeric AS
$$
x <- val
avg = mean(x)
return(avg)
$$
LANGUAGE 'plr';
SELECT mean_plr(sensor1) FROM public.tempSensors;
This, however, gives me the error:
ERROR: function mean_plr(numeric) does not exist
LINE 1: SELECT mean_plr(sensor1) FROM public.tempSensors;
^
HINT: No function matches the given name and argument types. You may need to add explicit type casts.

Figured out what I was doing wrong. mean_plr(sensor1) sends 1 value at a time. To send entire column, we need to use array_agg() function.
SELECT mean_plr(array_agg(sensor1::numeric)) FROM public.tempSensors;

Related

Pass in range operator columnName as paramater to a custom function in Kusto

I'm trying to run a custom function over a list of items which are stored in an array and I'm looking at the range operator in KQL.
Here is my code : `
let myfunction = (lot:string){T|where id=lot};
union( range x from 1 to 50 step 1
|invoke myfunction(strcat("test-",x))
)
`
but I'm getting failed message that cant resolve scalar named 'x'. What I would like to do is repeat myfunction for 50 times and then use union to merge the results of each iteration. If this can't be done by using range operator, is there an alternative way to achieve the results that I'm looking for?
Thank you,
The way you chose will not work because the input to the "invoke" operator is a tabular source and the function returns a tabular output.
One way to achieve this in Kusto is by using the python plugin.

Why the code only works with numbers and not letters?

I have to use the code bellow but I don't completely understand how it works. Why it won't work if I change du.4 by du.f and then use the f when calling the function? For some reason it only works with numbers and I do not undarstand why.
This is the error that it is giving in the case of du.f
Error in paste("Meth1=", nr, ".ps", sep = "") : object 'f' not found
du.4 <- function(u,v,a){(exp(a)*(-1+exp(a*v)))/(-exp(a)+exp(a+a*u)-exp(a*(u+v))+exp(a+a*v))}
plotmeth1 <- function(data1,data2,alpha,nr) {
psfile <-paste("Meth1=",nr,".ps",sep="")
diffmethod <-paste("du.",nr,sep="")
title=paste("Family",nr)
alphavalue <-paste("alpha=",round(alpha,digits=3),sep="")
#message=c("no message")
postscript(psfile)
data3<-sort(eval(call(diffmethod,data1,data2,alpha)))
diffdata <-data3[!is.na(data3)]
#if(length(data3)>length(diffdata))
#{message=paste("Family ",nr,"contains NA!")}
tq <-((1:length(diffdata))/(length(diffdata)+1))
plot(diffdata,tq,main=title,xlab="C1[F(x),G(y)]",ylab="U(0,1)",type="l")
legend(0.6,0.3,c(alphavalue))
abline(0,1)
#dev.off()
}
In R, a dot is used as just another character in identifiers. It is often used for clarity but doesn't have a formal function in defining the part after the dot as being in a name-space given by the part of the identifier before the dot. In something like du.f you can't refer to the function by f alone, even if your computation is inside of an environment named du. You can of course define a function named du.4 and then use 4 all by itself, but when you do so you are using the number 4 as just a number and not as a reference to the function. For example, if
du.4 <- function(u,v,a){(exp(a)*(-1+exp(a*v)))/(-exp(a)+exp(a+a*u)-exp(a*(u+v))+exp(a+a*v))}
Then du.4(1,2,3) evaluates to 21.08554 but attempting to use 4(1,2,3) throws the error
Error: attempt to apply non-function
In the case of your code, you are using paste to assemble the function name as a string to be passed to eval. It makes sense to paste the literal number 4 onto the string 'du.' (since the paste will convert 4 to the string '4') but it doesn't make sense to paste an undefined f onto 'du.'. It does, however, make sense to paste the literal string 'f' onto 'du.', so that the function call plotmeth1 (data1, data2, alpha, 'f') will work even though plotmeth1 (data1, data2, alpha, f) will fail.
See this question for more about the use of the dot in R identifiers.

Using an argument to define an extraction operator in user defined function

I have created a function that uses the subset function. How do I assign an argument then have it used after the extraction operator?
Here is what I have now:
function_test<-function(time1,size,Param){
test1_in_equilibrium<-(subset(alldata,Time>=time1 & FinalPopSize==size)$Param)
}
Given the following call:
function_test(100,5000,Time)
I would like R to expand it like so:
test1_in_equilibrium<-(subset(alldata,Time>=time1 & FinalPopSize==size)$Param)
Unfortunately when I attempt to run this function I receive the error object "Time" not found.
I assume I am missing an escape character or something similar but have been unable to find it.
Thanks!
You cannot add the $ operator to a function call and you cannot use a variable with the $ operator.
However, I understand that you want to get the column defined by the input variable Param from the subsetted data.frame. In this case you can easily write the function like this:
function_test <- function(time1,size,Param){
reduced_data <- subset(alldata, Time>=time1 & FinalPopSize==size)
test1_in_equilibrium <- reduced_data[, Param]
}

Method for ascertaining if a value has been given in a function call

I am writing a function, there is one part of the function that can either accept a single value, or a vector, or keeps the default if nothing is specified in the function call.
In the code below "a" is given the default of 0 and then in the function there is an if statement to see if "a" is still 0 or if a value has been assigned to it.
If I call the function using a single value, a vector, or leave it blank it prints the expected result. If the vector that is given to it has a length of more than 1, I get the expected warning message (despite the function still working):
' the condition has length > 1 and only the first element will be used'
My query is therefore what is the best way to test to see if "a" has been left blank and if it has been left blank to run a piece of code (in this case to print "default") or if "a" has been given a value then to run some other code (in this case to print "value given") without having the warning message if a vector is assigned to "a" when I call the function.
The example code is as below:
#Function
ExampleFunction<-function(a=0)
{
if (a==0)
print ("default")
else{print("value given")}
}
#Run function using a vector which gives the warning message
abc<-c(1,2,3)
ExampleFunction(abc)
The warning is seen because in evaluating an if statement, R needs a single Boolean,
not a vector of Booleans, hence the warning seen is seen when you try to send abc <- c(1,2,3) only the first value of vector abc, which is 1 gets assigned to the parameter a in the function. I would suggest you to read about the "Named arguments" in R.
In the meanwhile you could put a check of length of parameter a along with its value to see whether or not it has been assigned to a non-zero value when the function is called. Hope it helps!
Links about Named Arguments: Function default arguments and named values
https://www.r-bloggers.com/r-tip-force-named-arguments/
https://www.safaribooksonline.com/library/view/r-in-a/9781449358204/ch09s05.html

Unexpected R behavior with function parameter

Am R newb. I coded a function that uses 3 parameters. In my code i use one of the parameters to help me read files from a directory. There are 100 files in the directory. The code works fine when I pass it all the function parameters and specify the files i want to read.
functionX(var1, var2, id) and functionX(var1, var2, id = 1:100)
## Below is the first line of code for me that uses "id".
sub.file.names <- file.names[id] ### Get file names
The odd thing is that when a value for "id" is not passed to the function initially (or set with a 1:100 default), the code seems to read all the file names anyway. And it does so even though a value for "id" has never been established.
It's as if R somehow treats the two functions below the same when the user omits passing a value to "id" when executing the function ... eg, functionx("var1", "var2") ## and does not pass any id variable
functionx(var1, var2, id)
functionx(var1, var2, id = 1:100)
Any pointers on why this is happening would be great to know. I feel the answer is obvious, but have not been able to figure it out.
Let me try to explain what is happening with a simple example. Consider the following function
foo = function(i){
LETTERS[i]
}
When you try foo(), you will notice that the function returns all 26 uppercase letters. Why does that happen? Well, everything in R is a function. So when you say LETTERS[i], you are essentially calling the function [. So, the function call is
`[`(LETTERS, i)
Since i is missing, this call is executed as [(LETTERS) (essentially LETTERS[]) which returns all elements of the vector. Note that this occurs because the [ function allows for the i argument to be missing while calling it. Check ?[
If you want the function to act differently when id is missing, either check for missing(id), or explicitly set it to NULL as default. So, if you do
foo2 = function(i = NULL){
LETTERS[i]
}
foo2() will return a zero length character vector.

Resources