基本的な算術演算子 -その1-

*, +, -, /, ^, %%, %/%

# sample data
x <- mtcars[mtcars["mpg"] >= 30.0,]
x
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

*

乗算

# wt(1000lbs), 1000lbs = 453.592kg
x$wt_kg <- x$wt[] * 453.592
x
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb    wt_kg
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1 997.9024
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2 732.5511
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1 832.3413
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2 686.2847

+

加算

ttl <- x$carb[1] + x$carb[2] + x$carb[3] + x$carb[4] 
ttl
## [1] 6

-

減算

# Civic(mpg) - Corolla(mpg) 
dif <- x$mpg[2] - x$mpg[3]
dif
## [1] -3.5

/

除算

x$kg_hp <- x$wt_kg[] / x$hp[]
x
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb    wt_kg
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1 997.9024
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2 732.5511
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1 832.3413
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2 686.2847
##                    kg_hp
## Fiat 128       15.119733
## Honda Civic    14.087521
## Toyota Corolla 12.805251
## Lotus Europa    6.073316

^

べき算

# sum:合計、mean:平均
x_var <- sum((x$hp[] - mean(x$hp)) ^ 2) / (nrow(x) - 1)
x_var
## [1] 716.6667
var(x$hp)
## [1] 716.6667

*Var関数はn-1で算出される。
詳しくはR-souceの標本分散と不偏分散を参照

%%

剰余

mod <- x[x["gear"] %% 2 == 0,]
mod
##                 mpg cyl disp hp drat    wt  qsec vs am gear carb    wt_kg
## Fiat 128       32.4   4 78.7 66 4.08 2.200 19.47  1  1    4    1 997.9024
## Honda Civic    30.4   4 75.7 52 4.93 1.615 18.52  1  1    4    2 732.5511
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.90  1  1    4    1 832.3413
##                   kg_hp
## Fiat 128       15.11973
## Honda Civic    14.08752
## Toyota Corolla 12.80525

%/%

整数商

quo <- x[x["gear"] %/% 5 >= 1,]
quo
##               mpg cyl disp  hp drat    wt qsec vs am gear carb    wt_kg
## Lotus Europa 30.4   4 95.1 113 3.77 1.513 16.9  1  1    5    2 686.2847
##                 kg_hp
## Lotus Europa 6.073316

abs

絶対値を求める

# sample data
x <- mtcars[mtcars["mpg"] >= 30.0,]
x
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2
x$mpg <- x$mpg[] * -1
x
##                  mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Fiat 128       -32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    -30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla -33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
## Lotus Europa   -30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2
x$mpg <- abs(x$mpg[])
x
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

sign

要素の符号を表すベクトルを返す。(実数の正の場合は1、負の場合は-1、ゼロの場合は0となる)

y <- c(256.5, -893, 0)
sign(y)
## [1]  1 -1  0

acos, asin, atan, atan2

逆余弦、逆正弦、逆正接、2つの引数の逆正接

acos(-1)
## [1] 3.141593
asin(-1)
## [1] -1.570796
atan(1)
## [1] 0.7853982
atan2(2,1)
## [1] 1.107149
plot(acos,1,-1)

sin, cos, tan

正弦、余弦、正接

sin(90)
## [1] 0.8939967
cos(60)
## [1] -0.952413
tan(45)
## [1] 1.619775
最終更新日:2016/04/27

copyrigth © 2016 r-beginners.com All rigths reserved.

PAGE TOP ▲