R-strings

提供:Dev Guides
移動先:案内検索

R-ストリング

Rの単一引用符または二重引用符のペア内に記述された値は、文字列として扱われます。 内部的にRは、単一引用符で作成した場合でも、すべての文字列を二重引用符で囲んで保存します。

文字列の構築に適用されるルール

  • 文字列の先頭と末尾の引用符は、両方とも二重引用符か両方とも単一引用符にする必要があります。 それらを混在させることはできません。
  • 二重引用符は、単一引用符で開始および終了する文字列に挿入できます。
  • 単一引用符は、二重引用符で始まり、二重引用符で終わる文字列に挿入できます。
  • 二重引用符で始まり、二重引用符で終わる文字列に二重引用符を挿入することはできません。
  • 単一引用符は、単一引用符で始まり、単一引用符で終わる文字列に挿入できません。

有効な文字列の例

次の例は、Rでの文字列の作成に関するルールを明確にします。

a <- 'Start and end with single quote'
print(a)

b <- "Start and end with double quotes"
print(b)

c <- "single quote ' in between double quotes"
print(c)

d <- 'Double quotes " in between single quote'
print(d)

上記のコードを実行すると、次の出力が得られます-

[1] "Start and end with single quote"
[1] "Start and end with double quotes"
[1] "single quote ' in between double quote"
[1] "Double quote \" in between single quote"

無効な文字列の例

e <- 'Mixed quotes"
print(e)

f <- 'Single quote ' inside single quote'
print(f)

g <- "Double quotes " inside double quotes"
print(g)

スクリプトを実行すると、以下の結果が表示されて失敗します。

Error: unexpected symbol in:
"print(e)
f <- 'Single"
Execution halted

文字列操作

文字列の連結-paste()関数

Rの多くの文字列は、* paste()*関数を使用して結合されます。 任意の数の引数を組み合わせて組み合わせることができます。

構文

貼り付け機能の基本的な構文は次のとおりです-

paste(..., sep = " ", collapse = NULL)

以下は、使用されるパラメータの説明です-

  • …​ は、結合される任意の数の引数を表します。
  • sep は、引数間の区切り文字を表します。 オプションです。
  • collapse は、2つの文字列の間のスペースを削除するために使用されます。 しかし、1つの文字列の2つの単語内のスペースではありません。

a <- "Hello"
b <- 'How'
c <- "are you? "

print(paste(a,b,c))

print(paste(a,b,c, sep = "-"))

print(paste(a,b,c, sep = "", collapse = ""))

上記のコードを実行すると、次の結果が生成されます-

[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "

数字と文字列のフォーマット-format()関数

  • format()*関数を使用して、数字と文字列を特定のスタイルにフォーマットできます。

構文

フォーマット機能の基本的な構文は次のとおりです-

format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))

以下は、使用されるパラメータの説明です-

  • x はベクトル入力です。
  • digits は、表示される合計桁数です。
  • nsmall は、小数点の右側の最小桁数です。
  • 科学表記法を表示するには、 scientific をTRUEに設定します。
  • width は、先頭に空白を埋めることによって表示される最小幅を示します。
  • justify は、文字列を左、右、または中央に表示します。

# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.
result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)

# Left justify strings.
result <- format("Hello", width = 8, justify = "l")
print(result)

# Justfy string with center.
result <- format("Hello", width = 8, justify = "c")
print(result)

上記のコードを実行すると、次の結果が生成されます-

[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "

文字列の文字数を数える-nchar()関数

この関数は、文字列内のスペースを含む文字数をカウントします。

構文

nchar()関数の基本的な構文は次のとおりです-

nchar(x)

以下は、使用されるパラメータの説明です-

  • x はベクトル入力です。

result <- nchar("Count the number of characters")
print(result)

上記のコードを実行すると、次の結果が生成されます-

[1] 30

大文字と小文字の変更-toupper()およびtolower()関数

これらの関数は、文字列の文字の大文字と小文字を変更します。

構文

toupper()およびtolower()関数の基本的な構文は-

toupper(x)
tolower(x)

以下は、使用されるパラメータの説明です-

  • x はベクトル入力です。

# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)

上記のコードを実行すると、次の結果が生成されます-

[1] "CHANGING TO UPPER"
[1] "changing to lower"

文字列の一部の抽出-substring()関数

この関数は、文字列の一部を抽出します。

構文

substring()関数の基本的な構文は次のとおりです-

substring(x,first,last)

以下は、使用されるパラメータの説明です-

  • x は文字ベクトル入力です。
  • first は、抽出される最初の文字の位置です。
  • last は、抽出される最後の文字の位置です。

# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)

上記のコードを実行すると、次の結果が生成されます-

[1] "act"