如何为R语言dataframe的text列每行添加三重引号?
给R语言DataFrame的text列添加首尾三重引号的解决方案
没问题,这就帮你搞定需求!要给DataFrame里的text列每个值首尾加上三重引号,用R的字符串拼接函数就能轻松实现,具体步骤如下:
- 先创建你给出的初始DataFrame:
df <- data.frame(id = c(4,2), text = c("my text here", "another text here"))
- 接着用
paste0()函数给text列拼接首尾的三重引号:
# 为text列每个值添加首尾三重引号 df$text <- paste0('"""', df$text, '"""')
- 最后查看处理后的结果:
print(df)
运行完以上代码,你就能得到期望的输出:
id text 1 4 """my text here""" 2 2 """another text here"""
原理很简单,paste0()会把三个字符串片段无缝拼接在一起:开头的"""、原text列的内容、结尾的""",刚好实现你要的格式效果~
内容的提问来源于stack exchange,提问作者Nathalie
相关产品推荐
相关产品推荐

