how to count and convert charecter strings in R Programming

 

Convert Character String to Variable Name in R

 

Method 1: Using assign() function

 We can assign character string to variable name by using assign() function. We simply have to pass the name of the variable and the value to the function.

Syntax: assign(“variable_name”,value)

Parameter:

·         variable_name is the name of the value

·         value is the variable.

# assign variable name to 3 value

assign("variable_name",3)

  # print variable name

We can also create a vector with a group of variables and assign a single variable name.

Example:

 

print(variable_name)

# create 5 variables at a time

assign("vector1",c(1,2,3,4,5))

  # print variable name

print(vector1)

 Method 2: Using do.call() function

This function allows you to call any R function. It allows using a list to hold the arguments of the function along with passing of single arguments.

Syntax:

do.call(“=”,list(“variable_name”, value))

Where “=” is an assign operator

The variable name is the named assigned to the value and value is the input value/variable.

Example:

do.call("=",list("a", 1))

  print(a)

 Count Number of Characters in String in R

 

This can be calculated using the below-mentioned functions:

·         Using nchar() method

·         Using str_length() method

·         Using stri_length() method

Method 1: Using nchar() method.

nchar() method in R programming is used to get the length of a string.

Syntax: nchar(string)

Parameter: string

Return: Returns the length of a string

# R program to calculate length 

# of string

  

# Given String

gfg <- "s3programmingtech"

  

# Using nchar() method

answer <- nchar(gfg)

  print(answer)

This method can be used to return the length of multiple strings passed as a parameter.

nchar(c("Hello World!","Akshit"));

Method 2. Using str_length () method.

The function str_length() belonging to the ‘stringr’ package can be used to determine the length of strings in R.

Syntax: str_length (str)

Parameter: string as str

Return value: Length of string

# R program for finding length

# of string

  # Importing package

library(stringr)

  # Calculating length of string    

str_length("hello")

This method can be used to return the length of multiple strings passed as a parameter.

library(stringr);

  

str_length(c("Hello World!","Akshit"));

Method 3. Using stri_length() method.

This function returns the number of code points in each string.

Syntax: stri_length(str)

Parameter: str as character vector

Return Value: Returns an integer vector of the same length as str.

Note that the number of code points is not the same as the `width` of the string when printed on the console.

library(stringi);

  stri_length(c("Akshit"));

 

Extract Numbers from Character String Vector in R

Method 1: Using gsub() function.

In this method to extract numbers from character string vector, the user has to call the gsub() function which is one of the inbuilt function of R language, and pass the pattern for the first occurrence of the number in the given strings and the vector of the string as the parameter of this function and in return, this function will be returning the first occurred number in the given string to the user.

gsub() function: This function is used to replace find all matches of a string, if the parameter is a string vector, returns a string vector of the same length and with the same attributes. 

Syntax: gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,fixed = FALSE, useBytes = FALSE)

Parameters:

·         pattern: string to be matched, supports regular expression

·         replacement: string for replacement

·         x: string or string vector

·         perl: logical. Should Perl-compatible regexps be used? Has priority overextended

·         fixed: logical. If the TRUE, the pattern is a string to be matched as is.

·         useBytes: logical. If TRUE the matching is done byte-by-byte rather than character-by-character

For finding numbers in the string the pattern will be:

".*?([0-9]+).*"

Example:

gfg <- c("7g8ee6ks1", "5f9o1r0", "geeks10")           

print(gfg)

  res = as.numeric(gsub(".*?([0-9]+).*", "\\1", gfg))             

print(res)

 

Method 2: Using gregexpr() and regmatches() functions

In this method of extracting numbers from character string using gregexpr() and regmatches() function, where the user needs to call these function with specific parameter into it and then in return these function will be returning all digits present in the vectors of strings to the user.

gregexpr() function: This function returns a list of the same length as text each element of which is of the same form as the return value for regexpr, except that the starting positions of every (disjoint) match are given. 

Syntax: gregexpr(pattern, text, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

Parameters:

·         pattern: regular expression, or string for fixed=TRUE

·         text: string, the character vector

·         ignore.case: case sensitive or not

·         perl: logical. Should perl-compatible regexps be used? Has priority over extended

·         fixed: logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments

·         useBytes: logical. If TRUE the matching is done byte-by-byte rather than character-by-character

regmatches() function: This function is used to extract or replace matched sub-strings from match data.

Syntax: regmatches(x, m, invert = FALSE)

Parameters:

·         x:-a character vector

·         m:-an object with match data

·         invert:-a logical: if TRUE, extract or replace the non-matched substrings.

Example:

gfg <- c("7g8ee6ks1", "5f9o1r0", "geeks10")           

  gfg_numbers <- regmatches(gfg, gregexpr("[[:digit:]]+", gfg))

as.numeric(unlist(gfg_numbers))

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction