为 R 中的所有行添加前缀 [英] Add a prefix to all rows in R

查看:67
本文介绍了为 R 中的所有行添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向数据帧 chrs 中的 col ensnp 中的所有行添加前缀 end:

I am trying to add a prefix end to all rows in a col ensnp in a dataframe chrs:

 Name    endsnp
Bov001   Bov001
Bov002   Bov001

我的预期输出必须是这样的:

My expected output must be like that:

 Name     endsnp
Bov001   endBov001
Bov002   endBov001

我已经尝试过 chrs <- transform(chrs,endsnp = sprintf("end",endsnp)),但我得到了这个输出:

I have tried chrs <- transform(chrs, endsnp = sprintf("end", endsnp)), but I get this output:

 Name     endsnp
Bov001     end
Bov002     end

对我的错误有什么想法吗?谢谢!

Any ideas about my error? Thank you!

推荐答案

只需使用 paste0 来组合字符串.

Just use paste0 to combine strings.

例如,

chrs$endsnp = paste0('end', chrs$endsnp)

或使用 paste 并指定字符串之间的分隔符

or using paste and specifing the separator between the strings

chrs$endsnp = paste('end', chrs$endsnp, sep='')

这篇关于为 R 中的所有行添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