Home > OS >  R Combining and creating functions
R Combining and creating functions

Time:11-03

Here's my code

mean <- 100
sd <- 15
Fluorescence_Intensity = rnorm(x, mean, sd)

I would like to create a function that calculates Fluorescence Intensity for me where I can give and change the value of sd mean and distribution for example rnorm to dnorm. As I am new to both R as in this forum, please help

CodePudding user response:

Edit: I added the qnorm function

This should do the job:

Fluorescence_Intensity <- function(X, MEAN, SD, METHOD, LOWER.TAIL=TRUE, LOG.P=FALSE){
  if(METHOD=="rnorm"){
    res <- rnorm(X, MEAN, SD)
    }
  if(METHOD=="dnorm"){
    res <- dnorm(X, MEAN, SD)
    }
  if(METHOD=="qnorm"){
    res <- qnorm(X, MEAN, SD, LOWER.TAIL, LOG.P)
    }
  print(res)
}

Fluorescence_Intensity(X=10, MEAN=100, SD=7, METHOD="rnorm")
Fluorescence_Intensity(X=10, MEAN=100, SD=7, METHOD="dnorm")
Fluorescence_Intensity(X=0.8, MEAN=100, SD=7, METHOD="qnorm")
Fluorescence_Intensity(X=0.8, MEAN=100, SD=7, METHOD="qnorm", LOWER.TAIL=FALSE, LOG.P=FALSE)
  • Related