The goal of this tutorial is to learn the basics about the print function
# In this example we will use the open repository of economy longley
data("longley")
str(longley)
## 'data.frame': 16 obs. of 7 variables:
## $ GNP.deflator: num 83 88.5 88.2 89.5 96.2 ...
## $ GNP : num 234 259 258 285 329 ...
## $ Unemployed : num 236 232 368 335 210 ...
## $ Armed.Forces: num 159 146 162 165 310 ...
## $ Population : num 108 109 110 111 112 ...
## $ Year : int 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 ...
## $ Employed : num 60.3 61.1 60.2 61.2 63.2 ...
# Print is a function that is used to send to console the value inside a variable
# It can be used inside of loops or functions to show the result of our actions
# Sometimes we need to print a plot in order to make it appear
# First let's see the effect of print
print("Print function tutorial")
## [1] "Print function tutorial"
# We could store this string in a variable
sentence <- "Print function tutorial"
# And write the content of the variable using its name
sentence
## [1] "Print function tutorial"
# However when we are inside a function the content of the variable may not be displayed
for (i in 1:1){
sentence
}
# Then we have to print the result to see it appear on console
for (i in 1:1){
print(sentence)
}
## [1] "Print function tutorial"
# We can remove the quotes of the string
print(sentence, quote = FALSE)
## [1] Print function tutorial
# We can print a numerical value
print(longley$GNP[1])
## [1] 234.289
# We can use the digits parameter to to specify the number of digits shown on the screen
print(longley$GNP[1], digits = 3)
## [1] 234
# Notice only decimal numbers are cut so a number smaller than the amount of non decimal numbers will do nothing
print(longley$GNP[1], digits = 1)
## [1] 234
# If we want to print to a certain number of decimal numbers instead of showing a total amount of numbers we should use the round function
print(round(longley$GNP[1:5], 1))
## [1] 234.3 259.4 258.1 284.6 329.0
# We can define the Na string when printing
# Let's define a vector including NAs
my_number <- longley$GNP[1:10]
my_number[c(4,7)] <- NA
print(my_number)
## [1] 234.289 259.426 258.054 NA 328.975 346.999 NA 363.112
## [9] 397.469 419.180
# Now we can define the missing value
print(my_number, na.print = "000.000")
## [1] 234.289 259.426 258.054 000.000 328.975 346.999 000.000 363.112
## [9] 397.469 419.180
# Sometimes a vector is too big to print the full vector on screen
# We have several ways to cope with this problem
# Let's print the first 10 elements of the column GNP in several ways
# First we can call only the first 10 elements of the vector
print(longley$GNP[1:10])
## [1] 234.289 259.426 258.054 284.599 328.975 346.999 365.385 363.112
## [9] 397.469 419.180
# We can use the head function to show only the first 10 elements
print(head(longley$GNP, 10))
## [1] 234.289 259.426 258.054 284.599 328.975 346.999 365.385 363.112
## [9] 397.469 419.180
# We can use the max parameter of the print function to do the same
print(longley$GNP, max = 10)
## [1] 234.289 259.426 258.054 284.599 328.975 346.999 365.385 363.112
## [9] 397.469 419.180
## [ reached getOption("max.print") -- omitted 6 entries ]
# We can print a dataframe as well, in this case only the first 6 rows
print(head(longley))
## GNP.deflator GNP Unemployed Armed.Forces Population Year Employed
## 1947 83.0 234.289 235.6 159.0 107.608 1947 60.323
## 1948 88.5 259.426 232.5 145.6 108.632 1948 61.122
## 1949 88.2 258.054 368.2 161.6 109.773 1949 60.171
## 1950 89.5 284.599 335.1 165.0 110.929 1950 61.187
## 1951 96.2 328.975 209.9 309.9 112.075 1951 63.221
## 1952 98.1 346.999 193.2 359.4 113.270 1952 63.639
# We can not print the row names in case we need it
print(head(longley), row.names = FALSE)
## GNP.deflator GNP Unemployed Armed.Forces Population Year Employed
## 83.0 234.289 235.6 159.0 107.608 1947 60.323
## 88.5 259.426 232.5 145.6 108.632 1948 61.122
## 88.2 258.054 368.2 161.6 109.773 1949 60.171
## 89.5 284.599 335.1 165.0 110.929 1950 61.187
## 96.2 328.975 209.9 309.9 112.075 1951 63.221
## 98.1 346.999 193.2 359.4 113.270 1952 63.639
In this tutorial we have learnt how to print different variable types with the configuration that we want to use.