2

Here is my code:

colnames(sheet1)[4:723] <- colnames(sheet1[,4:723],do.NULL = FALSE, prefix = "distance")

It did not show any error message.

What I want are column names in the pattern of "distance 1" "distance 2" ... "distance 719" "distance 720"

However, what I got were still the default names "Col4" "Col5" "Col6" "Col7" "Col8" "Col9" "Col10" "Col11" "Col12" ...

Can someone help me with it? Thank you in advance!

pogibas
  • 27,303
  • 19
  • 84
  • 117
Joyce Z
  • 31
  • 5
  • 3
    `colnames(sheet1)[4:723] <- paste0('distance_', colnames(sheet1)[4:723])` – utubun Nov 19 '19 at 14:42
  • In addition to ^^ on why setting prefix isn't working, several SO posts deal with pasting column names on a subset of columns, such as https://stackoverflow.com/q/48566857/5325862 – camille Nov 19 '19 at 15:25
  • @utubun thank you for your answer but it seems that the output is "distance_Col4" "distance_Col5" "distance_Col6" "distance_Col7"..... – Joyce Z Nov 21 '19 at 09:16
  • @camille thank you for your help! It solved my problem. – Joyce Z Nov 22 '19 at 07:37

1 Answers1

0

Since your sheet1 already has column names, we can replace the wanted slice of them with:

colnames(sheet1)[4:723] = colnames(matrix(ncol=720), F, "distance ")
Armali
  • 18,255
  • 14
  • 57
  • 171