I am building a function in R that calculates the pythagorean of a triangle. The function can take 3 arguments: a,b,c. However, it requires that only 2 of them be inputted, or it throws an error, since a Pythagorean can only be calculated with 2 sides. How do I make a function that 3 inputs but only requires 2?
You can assign default values to arguments within your argument list to prevent the error. In this case, assign NULL to all.
pythag <- function(a=NULL, b=NULL, c=NULL) {
if(length(c(a, b, c))!=2)
stop("You must supply exactly 2 of a, b and c")
...
}
If you wanted to add other checks, such as numeric values, you can do these individually:
if(is.null(a) && is.numeric(b) && is.numeric(c))
return(sqrt(c^2 - b^2))
else if(is.null(b) && is.numeric(a) && is.numeric(c))
return(sqrt(c^2 - a^2))
else if(is.null(c) && is.numeric(a) && is.numeric(b))
return(sqrt(a^2 + b^2))
print("All arguments must be numeric")
}
Related
The common notation for factorial is the ! operator in mathematics.
I can create function:
"%!%" = function(n, r=NULL) { factorial(n); }
This works as expected if I pass a NULL or NA to the RHS, which I don't really want to do.
3 %!% NA
3 %!% NULL
3 %!% .
What I would like to do is just enter:
3 %!%
Any suggestions on HOW I can do that? In that setup, I want the LHS (left) to be the input and the RHS (right) to be ignored.
I can do the BOTH no problem:
nPr = function(n, r, replace=FALSE)
{
if(replace) { return( n^r ); }
factorial(n) / factorial(n-r);
}
"%npr%" = "%nPr%" = nPr;
nCr = function(n, r, replace=FALSE)
{
# same function (FALSE, with n+r-1)
if(replace) { return( nCr( (n+r-1), r, replace=FALSE ) ); }
factorial(n) / ( factorial(r) * factorial(n-r) );
}
"%ncr%" = "%nCr%" = nCr;
where
5 %nCr% 3
5 %nPr% 3
work as expected based on selection without replacement.
Question: How to use the special operator with just the LHS?
The follow-on question is the opposite. Let's say I want the LHS (left) to be ignored and focus on the RHS (right). I believe this is how the built-in ? function links to help and ?? links to help.search(). Let's say I wanted to create an %$$% operator that worked that way.
No, you can't do that. The %any% operators are defined by the parser to be binary operators.
You can see all of the operators in R in the ?Syntax help page. Some are binary, some are unary, and some can be either one, but the unary operators always precede the argument. You can attach different functions to most of them (e.g. change the meaning of ! in !x), but you can't change the parser to allow x! to be legal code.
I want write R code for Pythagoras theorem.
The Pythagorean Theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.
(sideA)^2+(SideB)^2=hypotenuse^2
Now I wrote the R code as below:
pythag<-function(sidea,sideb){
if (sidea>=0&sideb>=0)
hypoteneuse=sqrt(sidea^2+sideb^2)
else if (sidea<0|sideb<0)
hypoteneuse<-"Values Need to be Positive"
else if (!is.vector(x))
hypoteneuse<-"I need numeric values to make this work"
print(hypoteneuse)
}
pythag(4,5)
pythag("A","B")
pythag(-4,-5)
In case of pythag(4,5) it is ok, also pythag(-4,-5) is giving comment "Values Need to be Positive".
But in case of pythag("A","B") I want comment "I need numeric values to make this work", but unfortunately my code does't work for this.
You can try like this:
get_hypotenuse_length <- function(height, base)
{
sides <- c(height, base)
if(any(sides < 0))
{
message("sides must be positive")
} else if(!is.numeric(x = sides))
{
message("sides can not be non-numeric")
} else
{
sqrt(x = sum(sides ^ 2))
}
}
Here's an annotated version. It is creating the function which takes the values a and b and calculates c. It is first testing if the values are numeric, if they are not numeric it will print your error message, otherwise it will ignore what is within those curly brackets and move on to the next test. The second test is checking that both are greater than zero (seeing as a triangle can't have a side of length zero or negative length). If it satifies the condition that both are >0 then it will calculate c, if not it will give the error stating that there are negative values.
# Feed it the values a and b (length of the two sides)
pythag <- function(a,b){
# Test that both are numeric - return error if either is not numeric
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){
return('I need numeric values to make this work')}
# Test that both are positive - return length of hypoteneuese if true...
if(a > 0 & b > 0){
return(sqrt((a^2)+(b^2)))
}else{
# ... give an error either is not positive
return('Values Need to be Positive')
}
}
Here's a more streamlined version:
pythag <- function(a,b){
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){return('I need numeric values to make this work')}
if(a > 0 & b > 0){return(sqrt((a^2)+(b^2)))}
else{return('Values Need to be Positive')}
}
And this is what it returns with your examples:
> pythag(4,5)
[1] 6.403124
> pythag("A","B")
[1] "I need numeric values to make this work"
> pythag(-4,-5)
[1] "Values Need to be Positive"
if x = c("sideA", "sideB"), then it will still be a vector so your test is.vector(x) will return true:
> is.vector(x)
[1] TRUE
But you want to test if it's numbers, so if it's numeric:
> is.numeric(x)
[1] FALSE
I made a function to to compute the sum of I(Xi
my.ecdf<- function(x,y) {
if(!is.null(dim(y)))
stop("y has more than one dimension")
n<-length(x)
i<-1:n
p<-if(x[i]<y) 1 else {
0
}
(sum(p))/n
}
But when I run it with input (rnorm(11),6), I get this error:
Warning message:
In if (x[i] < y) 1 else { :
the condition has length > 1 and only the first element will be used
Any ideas? I'm new to r so sorry if it's something obvious. (Also I don't want to use the for loop)
There are a number of issues in your code:
1) Whats the point of x[1:length(x)] in the if statement? Right now these are meaningless and can be dropped:
n<-length(x)
i<-1:n
x[i]
2) If statement accepts a logical argument not a vector of logical, you can consider adding all() any() etc like
if(all(x < y)) 1 else {0}
or use ifelse() statement for the assignment
3) Finally from what I can understand you overcomplicate things and the whole thing can be written as one-liner:
sum(x < y)/length(x)
This is a logical vector of the same length as y
is.null(dim(y))
You're using it as a logical test. An object with a length greater than 1 can't be unambiguously interpreted by the if statement. Consider if (TRUE FALSE FALSE TRUE) <do something>. When should you do that thing?
If you want to make sure y doesn't have more than one dimension, do
if(length(dim(y)) > 1){
stop("message")
}
In my bisection algorithm, it says that there's a "missing value where true/false is needed", and it points to this line:
if (sign(f(c)) == sign(f(a)) ) {
a <- c
}
Why? There's nothing wrong with that line, and if I replicate it manually, it works just fine. Yet when I run the function, it produces
Error in if (sign(f(c)) == sign(f(a))) { :
missing value where TRUE/FALSE needed
EDIT: Full code is
Bisection <- function(f, a,b, tol = 0.005, maxiter = 1000) {
i <- 1
while (i < maxiter) {
c <- (a+b)/2
if (f(c) == 0 | (b-a)/2 < tol) { return(c)}
i <- i + 1
if (sign(f(c)) == sign(f(a)) ) {
a <- c
}
else {b <- c}}
return(NA)
}
Always, 0 and 100 are used as a and b.
I'm calling the function on different functions f, that are the same except for a different parameter, and it is only for one very particular parameter that the bisection function fails. For all other parameters, the bisection function works fine.
Generally, the missing value where TRUE/FALSE needed means R is encountering an NA value - in this case, I would expect an NA value in either a, c, or whatever f() returns for these values.
It could be that when you run these lines manually, the values stored in your global environment for a and c are just fine (non-missing), but whatever values are passed into your function (or computed within it) contains missing values. I'd recommend checking that.
I am trying to make a function which gets few inputs. I would like to know how to check the availability of my arguments . Here is my function:
MyFunction<-function(data,window,dim,option) {
}
First, I want to see if there is any argument , if no, print an error
is it correct to use
if ~nargin
error('no input data')
}
Then, I want to make sure that the second argument is also inserted
is it right to ask like this
if nargin < 2
error('no window size specified')
}
Then, I want to check if the third argument is empty , set it as 1
if nargin < 3 || isempty(dim)
dim<-1
}
you can use hasArg()
testfunction <- function(x,y){
if(!hasArg(x)){
stop("missing x")
}
if(!hasArg(y)){
y = 3
}
return(x+y)
}
>testfunction(y=2)
Error in testfunction(y = 2) : missing x
> testfunction(x=1,y=2)
[1] 3
> testfunction(x=1)
[1] 4
As #Ben Bolker said , missing is used to test whether a value was specified as an argument to a function. You can have different conditions in your R functions such as warning or stop.
In your case, I would do the following
MyFunction<-function(data,window,dim,option) {
if (missing(data))
stop("the first argument called data is missing")
if (missing(window))
stop("the second argument called window is missing")
if (missing(dim))
dim <- 1
if (missing(option))
stop("the second argument called option is missing")
}