Membuat fungsi yang menerangkan pendapatan tahunan yang kemudian diubah menjadi pendapatan harian
as_daily_income <- function(yearly_income) {
yearly_income / 365
}
menentukan perhitungan yang akan dilakukan yaitu function(yearly_income)functionyearly_incomeyearly_income. Sangat membantu untuk membedakan antara nilai input dan peran yang akan dimainkan input dalam fungsi tersebut menggunakan kata argumen. contohnya 61362as_yearly_income.Mengikuti kata kunci dan tanda kurung di mana argumen didefinisikan datang sepasang kurung kurawal keriting dan berisi beberapa pernyataan R yaitu function{
as_daily_income <- function(x) {
x / 365
}
Membuat fungsi yang menerangkan pendapatan tahunan yang kemuadian diubah menjadi pendapatan harian
as_daily_income <- function(yearly_income, duration) {
yearly_income / duration
}
as_daily_income <- function(yearly_income, duration = 365) {
yearly_income / duration
}
Akan ada tiga kuantitas yang terlibat bahkan dalam perhitungan sederhana ini: dosis, jumlah waktu sejak dosis diambil, dan apa yang disebut “konstanta waktu” untuk eliminasi obat melalui hati atau mekanisme lain. Berikut adalah berapa banyak obat yang masih ada di dalam tubuh sepuluh hari setelah dosis 100 mg diberikan.
dose <- 100 # mg
duration <- 10 # days
time_constant <- 4 # days
dose * exp(- duration / time_constant)
## [1] 8.2085
drug_remaining <- function(dose, duration, time_constant) {
dose * exp(- duration / time_constant)
}
Operasi program yang dilakukan di R memiliki notasi itu menggunakan karakter tilde. mosaicmosaicCalc~slice_plot()
library(mosaicCalc)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
## Loading required package: mosaicCore
##
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
##
## count, tally
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
##
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
##
## count, tally
##
## Attaching package: 'mosaicCalc'
drug_remaining <- function(dose, duration, time_constant) {
dose * exp(- duration / time_constant)
}
library(mosaicCalc)
slice_plot(
drug_remaining(dose = 100, time_constant = 4, duration = t) ~ t,
domain(t = 0:20))