|R| Experts – Telegram
|R| Experts
1.05K subscribers
376 photos
35 videos
58 files
205 links
@R_Experts
🔴آمار علم جان بخشیدن به داده‌هاست.
🔷ارتباط با ما
@iamrezaei
لینک یوتیوب و اینستاگرام و ویرگول:
https://zil.ink/expertstv
Download Telegram
#Solution_Practice_3

> cc<-c(1,2,3,4,5,2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,5,4,3,2,1)
> A<-matrix(cc,5,5)
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 1 2 3 4
[3,] 3 2 1 2 3
[4,] 4 3 2 1 2
[5,] 5 4 3 2 1
> b<-matrix(c(7,-1,-3,5,17),5,1)
> b
[,1]
[1,] 7
[2,] -1
[3,] -3
[4,] 5
[5,] 17
> x<-solve(A)%*%b
> x
[,1]
[1,] -2
[2,] 3
[3,] 5
[4,] 2
[5,] -4
#factor_function
فرم کلی این تابع به صورت :
factor(x = character(), levels, labels = levels,
exclude = NA, ordered = is.ordered(x))

که از یک رشته شی محتويات انها را خارج و در
Levels
قرار میدهد و میتوان به انها برچسب نیز نسبت داد ، و ordered ترتیب را برای ما مشخص میکند

> mons = c("March","April","January","November","January",
+ "September","October","September","November","August",
+ "January","November","November","February","May","August",
+ "July","December","August","August","September","November",
+ "February","April")
> mons = factor(mons)
> table(mons)
mons
April August December February January July
2 4 1 2 3 1
March May November October September
1 1 5 1 3

ordered=TRUE

ترتیب اعضای فاکتور را مشخص میکند

> fert = c(10,20,20,50,10,20,10,50,20)
> fert = factor(fert,levels=c(10,20,50),ordered=TRUE)
> fert
[1] 10 20 20 50 10 20 10 50 20
Levels: 10 < 20 < 50


@R_Experts
#abs


abs(x)



x: Numeric value, array or vector

> abs(-1)

[1] 1


> abs(20)

[1] 20


> abs(0)

[1] 0


> x <- c(-2,4,0,45,9,-4)
> abs(x)

[1] 2 4 0 45 9 4


> x <- matrix(c(-3,5,-7,1,-9,4),nrow=3,ncol=2,byrow=TRUE)
> abs(x[1,])

[1] 3 5


> abs(x[,1])

[1] 3 7 9



@R_Experts
#atan

atan() function returns the radian arctangent of number data.

atan(x)



x: Numeric value, array or vector

> atan(1)

[1] 0.7853982


> atan(0)

[1] 0


> atan(0.5)

[1] 0.4636476


> x <- c(1, 0, 0.5)
> atan(x)

[1] 0.7853982 0.0000000 0.4636476


@R_Experts
#beta


beta() function return the beta function and the natural logarithm of the beta function.

B(a,b) = Γ(a)Γ(b)/Γ(a+b)

beta(a, b)

a,b: non-negative numeric vectors

> beta(4,9)

[1] 0.0005050505



> x <- c(3,6, 4)
> y <- c(7,4, 12)
> beta(x,y)

[1] 0.0039682540 0.0019841270 0.0001831502


@R_Experts
#choose()



choose(n,r)


n: n elements
r: r subset elements
...

nCr = n!/(r! * (n-r)!)

> choose(5,2)

[1] 10


> choose(2,1)

[1] 2


@R_Experts
#log( )

function computes natural logarithms (Ln) for a number or vector. log10 computes common logarithms (Lg).log2 computes binary logarithms (Log2). log(x,b) computes logarithms with base b.

>log(5)     #ln5

[1] 1.609438


>log10(5) #lg5

[1] 0.69897


>log2(5) #log25

[1] 2.321928


>log(9,base=3) #log39 = 2

[1] 2


>x <- rep(1:12)
>x

[1] 1 2 3 4 5 6 7 8 9 10 11 12


>log(x)

