Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Rのpaste()(paste0())

More than 1 year has passed since last update.
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"
Quantas
初心者向けの投稿を多めにしてます!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away