R print() Function
In R, we use the print()
function to print values and variables. For example,
# print values
print("R is fun")
# print variables
x <- "Welcome to Programiz"
print(x)
Output
[1] "R is fun" [1] "Welcome to Programiz"
In the above example, we have used the print()
function to print a string and a variable. When we use a variable inside print()
, it prints the value stored inside the variable.
paste() Function in R
You can also print a string and variable together using the print()
function. For this, you have to use the paste()
function inside print()
. For example,
company <- "Programiz"
# print string and variable together
print(paste("Welcome to", company))
Output
Welcome to Programiz
Notice the use of the paste()
function inside print()
. The paste()
function takes two arguments:
- string - "Welcome to"
- variable - company
By default, you can see there is a space between string Welcome to
and the value Programiz
.
If you don't want any default separator between the string and variable, you can use another variant of paste()
called paste0()
. For example,
company <- "Programiz"
# using paste0() instead of paste()
print(paste0("Welcome to", company))
Output
[1] "Welcome toProgramiz"
Now, you can see there is no space between the string and the variable.
R sprintf() Function
The sprintf()
function of C Programming can also be used in R. It is used to print formatted strings. For example,
myString <- "Welcome to Programiz"
# print formatted string
sprintf("String: %s", myString)
Output
[1] "String: Welcome to Programiz"
Here,
String: %s
- a formatted string%s
- format specifier that represents string valuesmyString
- variable that replaces the format specifier%s
Besides %s
, there are many other format specifiers that can be used for different types values.
Specifier | Value Type |
---|---|
%c |
Character |
%d or %i |
Signed Decimal Integer |
%e or %E |
Scientific notation |
%f |
Decimal Floating Point |
%u |
Unsigned Decimal Integer |
%p |
Pointer address |
Let's use some of them in examples.
# sprintf() with integer variable
myInteger <- 123
sprintf("Integer Value: %d", myInteger)
# sprintf() with float variable
myFloat <- 12.34
sprintf("Float Value: %f", myFloat)
Output
[1] "Integer Value: 123" [1] "Float Value: 12.340000"
R cat() Function
R programming also provides the cat()
function to print variables. However, unlike print()
, the cat()
function is only used with basic types like logical, integer, character, etc.
# print using Cat
cat("R Tutorials\n")
# print a variable using Cat
message <- "Programiz"
cat("Welcome to ", message)
Output
R Tutorials Welcome to Programiz
In the example above, we have used the cat()
function to display a string along with a variable. The \n
is used as a newline character.
Note: As mentioned earlier, you cannot use cat()
with list or any other object.
Print Variables in R Terminal
You can also print variables inside the R terminal by simply typing the variable name. For example,
# inside R terminal
x = "Welcome to Programiz!"
# print value of x in console
x
// Output: [1] "Welcome to Programiz"