Monday, November 21, 2011

Print and Cat in R


Print : we use print when we want R to print something.
Cat:  we can use the cat function as an alternative way to ask R to print something.

> pi
[1] 3.141593

> sqrt(2)
[1] 1.414214

> print(pi)
[1] 3.141593

> print(sqrt(2))
[1] 1.414214

> print(matrix(c(1,2,3,4,5,6,7,8),4,4))
     [,1] [,2] [,3] [,4]
[1,]    1    5    1    5
[2,]    2    6    2    6
[3,]    3    7    3    7
[4,]    4    8    4    8

> print(list("a","b","c"))
[[1]]
[1] "a"

[[2]]
[1] "b"

[[3]]
[1] "c"


> print("The zero occurs at"); print(2*pi); print("radians")
[1] "The zero occurs at"
[1] 6.283185
[1] "radians"

> cat("the zero occurs at", 2*pi, "radians.", "\n")   # using (\n) to terminate the line
the zero occurs at 6.283185 radians.

> x<-c(2, 3, 5, 7, 11, 13, 17, 19)
> cat("The prime numbers under 30 are:",x,"...\n")
The prime numbers under 30 are: 2 3 5 7 11 13 17 19 ...



No comments:

Post a Comment