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 a Vector of Strings
Example 1: Concatenate a Vector of Strings Using cat() in R
# create a vector with string values
vector1 <- c("Data Science", "is", "fun")
# using cat() to concatenate a vector strings
cat(vector1)
Output
Data Science is fun
In the above example, we have used the cat()
function to concatenate the vector named vector1 which contains multiple strings.
So the vector elements "Data Science"
"is"
"fun"
are joined together and Data Science is fun is returned
.
Example 2: Concatenate a Vector of Strings Using paste() in R
# create a vector of strings
vector1 <- c("Science", "is","fun")
# using paste() and separate vector strings with whitespace
result1 <- paste(vector1, collapse = " ")
print(result1)
# using paste() and separate vector strings with hyphen
result2 <- paste(vector1, collapse = "-")
print(result2)
Output
[1] "Science is fun"
[1] "Science-is-fun"
Here,
paste(vector1, collapse = " "
- joins vector of strings with" "
as separator of strings. So the output will be"Science is fun"
paste(vector1, collapse = "-"
- joins vector of strings with"-"
as separator of strings. So the output will be"Science-is-fun"