bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Strings

The string is any value written either in a single quote or double quote. Internally R takes single quotes as double quotes only.

Valid Strings

The quotes at the beginning and end of a string should be both single or double quote and cannot be mixed.

Example:

x <- “This is a valid proper ‘ string”

print(x)

y <- ‘this is still valid as this one” double quote is used inside single quotes”

print(y)

Output:

This is a valid proper ‘ string

this is still valid as this single” double quote is used inside single quotes

Invalid Strings

There should not be a single quote in a string which is having double quotes at the beginning and ending and vice versa.

Example:

a<-  ‘Incorrect string”

print(a)

b <- ‘no single quote’ should be present within it’

print(b)

Output:

…: unexpected INCOMPLETE_STRING

…. unexpected symbol

1: b <- ‘no single quote’ should

String Manipulation

Concatenating Strings: paste() function

paste() function combines strings and can take many numbers of arguments.

Syntax

The Basic syntax for paste function is :

paste(x,y,z,sep=” “, collapse=NULL)

  • x, y, z represents any number of arguments
  • sep represents any kind of separator between arguments.
  • collapse represents the elimination of space in between strings and not in between two or more words of the same string.

Example:

x <- “Welcome”

y<- ‘to’

z <- “Intellipaat Services”

print(paste(x, y, z))

print(paste(x,y,z,sep = “_”))

print(paste(x,y,z, sep=””, collapse=””))

Output:

[1] Welcome to Intellipaat Services

[1]  Welcome_to_Intellipaat Services

[1] WelcometoIntellipaat Services

Formatting numbers and strings

Using format() function

Numbers and strings with the help of format() function can be formatted to a specific style.

format(x, digits, nsmall, width, scientific, justify=c(“left”, “centre”, “right”, “none”))

x is a vector, digits are the total number of digits, nsmall is the minimum digits towards right of the decimal point, scientific has TRUE and FALSE to display scientific notation, width is a number of blank spaces at the beginning of a string and justify is to display string towards left, centre or right.

Example:

#illustrating use of digit

dig<- format( 12.3456789, digits = 8)

print(dig)

#illustrating scientific notation

ans <- format(c(5, 13.14521), scientific = TRUE)

print(ans)

#Illustrating justify use of strings

sol <-format”(21.9″, width = 6, justify=l)

print(sol)

Output:

[1] “12.345679”

[1] “5.000000e+00”  “1.314521e+01”

[1] “21.9   “

Counting characters in a string – nchar() function

The function counts the number of blank spaces and characters in a string.

  • nchar(a); where “a” is the vector input

Example:

s <- nchar(“calculating number of charactersis upper case conversion”)”)

print(s)

Output:

[1] 32

Changing the case – toupper() & tolower() functions

This function can change the case of letters of a string.

Example:

#converting to upper case

ans <- toupper(“This is upper case Transform”)

print(ans)

Output:

[1] “THIS IS UPPER CASE TRANSFORM”

Extracting components of a string via substring() function

Syntax:  substring(a, first, last)

  • where a is vector input of character
  • first is the position of “a” character to be extracted from starting.
  • last is the position of “a” character to be extracted till the last character

Example:

#Extracting characters from 3rd to 5th position

sol <- substring(“Welcome”, 3, 5)

print(sol)

Output:

[1] “loc”

"0 Responses on Strings"

Leave a Message

Your email address will not be published.

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 09th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Strings.