1

I have a large data set with set column names, I need to rename the columns. Here are the columns:

class(spec_sub_act_feat_all)
[1] "data.frame"
names(spec_sub_act_feat_all[,82:86])
[1] "angle(tBodyAccMean,gravity)"          "angle(tBodyAccJerkMean),gravityMean)"
[3] "angle(tBodyGyroMean,gravityMean)"     "angle(tBodyGyroJerkMean,gravityMean)"
[5] "angle(X,gravityMean)"
class(names(spec_sub_act_feat_all[,82:88]))
[1] "character"

However, when I try to rename the columns using

names(spec_sub_act_feat_all) <- gsub(`angle(tBodyAccMean,gravity)`,
'Vector of mean body accelerometer signal',names(spec_sub_act_feat_all))
Error in gsub(`angle(tBodyAccMean,gravity)`, "Vector of mean body accelerometer signal",  : 
  object 'angle(tBodyAccMean,gravity)' not found

or simply

names(spec_sub_act_feat_all[,82])<-"Vector of mean body accelerometer signal"

Neither one works. I believe my problem is that R is recognizing the column name as an actual function and won't let me select the character string to change it. The first renaming I tried with gsub(), I used `` to try to select the column name as not a function, which was recommended in another post, but did not work for me. I did notice that I could substitute out what was in the () but not the whole 'angle(...)' part.

M Doster
  • 37
  • 5

1 Answers1

1

As the ( and ) are metacharacters to capture the group in regex mode, if we use this in gsub, the default option is fixed = FALSE i.e. regex mode, either we need to escape (\\() or place it in square brackets ([(]) to literally evaluate it in regex mode or we can specify fixed = TRUE

gsub("angle(tBodyAccMean,gravity)",
    'Vector of mean body accelerometer signal',
                     names(spec_sub_act_feat_all), fixed = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662