我试图在字符串tst中分别将“ S00”,“ S0”或“ S”添加为1或2或3倍长。 结果就是S004,S008,S009,S022,S051,...,S753。
尝试使用ifelse
和grepl
,但未成功:
tst <- as.character(c(4,8,9,22,51,56,57,12,15,150,147,852,654,753))
ifelse(grepl("[0-9]{1} ", tst), paste("S00", tst,sep = ""),
ifelse(grepl("[0-9]{2} ", tst), paste("S0", tst,sep = ""), "S"))