Course
Data Frames
Convert a List to a DataframeCreate an Empty DataframeCombine Two Dataframe into OneChange Column Name of a DataframeExtract Columns From a DataframeDrop Columns in a DataframeReorder Columns in a DataframeSplit DataframeMerge Multiple DataframesDelete Rows From DataframeMake a List of DataframesIntroduction
"Hello World" ProgramAdd Two VectorsFind Sum, Mean and Product of Vector in R ProgrammingTake Input From UserGenerate Random Number from Standard DistributionsSample from a PopulationFind Minimum and MaximumSort a VectorStrings
Concatenate Two StringsFind the Length of a StringCheck if Characters are Present in a StringExtract n Characters From a StringReplace Characters in a StringCompare two StringsConvert Factors to CharactersTrim Leading and Trailing WhitespacesVectors
Concatenate a Vector of StringsCheck if a Vector Contains the Given ElementCount the Number of Elements in a VectorFind Index of an Element in a VectorAccess Values in a VectorAdd Leading Zeros to VectorR Program to Compare two Strings
Example 1: Using toupper() to Compare Two Strings in R
# create two strings
string1 <- "R Programming"
string2 <- "R Programming"
# compare both strings
result <- toupper(string1) == toupper(string2)
print(result)
# Output: [1] TRUE
In the above example, two strings are compared. Here,
- The
toupper()
function converts all the string characters to uppercase. ==
is used to check if both the strings are the same.
Note: You can also use the tolower()
function to convert all the strings to lowercase and perform the comparison.
Example 2: Compare Two Strings Using identical()
str1 <- "Data Science"
str2 <- "Data scientist"
# using identical() to compare two strings
identical(tolower(str1), tolower(str2))
# Output : [1] FALSE
Here, we have passed the identical()
function to compare two strings: str1 and str2.
Inside identical()
, we have passed two strings and converted to lowercase using tolower()
.
Since, "Data Science"
and "Data Scientist"
are two different strings, the method returns FALSE
.