> x <- 'Hello world!'
> print(x)
[1] "Hello world!"
If quote is set to FALSE then the quotation marks won’t be printed-
> print(x, quote= F)
[1] Hello world!
to print values for specific number of digits-
> print(c(pi,4.783))
[1] 3.141593 4.783000
> print(c(pi,4.783),digits = 15)
[1] 3.14159265358979 4.78300000000000
To show something else in place of NA-
> vac <- c(1,3,5, NA, 6, NA, 47, NA)
> print(vac)
[1] 1 3 5 NA 6 NA 47 NA
> print(vac, na.print="0")
[1] 1 3 5 0 6 0 47 0
> print(vac, na.print="Empty")
[1] 1 3 5 Empty 6 Empty 47 Empty
To change the gap between the outputs-
> vac <- c(1,3,5,6,47)
> print(vac)
[1] 1 3 5 6 47
> print(vac, print.gap= 4)
[1] 1 3 5 6 47
To print right aligned outputs-
> de<- c('Himel','ahsanhimel@email.com')
> print(de)
[1] "Himel" "ahsanhimel@email.com"
> print(de, right= T)
[1] "Himel" "ahsanhimel@email.com"
To define how many values to show-
> s<- c(1:50)
> print(s)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> print(s, max=10)
[1] 1 2 3 4 5 6 7 8 9 10
[ reached getOption("max.print") -- omitted 40 entries ]
> print(s, max=5)
[1] 1 2 3 4 5
[ reached getOption("max.print") -- omitted 45 entries ]
To hide the row numbers set row.names to FALSE-
> sel<-c(1:3)
> names<-c('Himel','Elias','Tonmoy')
> df<- data.frame(sel,names)
> print(df)
sel names
1 1 Himel
2 2 Elias
3 3 Tonmoy
> print(df, row.names=F)
sel names
1 Himel
2 Elias
3 Tonmoy
nonquote() is equivalent to using quote=F in print()-
> str<- "programming with data is fun"
> print(str)
[1] "programming with data is fun"
> noquote(str)
[1] programming with data is fun
"" needs to be used in cat() so that the next object prints on the next line-
> str<- "programming with data is fun"
> for (i in 1:3){
+ cat(str)
+ }
programming with data is funprogramming with data is funprogramming with data is fun
> for (i in 1:3){
+ cat(str,"\n")
+ }
programming with data is fun
programming with data is fun
programming with data is fun
Also the outcome doesn’t show [1] meaning that if it is assigned to any variable then it will print NULL instead of printing out the object-
> a <- print("Print example")
[1] "Print example"
> b <- cat("Cat example")
Cat example
> a
[1] "Print example"
> b # will show NULL
NULL
The argument fill is either a logical variable or a positive number.
The default value is FALSE.
If TRUE, line breaks are set with “\n” or a break in a quoted string.
If fill is a positive number, the number is used to set the width.
> words<- c('a','b','d','e')
> for (i in 1:3){
+ cat(words, fill=F)
+ }
a b d ea b d ea b d e
> for (i in 1:3){
+ cat(words, fill=T)
+ }
a b d e
a b d e
a b d e
> cat(words, fill=1)
a
b
d
e
> cat(words, fill=6)
a b d
e
> cat(words, fill=4)
a b
d e
> cat(month.name[1:4],"\n")
January February March April
> cat(month.name[1:4], sep= ' > ')
January > February > March > April
An application of cat
> n<-c(-10,0,10,100)
> for (i in n){
+ R<-round(rnorm(10,i,1))
+ cat('When Mean is',i,'\nRandom Numbers: ',R,'\n', fill=T)}
When Mean is -10
Random Numbers: -9 -9 -10 -9 -10 -11 -9 -11 -10 -10
When Mean is 0
Random Numbers: -1 -1 0 0 1 0 -1 1 0 1
When Mean is 10
Random Numbers: 11 10 9 10 10 11 11 10 10 9
When Mean is 100
Random Numbers: 99 100 100 98 100 100 100 99 99 100
It converts its arguments to character strings via as.character
> paste('Himel', 4+5, 'Durjoy', 'Rafi') # by default sep=' '
[1] "Himel 9 Durjoy Rafi"
> paste(c(1:5, 4, 99))
[1] "1" "2" "3" "4" "5" "4" "99"
paste0 is equivalent to paste(.., sep=’’)
> paste0('Himel', 4+5,'Durjoy', 'Rafi')
[1] "Himel9DurjoyRafi"
> paste('Himel', 'Durjoy', 'Rafi', sep=', ')
[1] "Himel, Durjoy, Rafi"
> paste(1:3,'YES','NO', sep=' - ')
[1] "1 - YES - NO" "2 - YES - NO" "3 - YES - NO"
> paste(c(1:5, 4, 99), collapse=', ')
[1] "1, 2, 3, 4, 5, 4, 99"
> paste(c(1:5, 4, 99), c('not','done'), collapse=', ')
[1] "1 not, 2 done, 3 not, 4 done, 5 not, 4 done, 99 not"
> paste(c(1:5, 4, 99), c('not','done'), sep=', ')
[1] "1, not" "2, done" "3, not" "4, done" "5, not" "4, done" "99, not"
Using paste inside print()
> print(1+1,8+8,5-2)
[1] 2
> print(paste(1+1,8+8,5-2))
[1] "2 16 3"
> a <- seq(1,12000,900)
> format(a)
[1] " 1" " 901" " 1801" " 2701" " 3601" " 4501" " 5401" " 6301" " 7201"
[10] " 8101" " 9001" " 9901" "10801" "11701"
> format(a, trim=T)
[1] "1" "901" "1801" "2701" "3601" "4501" "5401" "6301" "7201"
[10] "8101" "9001" "9901" "10801" "11701"
> b <- c(1.3,1.43,pi)
> format(b)
[1] "1.300000" "1.430000" "3.141593"
> format(b, digits=3)
[1] "1.30" "1.43" "3.14"
> format(b, digits=12)
[1] "1.30000000000" "1.43000000000" "3.14159265359"
To suggest how many decimal places to show-
> b <- c(1.3,1.43,1.784)
> format(b)
[1] "1.300" "1.430" "1.784"
> format(b, nsmall=5)
[1] "1.30000" "1.43000" "1.78400"
It cannot round the values-
> format(b, nsmall=1)
[1] "1.300" "1.430" "1.784"
It suggests how long the total character string should be-
> print(b)
[1] 1.300 1.430 1.784
> format(b, width=7)
[1] " 1.300" " 1.430" " 1.784"
Like nsmall, it cannot cut any values-
> format(b, width=1)
[1] "1.300" "1.430" "1.784"
> d <- c("a","b","c","drunk")
> format(d, justify = "right")
[1] " a" " b" " c" "drunk"
> format(d, justify = "centre")
[1] " a " " b " " c " "drunk"
Combining with cat we can display it in a more convenient way
> cat(format(d, justify = 'right'), fill=1,"\n")
a
b
c
drunk
> cat(format(d, justify = 'centre'), fill=1,"\n")
a
b
c
drunk
> cat(format(d, justify = 'left'), fill=1,"\n")
a
b
c
drunk
For C like printing function-
> a <- 12; b <- pi; c <- "String";
> sprintf("%g and %.4f are not %s",a,b,c)
[1] "12 and 3.1416 are not String"