Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

What is Strings in R

Description

To understand the string concept and its function in R.

Process

String :

  • Value within a pair of single or double quotes.

Rules for string creation :

  • Beginning and end of the string both should be either double quotes or single quotes.No mixing.
  • Double quotes can be inserted into a string starting and ending with single quote
    Single quote can be inserted into a string starting and ending with double quotes.
  • Double quotes can not be inserted into a string starting and ending with double quotes.
  • Single quote can not be inserted into a string starting and ending with single quote.

Concatenating two strings :

  • Strings are combined using the paste() function

Syntax :
paste(arguments,sep=” ”,collapse=NULL)

  • Sep – Separator to be used b/w the strings
  • Collapse – to eliminate the white spaces b/w strings but not within a string


Formatting numbers and strings :

  • To format the numbers and strings in a specific style.For this purpose format() function is used.

Syntax :
fomat(x,digits,nsmall,scientific,width,justify)

x – The variable

digits – Total no.of digits to be displayed

nsmall – minimum digits to the right of the decimal point

scientific – To display the variable in scientific notation

width – to pad blanks in the beginning

justify – justification the the variable to left,right or center

Built in string functions :

substr(x,start,stop) – to get the subring of a string whose position is determined by start and stop values.

Strsplit(x,”split”) – to split the string into multiple substring when encountered with the substring given in double quotes

Toupper() – to convert the string to upper case

Tolower() – to convert the string to lower case

Sub(pattern,replacement,x) – to find a substring given in pattern and replace it with the string given in replacement from a string

Nchar(x) – to find the length of the string

Sapmle Code

Source code : string creation and concatenation
#string creation
a<-“Kaliamman kovil street”
b<-“Virugambakkam”
c<-“chennai”
#concatenating strings
d=paste(a,b,c)
cat(“The address is : “,d )
# comma (‘) as separator b/w strings
d=paste(a,b,c,sep=”,”)
cat(“\nThe address is : “,d )
#eliminating white spaces b/w strings but not within a string
d=paste(a,b,c,sep=””,collapse=””)
cat(“\nThe address is : “,d )

Source code : formatting strings and numbers
x=256.256987456321
#Total number of digits to be printed
print(format(x,digits=10))
#to fix no.of digits after the decimal point
print(format(x,nsmall=3))
#To print the vale in scientific notation
print(format(x,digits=10,nsmall=3,scientific=TRUE))
#Padding blanks in the beginning
print(format(x,nsmall=3,width=10))
#justification of the given argument
print(format(“Welcome”,width = 10,justify=”l”))
print(format(“Welcome”,width = 10,justify=”c”))
print(format(“Welcome”,width = 10,justify=”r”))

Source code : String functions

#inbuilt string functions
str1=readline(prompt=”Enter the string : “)
print(substr(str1,5,10))
#splitting the string
dt=readline(prompt=”Enter the date with / notation : “)
print(strsplit(dt,”/”))
#To upper case
cat(“Converting the string to upper case “,toupper(str1))
#to lower case
cat(“\nConverting the string to lower case”,tolower(str1))
str2=readline(prompt=”Enter the next string : “)
#replacing a substring with another substring
cat(“\nReplacing the string : “,sub(“te”,”ting”,str2),”\n”)
#to find the length of the string
print(str1)
cat(“\nThe length of the above string is “,nchar(str1))

Screenshots
What is Strings in R
string concept and its function in R
Rules for string creation