This blog postは、どのようなVBA(とJavascript)に似ている、独自の連結演算子を定義することを提案があるが、それはpaste
の力を保持:
"%+%" <- function(...) paste0(..., sep = "")
"Concatenate hits " %+% "and this."
# [1] "Concatenate hits and this."
私もこのソリューションの大ファンではありませんなぜなら、フードの下でpaste
が何をしているのか分かりにくいからです。たとえば、これが起こるのは直感的ですか?例えばJavaScriptで
"Concatenate this string " %+% "with this vector: " %+% 1:3
# [1] "Concatenate this string with this vector: 1"
# [2] "Concatenate this string with this vector: 2"
# [3] "Concatenate this string with this vector: 3"
、これはかなり異なっている、あなたにConcatenate this string with this vector: 1,2,3
を与えるだろう。私はExcelのために話すことができませんが、あなたはこのソリューションが有用であるよりもあなたに混乱を与えないかどうかについて考えなければなりません。
あなたはJavascriptのようなソリューションを必要とする場合は、これを試すことができます。
"%+%" <- function(...) {
dots = list(...)
dots = rapply(dots, paste, collapse = ",")
paste(dots, collapse = "")
}
"Concatenate this string " %+% "with this string."
# [1] "Concatenate this string with this string."
"Concatenate this string " %+% "with this vector: " %+% 1:3
# [1] "Concatenate this string with this vector: 1,2,3"
しかし、私は徹底的にテストしていないので、予期しない結果のために目を光らせて。
'paste'は非常に単純であるだけでなく、'はsprintfは( "値は次のとおりです。%D"、a)は ' –
申し訳ありませんが、答えを投稿する前にあなたのコメントを見ていません... –