Get First Element from Character String Split in R (Example Code)

This article shows how to extract the first part after splitting a character string in the R programming language.

Creation of Example Data

x <- "What an amazing character string in R"    # Example string
x                                               # Display example string
# [1] "What an amazing character string in R"

Example: Extracting First Element of Character String Split

strsplit(x, " ")[[1]][1]                        # Return first entry of string
# [1] "What"

Related Articles & Further Resources

You may find some related R tutorials on topics such as data conversion, data objects, naming data, and character strings below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top