假设我们有一个包含数值的数据框,如下所示:
Temperature Height
32 157
31 159
33 139
我想更换Height
价值观与pic_00001
, pic_00002
等等,最终结果是:
Temperature Height
32 pic_00001
31 pic_00002
33 pic_00003
完整数据框中有 10,000 多行,因此我需要一种比手动执行此操作更快的方法。
您可以使用sprintf
:
# create the example used by the OP
dat <- data.frame(Temperature = 31:33,
Height = c(157, 159, 139))
# use sprintf along with seq_len
dat$Height <- sprintf("pic_%05d", seq_len(NROW(dat)))
# show the result
dat
#R> Temperature Height
#R> 1 31 pic_00001
#R> 2 32 pic_00002
#R> 3 33 pic_00003
您可以更改05d
如果你想要更多前导零。例如。07d
将给出一个七位数字序列。的手册页sprintf
有进一步的细节。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)