我想向我的数据集添加标签。但是,问题在于我的数据集中有太多列,因此手动添加标签很费力。

我有33列,其中包括label列,其结尾如下。

features <- c("f1","f2","f3","f4","f5","f6","f7","f8","f9","f10",
              "f11","f12","f13","f14","f15","f16","f17","f18","f19","f20",
              "f21","f22","f23","f24","f25","f26","f27","f28","f29","f30",
              "f31","f32","label")
colnames(urc_training_norm) <- features

如您所见,手动键入每列很讨厌添加我想要的列名称。

有没有更好的方法来生成这些名称?

最佳答案

这是我通常的做法。 sprintf直接打印数字。通过添加%02d%03d,您可以添加前导零,这在处理大数时很有用:D

features <- c(sprintf("f%02d", seq(1,32)),"label")
colnames(urc_training_norm) <- features

关于r - 使连续的数字列名称以字母为前缀,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37154481/

10-12 19:53