AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Create R Strings and use them correctly

PARTAGEZ

Strings correspond to a basic data structure in R language. They are used to represent strings of characters, but also individual letters. Unlike others, the R programming language does not present any data type called “String”: Strings instead belong to the R data type called character.

What are R Strings?

Are you just starting to learn programming? Have you been used to working with different programming languages ​​for a long time? Know that even the most experienced programmers cannot ignore Strings.

Strings correspond to a data structure present in the R programming language. These Strings are actually simple strings of charactersidentified using single or double quotes in most languages, including R programming. Typically, they are used for the purpose of storing and processing non-numeric information directly in programs .

How to create an R String?

With R, it’s easy to create a String with a single line of code. For the initialization of your R String, you can decide to use single or double quotes :

string1 <- "Hello Word!"
# String avec des guillemets simples
string2 <- 'Hello world!'

R

Dealing with R Strings: quick overview of R Strings functions

R provides programmers with a variety of basic functions to enable them to work efficiently with Strings. These functions can be used to carry out different operations on or with character strings. Below are the main R Strings operations:

  • substr() : allows you to extract a substring of characters from an R String
  • paste() : allows you to concatenate Strings
  • tolower() / toupper() : allows you to change all the characters of a String to lowercase or uppercase
  • strsplit() : allows you to divide an R String using a defined separator character
  • trimws() : allows you to remove spaces at the start and end of a String
  • gsub() : allows you to replace patterns within a String by entering a character string
  • nchar() : allows you to determine the length of an R String

If you are used to working with other programming languages, you probably already know most of these functions. For example, there are many equivalent operations in the field of Python Strings.

substr()

With the “substr()” function, you can extract substrings from your R Strings. To do this, fill in the corresponding function as the first parameter of your String, with the start and end indexes of the selected substring as the second and third parameters. Please note: R Strings are indexed from 1, unlike many other languages.

string <- "Hello World"
print(substr(string, start=7, stop=10))

R

In the code example above, “World” appears on the screen.

paste()

In R, the “paste()” function allows you to concatenate two or more strings. Warning: “+” is not an R operator designed for string concatenation, because addition is only defined on numeric data types.

string <- "Hello"
string2 <- "World"
print(paste(string+ string2))

R

If you call “paste()”, the two R Strings concerned are then concatenated, and you obtain the following result: “Hello World”.

tolower() / toupper()

These two R Strings functions, “tolower()” and “toupper()“, allow you to change all the characters in your string to lowercase or uppercase. As for the passing parameter, both functions need the String they need to work with, and then return you a new one.

string <- "Hello World"
print(tolower(string))
print(toupper(string))

R

As expected, the above program first displays “hello world” and then “HELLO WORLD” on the screen. Using functions such as “tolower()” or “toupper()” can be particularly useful when different inputs need to be treated according to their case.

strsplit()

The “strsplit()” function in R may seem familiar to experienced programmers; they are in fact likely to have already encountered it in another language. And for good reason, Python also offers a “split()” function. As for the passing parameter, the “strsplit()” function needs the String you want to split into substrings, as well as the separator to be used for this split. Even in the presence of a single substring, calling the function returns the list of all substrings thus created.

string <- "Hello World"
print(strsplit(string, " "))

R

The output of the code above then looks like this:

[[1]]
[1] "Hello" "World"

R

The list you then obtain contains two Strings, because the space character present in the initial String was used as a separator.

trimws()

The “trimws()” function allows you to remove whitespace characters before and after the relevant R String, which can be particularly useful when processing input by users, who can then remove whitespace characters unwanted (these are often entered by mistake).

string <- "   Hello World   "
print(trimws(string))

R

This section of code returns the String “Hello World” without any whitespace characters at the beginning or end of the string.

gsub()

The “gsub()” function allows you to perform another operation on R Strings; To do this, indicate the substring to be replaced as the first transfer parameter. The second parameter corresponds to the character string intended to replace occurrences of the substring concerned. As for the third transfer parameter, it concerns the String to which the replacement refers.

string <- "Hello World"
print(gsub("World", "User", string))

R

Now, the output of the String thus defined in the code is no longer “Hello World”, but “Hello User”.

nchar()

The “nchar()” function is one of the main standard features offered at the String level, because it refers to the length of an R String.

string <- "Hello World"
print(nchar(string))

R

Don’t let the R command “length()” confuse you, even though it may intuitively make more sense to you: in the R language, “length()” is used to determine the number of elements present in an object; it therefore does not return the length of the R String concerned.

To make your programming projects available, why not use one of the web spaces offered by IONOS and thus benefit from a 30-day “satisfied or refunded” guarantee?

Control characters and escape sequences

You want to exert influence on the text flow of your R Strings (e.g. using line breaks or tabs) ? To do this, you can use command characters. These correspond to a predefined form of escape sequences, most often used to design text output.

It is also possible to display special characters normally marking the start or end of a String as part of R syntax, such as quotation marks, using escape sequences, directly inside character strings. Escape sequences are easily recognizable because they are introduced by a backslash. Below are the main command characters and escape sequences found in the R language:

  • \not : newline/linefeed
  • \t : tab
  • \ : backslash
  • : double quote
  • : single quote

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact