Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR R

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"
Source by statisticsglobe.com #
 
PREVIOUS NEXT
Tagged: #R #specific #character #string
ADD COMMENT
Topic
Name
2+6 =