Make sequential numeric column names prefixed with a letter

后端 未结 3 1885
忘掉有多难
忘掉有多难 2020-12-03 15:57

I want to add a label to my dataset. However, the problems is that there are so many columns in my data sets so adding the labels manually is laborious.

I have 33 co

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 16:52

    If you don't mind prefixing with X instead of f, then we can use make.names() function which is designed for making syntactically valid names:

    make.names(c(1:4, "label"))
    # [1] "X1"    "X2"    "X3"    "X4"    "label"
    

    Or we can use make.unique():

    make.unique(c(rep("f", 4), "label"), sep = "")
    # [1] "f"     "f1"    "f2"    "f3"    "label"
    

提交回复
热议问题