我想为数据集(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
感谢您的帮助!
答案 0 :(得分:1)
(?mx)
^
(?=0*10*10*$) # check if the word consists of only 0s and exactly two 1s
(?:..){1,}. # check if the word length is odd
$
参考:Adding prefix or suffix to most data.frame variable names in piped R workflow
答案 1 :(得分:0)
根据您的数据,这可能有效:
colnames(CTDB)[9:83] <- paste(colnames(CTDB)[9:83], "CHILD", sep = "_")
如果您不想手动设置索引,可以使用“which()”来查找索引。