string - R: How to change the column names in a data frame based on a specification -


i have data frame, start of below:

                                sm_h1455          sm_v1456          sm_k1457      sm_x1461          sm_k1462 ensg00000000419.8                290               270               314               364               240 ensg00000000457.8                252               230               242               220               106 ensg00000000460.11               154               158               162               136                64 ensg00000000938.7              20106             18664             19764             15640             19024 ensg00000000971.11                30                10                 4                 2                10 

note there many more cols , rows.

here's want do: want change name of columns. important information in column's name, e.g. sm_h1455, 4th character of character string. in case it's h. want change "sm" part "control" if 4th character "h" or "k", , "case" if 4th column "x" or "v". i'd keep else in name. in end, i'd table this:

                        control_h1455          case_v1456        control_k1457      case_x1461        control_k1462 ensg00000000419.8                290               270               314               364               240 ensg00000000457.8                252               230               242               220               106 ensg00000000460.11               154               158               162               136                64 ensg00000000938.7              20106             18664             19764             15640             19024 ensg00000000971.11                30                10                 4                 2                10 

please keep in mind whether 4th character "v", "x", "k" or "h" random.

i'd appreciate help! thanks.

one way, x df:

controls <- which(substring(names(x),4,4) %in% c("h","k")) cases <- which(substring(names(x),4,4) %in% c("x","v")) names(x)[controls] <- gsub("sm","control",names(x)[controls]) names(x)[cases] <- gsub("sm","case",names(x)[cases]) 

alternatively:

names(x) <- sapply(names(x),function(z) {     if(substring(z,4,4) %in% c("h","k"))         sub("sm","control",z)     else if(substring(z,4,4) %in% c("x","v"))         sub("sm","case",z) }) 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -