我有一个像这样的单词/表达列表:
df = data.frame(word = c('word','my word', 'another+word-'))
如何在单词/短语的开头和结尾后添加特定字符?
预期结果示例:
df = data.frame(word = c(' word ',' my word ', ' another+word- '))
答案 0 :(得分:8)
有很多方法可以做到这一点。收集所有评论:
int
使用#Sotos
paste0(' ', df$word, ' ')
#Andre Elrico 1
sub("(.*)"," \\1 ",df$word)
#Andre Elrico 2
suppressMessages(paste(message("very easy question"), df$word, message("very easy question")))
#Cath 1
paste(NULL, df$word, NULL)
#Cath 2
sprintf(" %s ", df$word)
:
stringr
<强>基准强>
stringr::str_pad(df$word, width = nchar(df$word) + 2, side = "both")