Created
November 3, 2023 12:31
-
-
Save neelabalan/d839d255e87fe431f7a61d831a728176 to your computer and use it in GitHub Desktop.
R functions - backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_numbers <- function(x, y) { | |
result <- x + y | |
return(result) | |
} | |
sumArray = function(arrayOfNumbers) { | |
result = 0 | |
if(typeof(arrayOfNumbers)=="double") { | |
for(i in c(1:length(arrayOfNumbers))) { | |
result = result + arrayOfNumbers[i] | |
} | |
result | |
} | |
else | |
print("Warning: There is a non-numeric element in your array.") | |
} | |
HM = function(x) { | |
if(prod(x)==0) { | |
return (0) | |
} else { | |
length(x)/sum(1/x) | |
} | |
} | |
GM <- function(x) { | |
if (any(x <= 0)) { | |
stop("All numbers must be positive for the geometric mean.") | |
} | |
return (prod(x)^(1/length(x))) | |
} | |
PM = function(x, p) { | |
if (p ==0) { | |
prod(x)^{1/length(x)} | |
} else { | |
mean(x^p)^{1/p} | |
} | |
} |
R
What is R? Is R is a any programing language?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which programing language it is?