Using gsub in R for multiple changes -


i have data.frame want 'clean' names of columns:

>names(data) [1] "tbodyacc.mean...x"  [2] "angle.x.gravitymean." [3] "fbodybodygyrojerkmag.mean.." [4] "fbodyaccmag.meanfreq.."              .              . 

i using following code:

names(data)<-gsub('[mm]ean',' mean ',names(data)) names(data)<-gsub('[ff]req',' frequency ',names(data)) names(data)<-gsub('^t','time  ',names(data)) names(data)<-gsub('\\.',' ',names(data)) 

to following:

[1] "time  bodyacc  mean    x"        [2] "angle x gravity mean  "          [3] "fbodybodygyrojerkmag  mean   "   [4] "fbodyaccmag  mean  frequency   " 

is there way impliment in 1 line or more elegant way one?

you try stri_replace_all_regex stringi package:

library(stringi) stri_replace_all_regex(names(data), c("mean", "freq", "^t", "\\."), c(' mean ', ' frequency ', 'time  ', ' '), f, list(case_insensitive = true)) # [1] "time  bodyacc  mean    x"        "angle x gravity mean  "          # [3] "fbodybodygyrojerkmag  mean   "   "fbodyaccmag  mean  frequency   " 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -