14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

長いこと知らなかったRの便利関数

Last updated at Posted at 2012-08-17

with関数

with(x, y)で、xの内容を含んだ環境の中でyを評価して返してくれる。attachじゃちょっとやり過ぎという状況で便利。

> with(iris, Sepal.Width + Petal.Width)
  [1] 3.7 3.2 3.4 3.3 3.8 4.3 3.7 3.6 3.1 3.2 3.9 3.6 3.1 3.1 4.2 4.8 4.3 3.8 4.1 4.1 3.6
 [22] 4.1 3.8 3.8 3.6 3.2 3.8 3.7 3.6 3.4 3.3 3.8 4.2 4.4 3.3 3.4 3.7 3.7 3.2 3.6 3.8 2.6
 [43] 3.4 4.1 4.2 3.3 4.0 3.4 3.9 3.5 4.6 4.7 4.6 3.6 4.3 4.1 4.9 3.4 4.2 4.1 3.0 4.5 3.2
 [64] 4.3 4.2 4.5 4.5 3.7 3.7 3.6 5.0 4.1 4.0 4.0 4.2 4.4 4.2 4.7 4.4 3.6 3.5 3.4 3.9 4.3
 [85] 4.5 5.0 4.6 3.6 4.3 3.8 3.8 4.4 3.8 3.3 4.0 4.2 4.2 4.2 3.6 4.1 5.8 4.6 5.1 4.7 5.2
[106] 5.1 4.2 4.7 4.3 6.1 5.2 4.6 5.1 4.5 5.2 5.5 4.8 6.0 4.9 3.7 5.5 4.8 4.8 4.5 5.4 5.0
[127] 4.6 4.8 4.9 4.6 4.7 5.8 5.0 4.3 4.0 5.3 5.8 4.9 4.8 5.2 5.5 5.4 4.6 5.5 5.8 5.3 4.4
[148] 5.0 5.7 4.8

local関数

テンポラリな環境を作って、その中で式を評価する事が出来る。

> x <- "abc"
> z <- 100
> local({
+   x <- 1
+   y <- 2
+   x + y + z
+ })
[1] 103
> x
[1] "abc"
> ls()
[1] "x" "z"

subset関数

データフレームから、特定の条件に合致する部分を抜き出す

> subset(iris, Species=="virginica")
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
101          6.3         3.3          6.0         2.5 virginica
102          5.8         2.7          5.1         1.9 virginica
103          7.1         3.0          5.9         2.1 virginica

14
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?