10
9

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のpaste()(paste0())

Posted at
tags: rBasicLearning

paste()

1. paste()とは

paste()とは, ()の中の要素を文字型に変えて, 繋げて出力する関数です.

1-1. 具体例

paste("a", "b", 3)
> paste("a", "b", 3)
[1] "a b 3"

平均を "mean : 4.6" のように出したい時には以下のように使える.

data <- c(1, 2, 4, 7, 9)

meanData <- mean(data)
paste("mean :", meanData)
> paste("mean :", meanData)
[1] "mean : 4.6"

2. paste(sep="")

sep=""は, "," で区切っている間を" " で区切って出力するのではなく, その他の方法で区切ることを指定する.

2-1. 具体例

paste("a", "b", 3)
paste("a", "b", 3, sep = "-")
> paste("a", "b", 3)
[1] "a b 3"
> paste("a", "b", 3, sep = "-")
[1] "a-b-3"

3. paste()とpaste0()の違い

paste0()は, "," で区切っている間を区切りなしで出力する関数.

3-1. 具体例

paste("a", "b", 3)
paste0("a", "b", 3)
> paste("a", "b", 3)
[1] "a b 3"
> paste0("a", "b", 3)
[1] "ab3"

参考. paste0()をpaste()を用いて表す

paste0()の役割をpaste()で表すと, 以下のようになる.

paste(, sep = "")
paste0()

具体例

paste("a", "b", 3, sep = "")
paste0("a", "b", 3)
> paste("a", "b", 3, sep = "")
[1] "ab3"
> paste0("a", "b", 3)
[1] "ab3"
10
9
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
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?