Teaching courses in programming language R
With any phrase that is enclosed in two quotation marks (” or ”); In the programming language R, it behaves like a string. In fact, R stores all the strings inside the quotation mark;
Even when you put them in single quotes (ie ”).
Rules applied in string structure
- The mark you put at the beginning and end of the string; Both must be a double quotation mark; Or be a single quotation mark. You cannot use one character as a binary and the other as a single character.
- A double quote can be used for a string that itself is marked with a single quotation mark.
- A single quote can be used for a string that itself is marked with a double quotation mark.
- Binary quotes cannot be used for a string that itself is marked with a double quotation mark.
- A single quote cannot be used for a string that itself is marked with a single quote.
Examples of authorized strings
The following examples better illustrate the rules for creating a string in R.
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)
When we run the above code; The following result is obtained:
[1] “Start and end with single quote”
[1] “Start and end with double quotes”
[1] “single quote ‘in between double quote”
[1] “Double quote \” in between single quote “
Examples of unauthorized strings
e <- ‘Mixed quotes ”
print (e)
f <- ‘Single quote’ inside single quote ‘
print (f)
g <- “Double quotes” inside double quotes ”
print (g)
When we run the above script; The following results could not be provided:
Error: unexpected symbol in:
“Print (e)
f <- ‘Single ”
Execution halted
Manipulation of strings
Connecting strings – paste () function
There are many strings in R that are combined using the paste () function. Using this function, any number of arguments can be combined.
Syntax
You can see the basic syntax for the paste function below:
paste (…, sep = ”“, collapse = NULL)
- . Indicates that any number of arguments can be combine.
- sep represents the separator between arguments. Its placement is optional.
- collapse is used to limit the distance between two strings. But it does not change the distance between two words in a string.
Example
When we run the above code; The following result is obtain:
[1] “Hello How are you? “
[1] “Hello-How-are you? “
[1] “HelloHoware you? “
Formatting numbers and strings – function () format in language R
Numbers and strings can be formatted in a specific style using the format () function
Syntax
The basic syntax for the format function is as follows:
format (x, digits, nsmall, scientific, width, justify = c (“left”, “right”, “center”, “none”))
The following parameters are using:
- x is the input vector.
- digits is the total number of digits shown.
- nsmall is the minimum number of numbers to the right of a decimal point.
- scientific is set to TRUE to display scientific symbols.
- width indicates the transverse minimum using the empty padding spaces at the beginning; is shown.
- justify is used to display strings on the left, right or middle.
Example
# Total number of digits displayed. Last digit rounded off.
result <- format (23.123456789, digits = 9)
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 pad 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)
When we run the above code; The following result is obtained:
[1] “23.1234568”
[1] “6,000,000 e + 00” “1.314521e + 01”
[1] “23.47000”
[1] “6”
[1] “13.7”
[1] “Hello”
[1] “Hello”
Counting the number of characters in a string – ncahr () function in language R
This function is the number of characters in a string; Includes counting their distances.
Syntax
The basic syntax for the nchar () function is as follows:
nchar (x)
nchar (x)
The following are the parameters uses in this syntax:
- x is a vector input.
Example
result <- nchar (“Count the number of characters”)
print (result)
When we execute the above code; The following result is obtain:
[1] 30
Case change – toupper () and tolower () functions
This function changes the characters of a string:
Syntax
The syntax of the toupper () and tolower () functions is as follows:
toupper (x)
tolower (x)
- x is the vector input in the syntax above.
Example
# Changing to Upper case.
result <- toupper (“Changing To Upper”)
print (result)
# Changing to lower case.
result <- tolower (“Changing To Lower”)
print (result)
When we run the above code; The following results are obtain:
[1] “CHANGING TO UPPER”
[1] “changing to lower”
Extract parts of a string-function () substring
This function extracts parts of a string.
Syntax
The basic syntax for the substring () function is as follows:
substring (x, first, last)
The variables used in the above syntax are:
- x is the vector input of the character.
- first position is the first character to be extract.
- last is the position of the last character to be extract.
Example
# Extract characters from 5th to 7th position.
result <- substring (“Extract”, 5, 7)
print (result)
When we run the above code; The following result is obtain:
[1] “act”