R: Matematika Dasar

From OnnoWiki
Revision as of 09:43, 28 November 2019 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Contoh operasi matematika

2 ^ 2
2 * 2
2 / 2
2 + 2
2 - 2
4 * 3 ^ 3

vector -4 s/d 3

-4:3

abs remainder

absolute

abs(-4:3)

remainder

(-4:3) %% 2
(-4:3) %/% 3

sign

sign(-4:3)

round

round(7/18,2)
7/118
options(digits=2)
7/118
floor(0.39)
ceiling(0.39)


Trigonometry

sin(pi/2)
tan(pi/4)
cos(pi)


summary functions

sum(1:3)
prod(c(3,5,7))
min(c(1,6,-14,-154,0))
max(c(1,6,-14,-154,0))
range(c(1,6,-14,-154,0))
any(c(1,6,-14,-154,0)<0)
which(c(1,6,-14,-154,0)<0)
all(c(1,6,-14,-154,0)<0) # all checks if criteria is met by each element


Complex

# Plot of Characteristic Function of a U(-1,1) Random Variable
a <- -1; b <- 1
t <- seq(-20,20,.1)
chu <- (exp(1i*t*b)-exp(1i*t*a))/(1i*t*(b-a))
plot(t,chu,"l",ylab=(expression(varphi(t))),main="Characteristic Function of Uniform Distribution [-1, 1]")


# Plot of Characteristic Function of a N(0,1) Variable
mu <- 0; sigma <- 1
t <- seq(-5,5,0.1)
chsnv <- exp(1i*t*mu-.5*(sigma^2)*(t^2))
plot(t,chsnv,"l",ylab=(expression(varphi(t))),main="Characteristic Function of Standard Normal Distribution")


# Plot of Characteristic Function of Poisson Random Variable
lambda <- 6
t <- seq(0,20,.1)
chpois <- exp(lambda*(exp(1i*t)-1))
plot(t,chpois,"l") # Warning omitted and left as exercise
# for the reader to interpret


Spesial

factorial(3)
factorial(3000) # any guess?
lfactorial(3000) #lfactorial helps out
sum(log(3000:2))


stirling <- function(n) {sqrt(2*pi)*n ̂ {n+.5}*exp(-n)}
stirling(100)
factorial(100)/stirling(100)


Pranala Menarik