Psst! Check what courses we have in our offer, and get them all here to save $727. Act fast–this deal is available only 3/14–3/21.
Text values
22. Function substr


Okay, it looks like you've got the hang of things! Unsurprisingly, functions can also return a text value, not just a numerical one. An example of such function is the substr function. The function substr is used to extract only one part of a particular string, called a substring. For example,

substr("John and Tanya", 1, 4)
will return "John". The first argument of this function is the string ( "John and Tanya") we'd like to work with.

The second argument is the starting position of the first character in the desired substring (in this case, that's the position of the first character in "John and Tanya", which is "J").

The third argument is the position of the last character that will be extracted and added to the substring (in this case, the position of the fourth character in "John and Tanya", which is "n"). All characters between these two positions are also included in the substring. If the second and third arguments are the same, then only that single character will be extracted.

Note that your specified positions must be valid for the given string.


Have R display only the first eight characters of the string stored in the john_shopping_list variable.