Could you please show me how to split strings at the end of last letter? I have some data read from xlsx. Please take a look from the pic. The strings consists of a english word and some other character (could be number or ?,!, etc). There is probably space between word and other character or not. Thank you in advance!
Asked
Active
Viewed 249 times
0
-
"bananas1" "insurance?" "town!" "hostel 4" "qualifications 5" "labor-consuming 6" "problem solving 7" – shy zhan Aug 02 '18 at 23:59
-
Thank you Mark Keen. But there are lots of other characters following words. So it's better split strings at the end of last letter. – shy zhan Aug 03 '18 at 00:06
-
2Please put all the important information in the question. You can edit your question by clicking `edit` under the tags. Once you've done that, you can delete those comments – divibisan Aug 03 '18 at 00:11
-
Since there isn't a simple thing to split on, you're going to want to use `regular expressions` to grab the 2 parts of your strings. Search for a tutorial on `regular expressions` or `regex` for short and use https://regex101.com to test out your expressions. Good luck! – divibisan Aug 03 '18 at 00:15
1 Answers
0
If you want to extract the last character only, you could try the following strategy using tidyverse
library(tidyverse)
x <- c("bananas1", "insurance?", "town!", "hostel 4", "qualifications 5", "labor-consuming 6", "problem solving 7")
map2_chr(x, (str_length(x) ), ~str_sub(.x, .y, .y))

cephalopod
- 1,826
- 22
- 31