序列生成
我很不好意思问这个简单的问题,但我无法弄清楚。
我有一个变量,
names <- c("M1", "K2L", "C2L", "N", "R_1_2", "CLA", "T123") # the real dataset has > 6000 valriables
我想要双重命名并添加字母“a”和“b”,输出(也考虑顺序)将如下所示:
M1a, M1b, K2La, K2Lb, C2La, C2Lb, Na, Nb, R_1_2a, R_1_2b, CLAa, CLAb, T123a, T123b
感谢您的帮助:
I am embrased to ask this simple question, but I could not figure it out.
I have a variable
names <- c("M1", "K2L", "C2L", "N", "R_1_2", "CLA", "T123") # the real dataset has > 6000 valriables
I want to double names and add alphabets "a" and "b" and the output (consider order too) would look like the following:
M1a, M1b, K2La, K2Lb, C2La, C2Lb, Na, Nb, R_1_2a, R_1_2b, CLAa, CLAb, T123a, T123b
Thanks for the help:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
rep
与each
参数一起使用可重复每个名称。然后使用paste
将它们与后缀组合起来。让我们以慢动作再看一遍:
使用外部产品的花式替代版本:
虽然我认为这更多是为了炫耀,因为它的可读性较差。
Use
rep
with theeach
argument to repeat each name. Then usepaste
to combine them with the suffix.Let's look at that again in slow motion:
Fancy alternate version using outer products:
Though I think this is more for showing off, since it's less readable.
使用
stringr
包的另一种解决方案Another solution using
stringr
package