| by Arround The Web | No comments

Uppercase in R

You may need to convert or modify a string to uppercase at some point. All lower case letters will be changed to capital letters. To transform a string or character to uppercase in R, use the toupper() method. This method is used to convert full strings to uppercase format. When we call the toupper() method, the return value is in string format and has been transformed to uppercase. If there isn’t one, the values are turned into a string. When we set this to a value that is not a string, the toupper() function does not affect the initial string values. For different case scenarios, you could use toupper() and other methods on the String to transform String to uppercase. In this topic, we will explore how to alter the case of a string to uppercase.

How to convert the string or character into upper case in the R in Ubuntu 20.04?

In this post, you will learn how to use R to change the case of string characters in a list to upper case. The toupper() function in R is the best approach to converting the characters in a string of text to uppercase. This method takes a text vector or column as input and converts it to all capital letters. Following that, we’ll show you how to utilize the toupper() method and also a second, lesser-known method in R to change the text to uppercase.

Example # 1: Using the str_detect to check the upper case in R in Ubuntu 20.04:

To detect the existence or absence of a specific pattern in a string, use the str detect() method from the stringr package R. The str detect() method in R is used to see if the substring supplied matches the original string. If a match is found, it will yield TRUE; else, it will yield FALSE for each of the vectors or matrix’s elements. We are going to use this str_detect method to check whether the elements inside the vector are in the upper case.

In the example, we have defined a vector of all alphabetical letters inside the variable x1. When the X1 is printed, it outputs all the letters in the upper case. Then, we have used a str_detect method, and inside this method, the X1 is included as input. Conditions are passed as the second argument, whether the alphabets are in upper case or not. When the str_detect command is executed, it shows all the TRUE values, which means all the alphabets are in upper case. Next, we have also taken some strings which are in lower case inside the vector X2. When we checked the X2 inside the str_detect method, it generated FALSE values as all the strings or characters are in lower case.

Example # 2: Using the toupper() method to convert a string into upper case in R in Ubuntu 20.04:

Using the toupper() method in R, you can transform the lowercase string into an uppercase string. For the transformation of a string’s case, deploy the toupper() method. The toupper() method accepts a string as an input and returns it in uppercase.

In the above example code, we have declared a variable ‘data’ and initialized it with the string. Then, we have another variable ‘converted_data’ to which we have assigned the toupper method. Inside the toupper method, the ‘data’ is passed that stores the string. When the ‘converted_data’ is printed, we have the defined string in the uppercase.

Example # 3: Using the casefold() method to convert the string into upper case in R in Ubuntu 20.04:

R’s casefold() function offers a generalized method for converting letters to lowercase or uppercase. Depending on the value of the argument higher, the casefold() method changes a character vector to upper or lower case. If you specify upper = TRUE, the string will be converted to uppercase; if you specify upper = FALSE, the string will be converted to lowercase.

Here, we have assigned the string which is in the lower case to the variable ‘data’. For the other variable ‘conversion’, we have utilized the casefold method. The casefold method takes the string inside it and mentions the TRUE value to the upper. When the variable ‘conversion’ is printed, it outputs the given string of characters from lower case to upper case.

Example # 4: Using the str_to_title method to convert the first letter of the string in R in Ubuntu 20.04:

We utilize the str to title() function from the “stringr” package to change a column to a title case in R. Let’s look at an example. Let’s look at how to convert R to the title or appropriate case.

We have added the stringr module of R inside our code. Now, we can utilize the str_to_title method for the uppercase conversion. We have defined the string to the variable ‘str’. Then, this str variable is passed in the str_to_title method which modifies the string to the upper case.

Example # 5: Using the dplyr package to convert the first letter of the column to upper case in R in Ubuntu 20.04:

Using dplyr, we turned the initial letter into an uppercase in a single column data set in R.

To begin, we have constructed a data frame that includes a string column. When the data frame is executed, it produces some random names. Due to randomness, this output may differ on your system. After that, we included the dplyr module in our script. Then, use the sub-function along with the mutate function of the dplyr package to convert the first letter into a capital in a string column. Notice that all the first letters of a string in every column are now transformed into the upper case.

Conclusion:

The article is all about the upper case in the R language. R includes a method for converting the lower case of a string, vector, or data frame to the upper case. Use either the toupper() or casefold() methods to transform letter vectors or variables to uppercase. The casefold() method converts characters in vectors from one case to another. For upper case conversion, these strategies are extremely useful. R also has some other functions which are helpful on their own as we have utilized them in the examples.

Share Button

Source: linuxhint.com

Leave a Reply