Strings
paste() glues its arguments together into one string, inserting a separator (a single space by default) between every argument. paste0() is paste() with that separator fixed to an empty string — the one you usually want when you're assembling text without stray spaces. sprintf() gives you printf-style formatting (%.2f for two decimal places, %d for an integer, %s for a string). nchar() returns a string's length in characters, and substr(s, start, stop) extracts the characters from position start through stop, inclusive.
name <- "R"
greeting <- paste("Hello,", name, "!")
print(greeting)
print(paste0("Hello, ", name, "!"))
print(sprintf("Pi is about %.2f", pi))
print(nchar(greeting))
print(substr(greeting, 1, 5))
[1] "Hello, R !" [1] "Hello, R!" [1] "Pi is about 3.14" [1] 10 [1] "Hello"
Notice the stray space before the "!" in the first result — paste()'s default separator was inserted between every pair of arguments, including between name and "!", which is rarely what you want for punctuation. paste0() avoids it entirely by using no separator at all. pi is a built-in constant, and sprintf("%.2f", pi) rounded it to two decimal places as text, not as a numeric rounding operation. Next: control flow — if/else, for, while, and why an explicit for loop is the exception, not the rule, in idiomatic R.
Practice exercise
Create word <- "RProgramming". Print its length using nchar(), then print its first 5 characters using substr(word, 1, 5).