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 Trim Leading and Trailing Whitespaces
Example 1: Remove Whitespaces in R
text1 <- " Programiz Pro "
# remove whitespaces using trimws()
print(trimws(text1))
Output
[1] "Programiz Pro"
In the above example, we have used the trimws()
function to remove the leading and trailing whitespace. It doesn’t remove whitespace that appears in the middle.
Example 2: Remove Leading Whitespaces in R
text1 <- " Programiz Pro "
# remove leading whitespaces using trimws()
print(trimws(text1, "l"))
Output
[1] "Programiz Pro "
In the above example, we have passed "l"
inside the trimws()
function to remove the leading whitespaces in the text1 string.
Example 3: Remove Trailing Whitespaces in R
text1 <- " Programiz Pro "
# remove trailing whitespaces using trimws()
print(trimws(text1, "r"))
Output
[1] " Programiz Pro"
In the above example, we have passed "r"
inside the trimws()
function to remove the trailing whitespaces in the text1 string.