[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101
[8] 2.0794415 2.1972246 2.3025851 2.3978953 2.4849066


>log(x,6)

[1] 0.0000000 0.3868528 0.6131472 0.7737056 0.8982444 1.0000000 1.0860331
[8] 1.1605584 1.2262944 1.2850972 1.3382908 1.3868528

@R_Experts
log10() function computes base 10 logarithm.

log10(x)



x: numeric vector


> log10(100)

[1] 2


> x <- c(100,1000, 10000)
> log10(x)

[1] 2 3 4


@R_Experts
#exp


exp(x) function compute the exponential value of a number or number vector, ex.

> x <- 5

> exp(x)


[1] 148.4132



> y <- rep(1:20)

> exp(y)


         [,1]     [,2]     [,3]     [,4]      [,5]

[1,] 2.718282 20.08554 148.4132 1096.633  8103.084

[2,] 7.389056 54.59815 403.4288 2980.958 22026.466


expm1() function computes exp() minus 1:»>حاصل منهای 1

> expm1(5)


[1] 147.4132



> expm1(rep(1:20))


         [,1]     [,2]     [,3]     [,4]      [,5]

[1,] 1.718282 19.08554 147.4132 1095.633  8102.084

[2,] 6.389056 53.59815 402.4288 2979.958 22025.466


@R_Experts
#factorial

factorial() function computes the factorial of a number.

factorial(x)


x: numeric vector


> factorial(2)  #2 × 1

[1] 2


> factorial(1) #1 × 1

[1] 1


> factorial(3) #3 × 2 × 1

[1] 6


> factorial(4) #4 × 3 × 2 × 1

[1] 24


> factorial(c(4,3,2))

[1] 24 6 2


@R_Experts
#max_min

max() function computes the maximun value of a vector. min() function computes the minimum value of a vector.

max(x,na.rm=FALSE)
min(x,na.rm=FALSE)


• x: number vector
• na.rm: whether NA should be removed, if not, NA will be returned
...

> x <- c(1,2.3,2,3,4,8,12,43,-4,-1)
> max(x)

[1] 43


> min(x)

[1] -4


Missing value affect the results:

> y<- c(x,NA)
> y

[1] 1.0 2.3 2.0 3.0 4.0 8.0 12.0 43.0 -4.0 -1.0 NA

> max(y)

[1] NA

> min(y)

[1] NA


After define na.rm=TRUE, result is meaningful:

> max(y,na.rm=TRUE)

[1] 43



Compare more than 1 vectors:

> x2 <- c(-100,-43,0,3,1,-3)
> min(x,x2)

[1] -100

@R_Experts
#atan2

atan2(y, x) function returns the radian arctangent between the x-axis and the vector from the origin to (x, y).


در مختصات دکارتی این نقطه نسبت به مبدا زاویه ای تولید میکند را تانژانت معکوس رادیانش را بر میگرداند
atans(y, x)


x, y: Numeric value, array or vector

> atan2(2,1)

[1] 1.107149


> y <- c(2,3)
> x <- c(5,6)
> atan2(y,x)

[1] 0.3805064 0.4636476


@R_Experts
#cos


cos() function computes the cosine value of numeric value.

cos(x)

x: Numeric value, array or vector

> cos(pi)

[1] -1


> cos(-pi)

[1] -1


> cos(pi/3)

[1] 0.5


> cos(0)

[1] 1


> x <- c(pi, pi/4, pi/3)
> cos(x)

[1] -1.0000000 0.7071068 0.5000000

@R_Experts
#acos

acos() function returns the radian arccosine of number data.

acos(x)


x: Numeric value, array or vector

> acos(1)

[1] 0


> acos(0)

[1] 1.570796


> x <- c(-1,-0.866025,-0.707107,-0.5,0,0.5,0.707107,0.866025,1)
> acos(x)

[1] 3.1415926 2.6179931 2.3561948 2.0943951 1.5707963 1.0471976 0.7853979
[8] 0.5235996 0.0000000

@R_Experts
#sqrt

sqrt() function computes the square root of a numeric vector.

sqrt(x)


x: numeric or complex vector, array


> sqrt(9)

[1] 3


> sqrt(-1)

[1] NaN
Warning message:
In sqrt(-1) : NaNs produced


> sqrt(3+5i)

[1] 2.101303+1.189738i


> sqrt(c(4,9,16))

[1] 2 3 4
#gamma

gamma() function returns the gamma function Γx.


gamma(x): numeric vectors


> gamma(2)

[1] 1


> gamma(3)

[1] 2


> gamma(4)

[1] 6


> gamma(5)

[1] 24


> gamma(6)

[1] 120


> gamma(c(4,5,6))

[1] 6 24 120
#outer

outer() function applies a function to two arrays.

outer(x, y, FUN="*", ...)
x %o% y


x,y: arrays
FUN: function to use on the outer products, default is multiply
...

>x <- c(1,2.3,2,3,4,8,12,43)
>y<- c(2,4)


Calculate logarithm value of array x elements using array y as bases:

>outer(x,y,"log")

[,1] [,2]
[1,] 0.000000 0.0000000
[2,] 1.201634 0.6008169
[3,] 1.000000 0.5000000
[4,] 1.584963 0.7924813
[5,] 2.000000 1.0000000
[6,] 3.000000 1.5000000
[7,] 3.584963 1.7924813
[8,] 5.426265 2.7131324


Add array x elements with array y elements:

> outer(x,y,"+")

[,1] [,2]
[1,] 3.0 5.0
[2,] 4.3 6.3
[3,] 4.0 6.0
[4,] 5.0 7.0
[5,] 6.0 8.0
[6,] 10.0 12.0
[7,] 14.0 16.0
[8,] 45.0 47.0


Multiply array x elements with array y elements:

> x %o% y  #equal to outer(x,y,"*")

[,1] [,2]
[1,] 2.0 4.0
[2,] 4.6 9.2
[3,] 4.0 8.0
[4,] 6.0 12.0
[5,] 8.0 16.0
[6,] 16.0 32.0
[7,] 24.0 48.0
[8,] 86.0 172.0


Concatenate characters to the array elements:

>z <- c("a","b")
>outer(x,z,"paste")

[,1] [,2]
[1,] "1 a" "1 b"
[2,] "2.3 a" "2.3 b"
[3,] "2 a" "2 b"
[4,] "3 a" "3 b"
[5,] "4 a" "4 b"
[6,] "8 a" "8 b"
[7,] "12 a" "12 b"
[8,] "43 a" "43 b"


@R_Experts
#svd

svd() function computes the singular-value decomposition of a rectangular matrix.

svd(x, nu = min(n, p), nv = min(n, p), LINPACK = FALSE)
La.svd(x, nu = min(n, p), nv = min(n, p))


x: a numeric, logical or complex matrix
nu: the number of left singular vectors to be computed. This must between 0 and n = nrow(x)
nv: the number of right singular vectors to be computed. This must be between 0 and p = ncol(x)
LINPACK: logical. Should LINPACK be used (for compatibility with R < 1.7.0)? In this case nu must be 0, nrow(x) or ncol(x)


> x <- matrix(1:16,4,4)
> x

[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16


> svd(x)

$d
[1] 3.862266e+01 2.071323e+00 2.076990e-15 4.119458e-16

$u
[,1] [,2] [,3] [,4]
[1,] -0.4284124 -0.7186535 0.43803202 0.3288281
[2,] -0.4743725 -0.2738078 -0.82913672 -0.1119477
[3,] -0.5203326 0.1710379 0.34417739 -0.7625890
[4,] -0.5662928 0.6158835 0.04692732 0.5457086

$v
[,1] [,2] [,3] [,4]
[1,] -0.1347221 0.82574206 0.5322301 -0.1293488
[2,] -0.3407577 0.42881720 -0.6132292 0.5691660
[3,] -0.5467933 0.03189234 -0.3702319 -0.7502855
[4,] -0.7528288 -0.36503251 0.4512310 0.3104683

@R_Experts
#diag

diag() function extracts or replaces the diagonal of a matrix, or constructs a diagonal matrix.

diag(x = 1, nrow, ncol)

diag(x) <- value


x: matrix, vector
nrow, ncol: Optional dimensions for the result when x is not a matrix
: either a single value or a vector of length equal to that of the current diagonal. Should be of a mode which can be coerced to that of x
...

> diag(10,3,4)


     [,1] [,2] [,3] [,4]

[1,]   10    0    0    0

[2,]    0   10    0    0

[3,]    0    0   10    0



> diag(3)


     [,1] [,2] [,3]

[1,]    1    0    0

[2,]    0    1    0

[3,]    0    0    1


@R_Experts
#dim


dim() function gets or sets the dimension of a matrix, array or data frame.

dim(x)


x: array, matrix or data frame.

>BOD #R Biochemical Oxygen Demand Dataset

Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8


>class(BOD)

[1] "data.frame"


>dim(BOD) #get dimension

[1] 6 2


Set dimension of a matrix:

>x <- rep(1:20)
>x

[1] 1 2 3 4 5 6 7 8 9 10


Set dimension to 2 × 5:
>dim(x) <- c(2,5)
>x

[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10


@R_Experts