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 Concatenate Two Strings
Concatenate means joining multiple strings into one. In R, we use the paste()
function to concatenate two or more strings.
Example 1: Concatenate Strings in R
# create two strings
string1 <- "Programiz"
string2 <- "Pro"
# using paste() to concatenate two strings
result = paste(string1, string2)
print(result)
Output
[1] "Programiz Pro"
In the above example, we have passed strings: string1 and string2 inside the paste()
function to concatenate two strings.
The default separator in the paste()
function is whitespace " "
. So "Programiz"
and "Pro"
are joined with whitespace in between them.
We can specify our own separator by passing the sep
parameter.
Example 2: Concatenate Strings Using a Separator
# create two strings
string1 = "Programiz"
string2 = "Pro"
# concatenate two strings using separator
result = paste(string1, string2, sep = "-")
print(result)
Output
[1] "Programiz-Pro"
Here, we have passed the sep
parameter inside the paste()
function to concatenate two strings: string1 and string2 with a hyphen in between them.