Substring() function in R can be used to extract the characters present in the data or to manipulate the given data.
You can easily extract the required characters from a string and Substring() in r can also replace the values in a string.
Syntax for substring r :-
substr
(x,start,stop)
substring
(x,first,last=1000000L)
Here we can do so many things with substring function in r link extracting of values, replacement of values etc.
x = input data / file.
Start / First= starting index of the given substring.
Stop / Last= Ending index of the substring.
r extract substring :-
#returns the characters from 1,11
df<-(
"computer_dev_private_limited"
)
substring
(df,1,11)
OUTPUT : -
"computer_de"
We hope you understand how to r extract part of the data from the given string.
r replace substring with program : -
#returns the string by replacing the _ by space
df<-(
"We are_developers"
)
substring
(df,7,7)=
" "
df
Output = “We are developers”
Put the same value to replace the values in a string with the help of
Substring() function in R.
String replacement using substring() function with multiple value :-
#replaces the 4th letter of each string by $
df<-
c
(
"Alok"
,
"Joseph"
,
"Hayato"
,
"Kelly"
,
"Paloma"
,
"Moca"
)
substring
(df,4,4)<-
c
(
"$"
)
df
Output = “Alo$” “Jos$ph” “Hay$to” “Kel$y” “Pal$ma” “Moc$”
Here the 4th letter in the given strings replaced by the "$" sign with the help of substring r.
Learn to use of str_sub() and substr() function in R :-
well r programming is used to create the data in the form od row or colums.
With the help of given example you can learn to add data in existing data with the help of substr() function in R.
Example for substr() in r : -
#creates the data frame
df<-
data.frame
(Technologies=
c
(
"Datascience"
,
"machinelearning"
,
"Deeplearning"
,
"Artificalintelligence"
),Popularity=
c
(
"70%"
,
"85%"
,
"90%"
,
"95%"
))
df
OUTPUT : -
Example for str_sub() function in R
#using the str_sub function
df$Extracted_Technologies=
str_sub
(df$Technologies,10,15)
> df
0 comments:
Post a Comment