Sunday, 12 August 2018

String - Display and Formatting in R Language

Strings
  • Formatting and Display of Strings
  • Operations with Strings
We need formatting and display of strings to obtain the results of specific operations in required format.

Formatting and Display of Strings

Important commands regarding formatting and display are
print , format , cat and  paste

print function prints its argument.

Usage
print ( )

print ( ) is a generic command that is available for every object class.

Examples:
> print (sqrt(2) )
 [1]  1.414214
> print ( sqrt (2) , digits = 5)
  [1] 1.4142

Format an R object for pretty printing.

Usage
format (x, ...)
x is any R object; typically numeric.

format (x, trim = FALSE, digits = NULL, nsmall = OL, justify = c("left" , "right" , "center" , "none") , width = NULL, . . .)

digits→shows how many significant digits are to be used.
nsmal→shows the minimum number of digits to the right of the decimal point.
justify→provides left-justified (the default), right-justified, or centered.

Examples:
> print (format ( 0.5, digits = 10, nsmall = 15) )
 [1]  "0.500000000000000"


Matrix display

> x <- matrix (nrow = 3, ncol = 2, data = 1:6, byrow = T)
> print (x)
          [,1]   [,2]
[1,]       1      2
[2,]       3      4
[3,]       5      6

Here, a matrix is displayed in the R command window.
One can specify the desired number of digits with the option digits.

The print function has a significant limitation that it prints only one object at a time.

Trying to print multiple items gives error message:

> print ("The zero occurs at", 2*pi, "radians.") Error in print.default("The zero occurs at",2*pi, "radians.") :
     invalid 'quote'  argument

The only way to print multiple items is to print them one at a time

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

The cat function is an alternative to print that lets you combine multiple items into a continuous output.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (364) Python Quiz (25) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses