String :
Any value written within a pair of single quote or double quotes in R is treated as a string. Internally R stores every string within double quotes, even when you create them with single quote.
Rules Applied in String Construction
- The quotes at the beginning and end of a string should be both double quotes or both single quote. They can not be mixed.
- 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.
In [1]:
a = 'Start and end with single quote'
print(a)
b = "Start and end with double quotes"
print(b)
c = "single quote ' in between double quotes"
print(c)
d = 'Double quotes " in between single quote'
print(d)
[1] "Start and end with single quote" [1] "Start and end with double quotes" [1] "single quote ' in between double quotes" [1] "Double quotes \" in between single quote"
In [6]:
a = "Hello"
b = 'How'
c = "are you? "
print(paste(a,b,c))
print(paste(a,b,c, sep = "-"))
print(paste(a,b,c, sep = "", collapse = ""))
[1] "Hello How are you? " [1] "Hello-How-are you? " [1] "HelloHoware you? "
In [9]:
# Total number of digits displayed. Last digit rounded off.
result = format(23.123456789, digits = 4)
print(result)
# Display numbers in scientific notation.
result = format(c(6, 13.14521), scientific = TRUE)
print(result)
# The minimum number of digits to the right of the decimal point.
result = format(23.47, nsmall = 5)
print(result)
# Format treats everything as a string.
result = format(6)
print(result)
# Numbers are padded with blank in the beginning for width.
result = format(13.7, width = 6)
print(result)
# Left justify strings.
result = format("Hello", width = 8, justify = "l")
print(result)
# Justfy string with center.
result = format("Hello", width = 8, justify = "c")
print(result)
[1] "23.12" [1] "6.000000e+00" "1.314521e+01" [1] "23.47000" [1] "6" [1] " 13.7" [1] "Hello " [1] " Hello "
Functions | Description |
---|---|
nchar() | It counts the number of characters in a string or vector. In the stringr package, it’s substitute function is str_length() |
tolower() | It converts a string to the lower case. Alternatively, you can also use the str_to_lower() function |
toupper() | It converts a string to the upper case. Alternatively, you can also use the str_to_upper() function |
chartr() | It is used to replace each character in a string. Alternatively, you can use str_replace() function to replace a complete string |
substr() | It is used to extract parts of a string. Start and end positions need to be specified. Alternatively, you can use the str_sub() function |
setdiff() | It is used to determine the difference between two vectors |
setequal() | It is used to check if the two vectors have the same string values |
abbreviate() | It is used to abbreviate strings. The length of abbreviated string needs to be specified |
strsplit() | It is used to split a string based on a criterion. It returns a list. Alternatively, you can use the str_split() function. This function lets you convert your list output to a character matrix |
sub() | It is used to find and replace the first match in a string |
gsub() | It is used to find and replace all the matches in a string / vector. Alternatively, you can use the str_replace() function |
In [10]:
result = nchar("Count the number of characters")
print(result)
[1] 30
In [2]:
result <- tolower("Changing To Lower")
print(result)
[1] "changing to lower"
In [3]:
result <- toupper("Changing To Upper")
print(result)
[1] "CHANGING TO UPPER"
In [8]:
result <- chartr('o','0',"Welcome to Itronix Solutions")
print(result)
[1] "Welc0me t0 Itr0nix S0luti0ns"
In [10]:
result <- substr("Extract", 5, 7)
print(result)
[1] "act"
In [12]:
setA = c("a", "b", "c", "d", "e")
setB = c("d", "e", "f", "g")
setdiff(setA,setB)
setdiff(setB,setA)
- ‘a’
- ‘b’
- ‘c’
- ‘f’
- ‘g’
In [15]:
a <- c(1,2,3)
b <- c(1,2,100)
setequal(a,b)
FALSE
In [2]:
x <- c("abcdefgh", "efgh", "abceg")
abbreviate(x, 2)
abbreviate(x, 2, strict = TRUE)
- abcdefgh
- ‘abcd’
- efgh
- ‘ef’
- abceg
- ‘abcg’
- abcdefgh
- ‘ab’
- efgh
- ‘ef’
- abceg
- ‘ab’
In [6]:
x <- "Do you wish you were Mr. Jones?"
strsplit(x, " ")
- ‘Do’
- ‘you’
- ‘wish’
- ‘you’
- ‘were’
- ‘Mr.’
- ‘Jones?’
In [7]:
mysentence <- "England is Beautiful. England is not the part of EU"
sub("England", "UK", mysentence)
‘UK is Beautiful. England is not the part of EU’
In [8]:
mysentence <- "England is Beautiful. England is not the part of EU"
gsub("England", "UK", mysentence)
‘UK is Beautiful. UK is not the part of EU’
In [ ]: