Answers for "R get specific character from string"

R
0

R get specific character from string

string_var = "this is an example"

# substr(string, first_char_pos, last_char_pos) # extracts characters from pos1 to pos2
substr(x, 1, 3)		# Extract first three characters
# "thi"
substr(x, 4, 4)		# Extract only the fourth character
# "s"

n_last <- 3			# Specify number of characters to extract
# nchar(x) counts total no. of characters in the string
substr(x, nchar(x) - n_last + 1, nchar(x)) 	# Extract last three characters
# "ple"
Posted by: Guest on September-22-2021

Code answers related to "R get specific character from string"

Browse Popular Code Answers by Language