| by Arround The Web | No comments

String Contains in R

“The string allows you to access the series’ values as strings and implement a variety of methods on them. The str_contains() method is used to see if a string of a Sequence or Index contains a pattern or regex. If a provided pattern or regex is included inside a string of a Sequence or Index, the method returns a boolean Sequence or Index. This function is sensitive to cases by default. We can also utilize the contains methods with the select command. In R, there is a method called contains(). The string is an object method used to determine whether the string object includes the supplied string object and gives a Boolean response of True or False. In this article, we will explore more about the string contains() method in R language through various instances.”

Syntax of the string contains() Method in R in Ubuntu 20.04?

Here, we have the syntax of the str_conatins() method from the R package.

str_contains(x, pattern, ignore.case = T, logic = “or”, switch = F)

x: A character string is searched for matches. It could also be a long character vector.

pattern: In x, a character string must be matched. It could also be a long character vector.

ignore.case: Whether or not case sensitivity should be used in matching is a logical question.

logic: If a logical union of numerous search patterns should be made, this value indicates whether it should be done.

switch: If TRUE, each element of the pattern will be searched for x. If the switch is TRUE, x must be one byte long.

This function iterates through all of the elements in the pattern, checking to see if any of them are present in any item of x, i.e., which pattern elements are located in the vector x. Iterating a pattern and using grep(x, pattern[i], fixed = TRUE) for every item of the pattern is how it works. If switch = TRUE, loop over the pattern and invoke grep(pattern[i], x, fixed = TRUE) with each item in the pattern.. As a result, x must be one length in the latter instance (if switch = TRUE).

Example # 1: Using the str_conatins() Method to Check String Presence in R in Ubuntu 20.04

We can use the str_contains() method to verify the presence of the string inside the vector. For this, we need to install the “sjmisc” library from R packages. This library will give access to use the str_contains() method.

In the above script, we have first included the sjmisc library. After that, we have a str_conatins() method inside which we have passed the string as the first argument and the substring as the second argument. The str_contains() method searches for this substring from the given string and outputs the TRUE values as the substring is present. We have taken different cases for the str_conatins() method; also, in our third case, we have passed the ignore.case parameter inside the str_conatin() method for the case sensitivity of the string. The logical parameters are also included in the pattern for the substring existence.

Example # 2: Using the dplyr module for the contains() Method in R in Ubuntu 20.04

As in the above example code, we have used the library “sjmisc” for using the str_conatin() method. Now, we are using the contains() method, which is possible by including the dplyr module.

Initially, we inserted the dplyr library and then created the data frame inside the variable data1. The data frame has three fields: id, name, and subject.

We are using the print command where we have called the contains() method in the select command. Inside the select command, we have data1 input, and then the “contains” method is invoked that takes the substring “je” as an input. When the print command is executed, it outputs the “subject” column as the substring is present in the column name “subject.”

As above, we have passed another substring inside the contains() method. The output returned the column that has this particular substring.

Example # 3: Using the contains() Method for the Regular Expression R in Ubuntu 20.04

In this particular example, we have used the “contains” method whose select function contains a given word. We have used the regular expression for searching the particular column.

As you can see, we have selected the data built-in data frame iris and applied the dplyr operator, then defined the select command. The select command is called the contains() method inside it, and in the contains() method, we have used the [pt] along with the word “al,” which interpret as a regular expression. Note that no results are found because regular expressions are not used by this method. Use matches to select with a regexp(), and it generated the columns which matched with the “al” word.

Example # 4: Using the contains() Method to Drop a Column in the Data Frame R in Ubuntu 20.04

We can also use the contains() method to delete any particular column from the data frame by using it with the minus operator.

We have specified the PlantGrowth data frame for this example. The PlantGrowth data frame, when printed, displays all the columns. As this data frame contains only two columns and we can remove one of the columns for this data frame. For this, the select takes the data frame, and the contains() method with the minus operator. Inside the contains() method, we have assigned the “w” word, which is going to be searched by the contains method and then removed from the PlantGrowth data frame.

Hence, the column which contains the word “w” is the weight column and is removed from the data frame successfully.

Conclusion

In this R article, you have learned how to verify if a string includes a particular substring or characters. To examine the existence of characters in a string, we have used the contains() method in R in some distinct ways. With the library “sjmisc” and the “dplyr” module, you can utilize these str_conatins(), and the contains() methods in our script. These functions determine whether the string pattern is present in a string or character vector.

Share Button

Source: linuxhint.com

Leave a Reply