为数据集中的 select 组列名添加后缀

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

我想为数据集 (CTDB) 中的一组列添加后缀。例如,我有以下列,我想在末尾添加“_Child”。

此子集是包含 100 多列的更大数据集的一部分,我不想重写每个列名称。

 [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"

我尝试了以下代码,但出现以下错误。

> 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

感谢您的帮助!

根据您的数据,这可能有效:

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

如果您不想手动设置索引,可以使用 "which()" 来查找它们。

更新:

dplyr 1.0.0 引入了重大改进和更改。不幸的是,为了使其他函数保持一致,他们将 rename_at 转换为 rename_with,这与 select 等其他函数不太一致,因为函数现在是第一个参数,而列是第二。您可以在此处查看开发人员所做的考虑:https://github.com/tidyverse/dplyr/pull/4923

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

参考:https://www.tidyverse.org/blog/2020/03/dplyr-1-0-0-select-rename-relocate/


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

参考: