R(数学)

2026年2月12日 (木)

[R]パスカルの三角形を作成する

choose関数とsapply関数を組合わせて使う。

> sapply(0:6, function(x) choose(x, 0:x))
[[1]]
[1] 1
[[2]]
[1] 1 1
[[3]]
[1] 1 2 1
[[4]]
[1] 1 3 3 1
[[5]]
[1] 1 4 6 4 1
[[6]]
[1] 1 5 10 10 5 1
[[7]]
[1] 1 6 15 20 15 6 1

2026年1月 6日 (火)

[R]複素数を使う

複素数を扱うには、複素数型ベクトルを使う。complex関数を使うか、+とi演算子を組み合わせることで、作ることができる。Rでは虚数単位をiで表す。iだけではiというオブジェクト(ベクトルやリストのこと)を表すため、虚部が1の場合は1iとする必要がある。complex関数はオプション名を省略すると、第一引数は作成するベクトルの数の指定になることに注意。

> z1 <- 2 + i
エラー: オブジェクト 'i' がありません
> z1 <- 2 + 1i
> z2 <- complex(real = 4, imaginary = 2)
> print(z1)
[1] 2+1i
> print(z2)
[1] 4+2i
> print(z1 + z2)
[1] 6+3i
> mode(z1)
[1] "complex"
> class(z1)
[1] "complex"
> typeof(z1)
[1] "complex"
> complex(real = 6, imaginary = 8)
[1] 6+8i
> complex(4, 6, 8)
[1] 6+8i 6+8i 6+8i 6+8i
> complex(1, c(3, 6), c(9, 12))
[1] 3+ 9i 6+12i
> complex(real = c(3, 6), imaginary = c(9, 12))
[1] 3+ 9i 6+12i

2025年11月24日 (月)

[R]複数の文字列による重複組合せを作成する

重複組合せとは、複数のものから同じものを繰り返しとることを許してとった組合せのこと。例えば、aとbから3個とる重複組合せは、aaa, aab, abb, bbbの4個となる。

gtoolsパッケージのcombinations関数を使う。repeats.allowオプションをTRUEにすること。

> library(gtools)
> ss <- c("a", "b")
> combinations(2, 3, ss, repeats.allow = TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"

2025年9月26日 (金)

[R]逆双曲線関数の値を求める

asinh関数(逆双曲線正弦)、acosh関数(逆双曲線余弦)、atanh関数(逆双曲線正接)をそれぞれ使う。逆双曲線余弦(緑色)は定義域が x > 1、逆双曲線正接(青色)は定義域が -1 < x < 1 であることに注意。

> plot(0, 0, type = "n", xlim = c(-3, 3), ylim = c(-3, 3),
+ xaxs = "i", yaxs = "i", xlab = "x", ylab = "y")
> d <- seq(-3, 3, 0.001)
> lines(d, asinh(d), col = "red")
> d <- seq(1, 3, 0.001)
> lines(d, acosh(d), col = "green")
> d <- seq(-0.999, 0.999, 0.001)
> lines(d, atanh(d), col = "blue")
> abline(h = -3:3, v = -3:3, lty = "dotted")
> text(-2.9, 2.8, expression("y =" * sinh^-1 * "(x)"), adj = 0, col = "red")
> text(-2.9, 2.5, expression("y =" * cosh^-1 * "(x)"), adj = 0, col = "green")
> text(-2.9, 2.2, expression("y =" * tanh^-1 * "(x)"), adj = 0, col = "blue")

R_inversehyperbolicfunction

計算例は、以下のとおり。

> d <- seq(-0.8, 0.8, 0.4)
> for (i in 1:length(d)) cat(sprintf("asinh(%4.1f) = %f\n", d[i], asinh(d[i])))
asinh(-0.8) = -0.732668
asinh(-0.4) = -0.390035
asinh( 0.0) = 0.000000
asinh( 0.4) = 0.390035
asinh( 0.8) = 0.732668
> d <- 1:5
> for (i in 1:length(d)) cat(sprintf("acosh(%4.1f) = %f\n", d[i], acosh(d[i])))
acosh( 1.0) = 0.000000
acosh( 2.0) = 1.316958
acosh( 3.0) = 1.762747
acosh( 4.0) = 2.063437
acosh( 5.0) = 2.292432
> d <- seq(-0.8, 0.8, 0.4)
> for (i in 1:length(d)) cat(sprintf("atanh(%4.1f) = %f\n", d[i], atanh(d[i])))
atanh(-0.8) = -1.098612
atanh(-0.4) = -0.423649
atanh( 0.0) = 0.000000
atanh( 0.4) = 0.423649
atanh( 0.8) = 1.098612

2024年4月 2日 (火)

[R]二つの集合MとNの共通部分M∩Nと和集合M∪Nを得る

共通部分(どちらにも属している要素の集合)M∩Nを得るにはintersect関数を、和集合(少なくとも一方に属する要素の集合)M∪Nを得るにはunion関数を得る。以下、例。

> m <- seq(2, 18, by = 2)
> n <- seq(3, 18, by = 3)
> print(m)
[1] 2 4 6 8 10 12 14 16 18
> print(n)
[1] 3 6 9 12 15 18
> union(m, n)
[1] 2 4 6 8 10 12 14 16 18 3 9 15
> intersect(m, n)
[1] 6 12 18

ベクトルの要素に対して行う処理のため、当然、要素が数値以外でもかまわない。

> s1 <- c("スーパークリーク", "メイショウドトウ", "サクラチヨノオー")
> s2 <- c("メイショウドトウ", "サクラチヨノオー", "メジロアルダン")
> union(s1, s2)
[1] "スーパークリーク" "メイショウドトウ" "サクラチヨノオー" "メジロアルダン"
> intersect(s1, s2)
[1] "メイショウドトウ" "サクラチヨノオー"

2023年2月 3日 (金)

[R]等差数列を作成する

seqコマンドを使う。引数は1~3つ与えることができ、それぞれ以下のようになる。最後の例のとおり、最後に公差を加えて末項に一致しない場合は、その1つ前の項までが作成される。

  • seq(初項)
  • seq(初項, 末項) ※公差は1
  • seq(初項, 末項, 公差) ※末項に届かないときはその1つ前の項まで
> seq(3)
[1] 1 2 3
> seq(2, 5)
[1] 2 3 4 5
> seq(3, 12, 4)
[1] 3 7 11

負数や実数の指定も可能。

> seq(-1, -11, -2)
[1] -1 -3 -5 -7 -9 -11
> seq(1.2, 2.8, 0.4)
[1] 1.2 1.6 2.0 2.4 2.8

2022年8月16日 (火)

[R]三角関数の値を求める

cos関数(余弦関数)、sin関数(正弦関数)、tan関数(正接関数)を使う。角θの値が、次の各値の時、cosθ、sinθ、tanθを求めてみる。


θ=0, π/2, π, 2π


> theta <- c(0, pi / 2, pi, 2 * pi)
> cos(theta)
[1] 1.000000e+00 6.123032e-17 -1.000000e+00 1.000000e+00
> sin(theta)
[1] 0.000000e+00 1.000000e+00 1.224606e-16 -2.449213e-16
> tan(theta)
[1] 0.000000e+00 1.633124e+16 -1.224647e-16 -2.449294e-16

~-17や~-16は計算上限りなく小さい値が求まった(≒0.0)ということであり、実際にそのような値が精度良く求まったということではない。~+16(≒∞)も同様である。

2022年8月14日 (日)

[R]組合せの数を求める

choose関数を使う。n個からr個とった組合せの数を求める。5C2(=10)、7C3(=35)、10C2(=45)を求めてみる。

> choose(5, 2)
[1] 10
> choose(7, 3)
[1] 35
> choose(10, 2)
[1] 45

2022年8月 8日 (月)

[R]対数の値を求める

log関数を使う。aを底とするNの対数の求め方は以下のとおり。

log(N, a)

log33(=1)、log51(=0)、log3243(=5)の値をそれぞれ求める。

> log(3, 3)
[1] 1
> log(1, 5)
[1] 0
> log(243, 3)
[1] 5

底は省略することができる。その場合はeを底とする自然対数が求まる。

> log(2)
[1] 0.6931472
> log(2, exp(1))
[1] 0.6931472

常用対数(10を底とする対数)を求めるlog10関数、2を底とする対数を求めるlog2関数もある。

> log10(100)
[1] 2
> log(100, 10)
[1] 2
> log2(20)
[1] 4.321928
> log(20, 2)
[1] 4.321928

2022年7月31日 (日)

[R]順列を求める

Rには標準では順列の総数を求める関数は搭載されていない。階乗を使った式で表すことができるため、階乗を求めるfactorial関数を使用して、順列の総数を求める関数permuを自作する。

> permu <- function(n, r) {factorial(n) / factorial(n - r)}

5P2(=20)、5P3(=60)を求めてみる。

> permu(5, 2)
[1] 20
> permu(5, 3)
[1] 60

なおn > r、0!=1である。n=rの場合は、階乗そのものの計算になる。

より以前の記事一覧

無料ブログはココログ

■■

■■■