Add a suffix to a select group of column names in a dataset

最后都变了- 提交于 2019-12-23 04:19:23

问题


I want to add a suffix to a group of columns in a data set (CTDB). For example I have the following columns and I would like to add "_Child" to the end.

This subset is part of a larger dataset with 100+ columns, and I do not want to re-write every single column name.

 [9] "SCARED_BREATHE"                          
 [10] "SCARED_HEADACHE_SCHOOL"                  
 [11] "SCARED_DISLIKE_STRANGERS"                
 [12] "SCARED_SLEEP_AWAY_HOME"                  
 [13] "SCARED_LIKE_ME"                          
 [14] "SCARED_PASS_OUT"                         
 [15] "SCARED_NERVOUS"                          
 [16] "SCARED_FOLLOW_PARENT"                    
 [17] "SCARED_LOOK_NERVOUS"                     
 [18] "SCARED_NERVOUS_AROUND_STRANGER"          
 [19] "SCARED_STOMACHACHE_SCHOOL"               
 [20] "SCARED_FEEL_CRAZY"                       
 [21] "SCARED_SLEEP_ALONE"                      
 [22] "SCARED_NOT_AS_GOOD"                      
 [23] "SCARED_NOT_REAL"                         
 [24] "SCARED_NIGHTMARE_PARENTS"                
 [25] "SCARED_SCHOOL"                           
 [26] "SCARED_HR_FAST"                          
 [27] "SCARED_SHAKY"                            
 [28] "SCARED_NIGHTMARE"                        
 [29] "SCARED_THINGS_WORK_OUT"                  
 [30] "SCARED_SWEAT"                            
 [31] "SCARED_WORRY"                            
 [32] "SCARED_NO_REASON"                        
 [33] "SCARED_ALONE_AT_HOME"                    
 [34] "SCARED_HARD_TO_TALK"                     
 [35] "SCARED_CHOKE"                            
 [36] "SCARED_WORRY_TOO_MUCH"                   
 [37] "SCARED_AWAY_FROM_FAMILY"                 
 [38] "SCARED_PANIC_ATTACK"                     
 [39] "SCARED_WORRY_PARENTS"                    
 [40] "SCARED_SHY_STRANGERS"                    
 [41] "SCARED_FUTURE"                           
 [42] "SCARED_THROW_UP"                         
 [43] "SCARED_HOW_WELL_I_DO"                    
 [44] "SCARED_GO_TO_SCHOOL"                     
 [45] "SCARED_PAST"                             
 [46] "SCARED_DIZZY"                            
 [47] "SCARED_OTHERS_WATCH_ME"                  
 [48] "SCARED_PARTY"                            
 [49] "SCARED_SHY"                              
 [50] "ARI_ANNOYED"                             
 [51] "ARI_LOSE_TEMPER_OFTEN"                   
 [52] "ARI_STAY_ANGRY"                          
 [53] "ARI_ANGRY_MOST"                          
 [54] "ARI_FREQ_ANGRY"                          
 [55] "ARI_LOSE_TEMPER_EASY"                    
 [56] "ARI_IRRITABLE"                           
 [57] "MFQ_S_UNHAPPY"                           
 [58] "MFQ_S_DIDNT_ENJOY"                       
 [59] "MFQ_S_TIRED"                             
 [60] "MFQ_S_RESTLESS"                          
 [61] "MFQ_S_NO_GOOD"                           
 [62] "MFQ_S_CRIED"                             
 [63] "MFQ_S_HARD_THINK"                        
 [64] "MFQ_S_HATE_MYSELF"                       
 [65] "MFQ_S_BAD_PERSON"                        
 [66] "MFQ_S_LONELY"                            
 [67] "MFQ_S_NOBODY_LOVE"                       
 [68] "MFQ_S_GOOD_OTHR_KID"                     
 [69] "MFQ_S_EVERTHING_WRONG"                   
 [70] "ENJOY_TV_RADIO"                          
 [71] "ENJOY_FMLY_CLOSE_FRND"                   
 [72] "ENJOY_HOBBIES"                           
 [73] "ENJOY_FAV_MEAL"                          
 [74] "ENJOY_SHOWER"                            
 [75] "ENJOY_SCENT"                             
 [76] "ENJOY_PPL_SMILE"                         
 [77] "ENJOY_LOOK_SMART"                        
 [78] "ENJOY_READ"                              
 [79] "ENJOY_FAV_DRINK"                         
 [80] "ENJOY_SMALL_THINGS"                      
 [81] "ENJOY_LANDSCAPE"                         
 [82] "ENJOY_HELP_OTHR"                         
 [83] "ENJOY_PRAISE"

I have tried the following code but I get the following error.

> colnames(CTDB[,c(BREATHE_SCARED:ENJOY_PRAISE)]) <- paste(colnames(CTDB[,c(BREATHE_SCARED:ENJOY_PRAISE)]), "CHILD", sep = "_")
Error in check_names_df(j, x) : object 'BREATHE_SCARED' not found

Thank you for your help!


回答1:


Depending on your data, this could work:

 colnames(CTDB)[9:83] <- paste(colnames(CTDB)[9:83], "CHILD", sep = "_")

If you don't want to set the indices manually, you can use "which()" to find them.




回答2:


CTDB %>% rename_at(BREATHE_SCARED:ENJOY_PRAISE, ~paste0(., "_Child"))

Reference: Adding prefix or suffix to most data.frame variable names in piped R workflow



来源:https://stackoverflow.com/questions/48566857/add-a-suffix-to-a-select-group-of-column-names-in-a-dataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!