Home > Net >  R: Append column names based on dataframe variables
R: Append column names based on dataframe variables

Time:03-12

I want to rename all the columns as the Profile variable in ann$Profile column and its sequential number. For example, the 4th BP will be labelled as BP_4. The 10th Unaffected control will be labelled as Unaffected control_10.

dat <- t(cbind(d$C1,d$C2))
for(i in 1:ncol(dat)) {names(dat)[i] <- paste(ann$Profile[i], i, sep="_")}

My code doesn't append any column names.

dat

> dput(d)
structure(list(FID = c("AC10", "AC11", "AC12", "AC13", "AC14", 
"AC15", "AC17", "AC18", "AC19", "AC1", "AC20", "AC21", "AC22", 
"AC23", "AC24", "AC25", "AC26", "AC27", "AC29", "AC2", "AC30", 
"AC31", "AC32", "AC33", "AC34", "AC35", "AC36", "AC37", "AC38", 
"AC39", "AC3", "AC40", "AC41", "AC42", "AC43", "AC45", "AC46", 
"AC47", "AC48", "AC49", "AC50", "AC51", "AC52", "AC53", "AC54", 
"AC55", "AC56", "AC57", "AC58", "AC5", "AC60", "AC61", "AC62", 
"AC63", "AC64", "AC65", "AC66", "AC67", "AC69", "AC6", "AC70", 
"AC71", "AC72", "AC73", "AC74", "AC75", "AC76", "AC77", "AC78", 
"AC79", "AC7", "AC80", "AC81", "AC82", "AC83", "AC84", "AC86", 
"AC87", "AC88", "AC89", "AC8", "AC90", "AC91", "AC92", "AC9", 
"AC100", "AC101", "AC102", "AC103", "AC104", "AC105", "AC16", 
"AC68", "AC93", "AC94", "AC95", "AC96", "AC97", "AC99", "DE10", 
"DE12", "DE13", "DE14", "DE15", "DE16", "DE17", "DE18", "DE19", 
"DE1", "DE20", "DE21", "DE22", "DE23", "DE25", "DE26", "DE27", 
"DE2", "DE33", "DE34", "DE35", "DE36", "DE37", "DE38", "DE39", 
"DE3", "DE40", "DE41", "DE42", "DE44", "DE45", "DE46", "DE47", 
"DE48", "DE49", "DE4", "DE50", "DE51", "DE52", "DE53", "DE54", 
"DE55", "DE56", "DE57", "DE58", "DE59", "DE60", "DE7", "DE9", 
"DE29", "DE30", "DE32", "DE43", "DE5"),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, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), C1 = c(-0.00385609, 0.0101138, -0.0146168, -0.0218236, -0.0134745, 
-0.017089, 0.0152448, 0.0134359, 0.00540102, -0.0125389, 0.00463956, 
-0.00416079, -0.000325898, 0.0132781, 0.0130666, 0.00718399, 
-0.0051912, -0.0227934, 0.0364974, -0.0180301, -0.0226556, -0.00585266, 
0.0258924, -0.00994298, -0.00380612, 0.0187883, 0.0103367, 0.00747272, 
0.0191431, -0.00501846, -0.00118336, 0.0361201, 0.00830498, 0.00380194, 
0.00667686, -0.000441697, -0.00170991, -0.0281008, -0.00424591, 
0.0213412, 0.00261405, 0.016154, 0.0098956, 0.0141544, 0.0367203, 
0.0144693, 0.0256731, -0.00218851, 0.0204603, -0.000603019, -0.00504176, 
-0.00917368, 0.00237875, 0.0175946, 0.0188388, 0.0368965, -0.00408476, 
0.00871812, -0.00851917, 0.0252035, -0.00915532, 0.0223745, 0.016866, 
0.026825, 0.0366276, 0.0540474, 0.0386237, 0.0029996, 0.0207176, 
0.0177353, -0.0066377, 0.0343811, 0.0282509, 0.00526683, 0.0459516, 
0.00976286, 0.0259005, -0.00104822, -0.012696, 0.0134071, 0.0231658, 
0.00359455, 0.0194968, -0.000936478, -0.0029218, -0.0058512, 
-0.000837274, -0.0129465, -0.0102079, -0.00559039, 0.0118966, 
0.00147658, 0.0120396, -0.0104779, -0.0315149, -0.0115454, -0.0122457, 
-6.72242e-05, 0.00370599, -0.0164126, -0.0107853, -0.0271741, 
-0.0212005, -0.0445118, -0.0387773, -0.025109, -0.0321735, -0.0398603, 
-0.0266408, -0.0260984, -0.0296337, -0.0185381, -0.0403944, 0.0197937, 
-0.0176322, -0.013238, -0.0071666, -7.27277e-05, 0.00397489, 
0.0335056, -0.00604706, -0.00926438, 0.00706601, -0.0156982, 
-0.0275085, -0.00864179, -0.0247967, -0.030564, -0.00767327, 
-0.0235161, 0.00649758, -0.0329062, -0.0016138, -0.00701695, 
0.00819454, 0.0100377, 0.0250199, -0.0493141, -0.0216641, -0.0244709, 
-0.00466616, 0.016751, -0.0191688, -0.00492488, -0.0162364, -0.0167085, 
-0.0113427, 0.000422333, 0.030274, 0.0317995, 0.00237194, -0.00693838, 
-0.0100835), C2 = c(0.000865365, -0.001752, 0.0189917, -0.023343, 
-0.0340531, -0.0258976, -0.00794043, 0.0173163, 0.00639341, -0.0343077, 
0.01083, -0.0402179, 0.0158751, -0.00262893, -0.0216757, -0.00261259, 
-0.00542089, -0.00515714, 0.0105216, -0.0193606, 0.00692795, 
-0.0117295, -0.0235627, -0.00850041, -0.0156109, -0.00871875, 
-0.0163218, 0.0227143, -0.0161961, -0.0176719, -0.0070994, 0.0262932, 
0.00164033, -0.00969917, -0.0197631, -0.0154387, -0.0194608, 
0.00442207, -0.0234804, 0.00822342, -0.00657274, -0.0092332, 
0.0130892, -0.0345162, -0.0114187, -0.0129497, -0.00306092, 0.0417858, 
0.0262002, -0.0188849, -0.0184154, -0.0109956, -0.0151195, -0.00414531, 
0.010064, 0.0308816, -0.0153337, 0.0157867, -0.0289866, -0.0106713, 
0.000112714, -0.00152177, 0.0184509, 0.0112357, 0.00097954, 0.032083, 
0.0190258, -0.0371498, -0.0307498, -0.00947645, -0.00198995, 
0.015845, -0.0240248, -0.0122369, -0.00107049, -0.0144661, 0.0207883, 
-0.0418619, -0.0123712, -0.0212721, -0.00667244, -0.028512, -0.00522357, 
-0.018842, -0.0123026, -0.00511655, 0.0188473, 0.00739189, 0.0321578, 
-0.015449, 0.0214631, -0.00995001, -0.00144645, 0.00934907, 0.0344757, 
-0.0220224, 0.0121403, -0.00615057, -0.0208969, 0.0313899, -0.0251011, 
0.011635, 0.00536455, 0.0233033, -0.0019204, 0.0273593, 0.00844028, 
0.00181444, 0.02824, 0.0255231, 0.00266055, -0.00850383, -0.0129938, 
0.0268634, 0.0195986, 0.0320615, -0.0026514, 0.0127147, 0.014279, 
0.0553434, -0.020963, 0.00629119, -0.0244099, -0.0080923, 0.0173508, 
0.0485753, -0.00666049, 0.0501603, 0.0029162, 0.0267363, 0.0066606, 
0.00857736, 0.0172693, -0.00827586, -0.0117478, -0.00336638, 
0.00954265, -0.00889617, 0.00290055, 0.0229832, 0.0504569, 0.025979, 
-0.00795356, -0.0135421, -0.00359528, 0.0150037, -0.0105817, 
0.0167827, 0.0110882, 0.00200862, -0.00597284, -0.0188371, -0.00827599
)), class = "data.frame", row.names = c(NA, -153L))

ann

> dput(ann$Profile)
c("Schiz.", "Schiz.", "Schiz.", "BP", "BP", "Unaffected control", 
"Schiz.", "BP", "Unaffected control", "BP", "BP", "BP", "Schiz.", 
"BP", "Unaffected control", "Schiz.", "Schiz.", "Unaffected control", 
"Unaffected control", "BP", "Unaffected control", "Schiz.", "BP", 
"Unaffected control", "BP", "Unaffected control", "BP", "Schiz.", 
"Unaffected control", "Schiz.", "Schiz.", "Schiz.", "Schiz.", 
"BP", "Unaffected control", "Schiz.", "BP", "Schiz.", "BP", "Unaffected control", 
"BP", "Unaffected control", "Unaffected control", "Unaffected control", 
"Unaffected control", "Schiz.", "Unaffected control", "BP", "BP", 
"BP", "Unaffected control", "BP", "BP", "BP", "BP", "Unaffected control", 
"Schiz.", "Unaffected control", "BP", "BP", "Unaffected control", 
"Unaffected control", "BP", "Schiz.", "BP", "Schiz.", "BP", "Unaffected control", 
"Schiz.", "Unaffected control", "Schiz.", "Unaffected control", 
"Schiz.", "Schiz.", "Unaffected control", "Unaffected control", 
"Unaffected control", "Schiz.", "Schiz.", "BP", "BP", "Unaffected control", 
"Unaffected control", "Schiz.", "Schiz.", "Schiz.", "Schiz.", 
"BP", "Unaffected control", "BP", "Unaffected control", "BP", 
"Schiz.", "Schiz.", "Schiz.", "Unaffected control", "Unaffected control", 
"Schiz.", "Unaffected control", "Unaffected control", "Unaffected control", 
"BP", "BP", "Dep.", "Unaffected control", "Unaffected control", 
"Dep.", "Dep.", "Dep.", "Dep.", "Dep.", "Unaffected control", 
"Unaffected control", "Schiz.", "Dep.", "BP", "Dep.", "Schiz.", 
"Schiz.", "Schiz.", "BP", "Unaffected control", "Unaffected control", 
"Unaffected control", "BP", "BP", "Dep.", "Schiz.", "Dep.", "BP", 
"Unaffected control", "Unaffected control", "Schiz.", "Schiz.", 
"Unaffected control", "Unaffected control", "BP", "BP", "Schiz.", 
"Dep.", "BP", "Dep.", "BP", "Schiz.", "Unaffected control", "Dep.", 
"BP", "Schiz.", "Dep.", "Dep.", "BP", "BP", "Schiz.")

CodePudding user response:

The questions refers to variables d, data but it is not clear to me how are they relevant here. Based on your explanation, only ann$Profile is needed here to answer the question. I saved ann$Profile in p variable.

Using rowid from data.table :

paste(p, data.table::rowid(p), sep = '_')

#[1] "Schiz._1"              "Schiz._2"              "Schiz._3"           "BP_1"
#[5] "BP_2"                  "Unaffected control_1"  "Schiz._4"           "BP_3"
#[9] "Unaffected control_2"  "BP_4"                  "BP_5"              "BP_6"
#.....
#.....
#[145] "Unaffected control_48" "Dep._13"       "BP_44"            "Schiz._43"    
#[149] "Dep._14"               "Dep._15"       "BP_45"                 "BP_46"
#[153] "Schiz._44"        

CodePudding user response:

A possible solution, but I am not sure whether it is what you want, as your question is not very clear:

ave(ann, ann, FUN = \(x) paste(x, 1:length(x), sep="_"))

#>   [1] "Schiz._1"              "Schiz._2"              "Schiz._3"             
#>   [4] "BP_1"                  "BP_2"                  "Unaffected control_1" 
#>   [7] "Schiz._4"              "BP_3"                  "Unaffected control_2" 
#>  [10] "BP_4"                  "BP_5"                  "BP_6"                 
#>  [13] "Schiz._5"              "BP_7"                  "Unaffected control_3" 
#>  [16] "Schiz._6"              "Schiz._7"              "Unaffected control_4" 
#>  [19] "Unaffected control_5"  "BP_8"                  "Unaffected control_6" 
#>  [22] "Schiz._8"              "BP_9"                  "Unaffected control_7" 
#>  [25] "BP_10"                 "Unaffected control_8"  "BP_11"                
#>  [28] "Schiz._9"              "Unaffected control_9"  "Schiz._10"            
#>  [31] "Schiz._11"             "Schiz._12"             "Schiz._13"            
#>  [34] "BP_12"                 "Unaffected control_10" "Schiz._14"            
#>  [37] "BP_13"                 "Schiz._15"             "BP_14"                
#>  [40] "Unaffected control_11" "BP_15"                 "Unaffected control_12"
#>  [43] "Unaffected control_13" "Unaffected control_14" "Unaffected control_15"
#>  [46] "Schiz._16"             "Unaffected control_16" "BP_16"                
#>  [49] "BP_17"                 "BP_18"                 "Unaffected control_17"
#>  [52] "BP_19"                 "BP_20"                 "BP_21"                
#>  [55] "BP_22"                 "Unaffected control_18" "Schiz._17"            
#>  [58] "Unaffected control_19" "BP_23"                 "BP_24"                
#>  [61] "Unaffected control_20" "Unaffected control_21" "BP_25"                
#>  [64] "Schiz._18"             "BP_26"                 "Schiz._19"            
#>  [67] "BP_27"                 "Unaffected control_22" "Schiz._20"            
#>  [70] "Unaffected control_23" "Schiz._21"             "Unaffected control_24"
#>  [73] "Schiz._22"             "Schiz._23"             "Unaffected control_25"
#>  [76] "Unaffected control_26" "Unaffected control_27" "Schiz._24"            
#>  [79] "Schiz._25"             "BP_28"                 "BP_29"                
#>  [82] "Unaffected control_28" "Unaffected control_29" "Schiz._26"            
#>  [85] "Schiz._27"             "Schiz._28"             "Schiz._29"            
#>  [88] "BP_30"                 "Unaffected control_30" "BP_31"                
#>  [91] "Unaffected control_31" "BP_32"                 "Schiz._30"            
#>  [94] "Schiz._31"             "Schiz._32"             "Unaffected control_32"
#>  [97] "Unaffected control_33" "Schiz._33"             "Unaffected control_34"
#> [100] "Unaffected control_35" "Unaffected control_36" "BP_33"                
#> [103] "BP_34"                 "Dep._1"                "Unaffected control_37"
#> [106] "Unaffected control_38" "Dep._2"                "Dep._3"               
#> [109] "Dep._4"                "Dep._5"                "Dep._6"               
#> [112] "Unaffected control_39" "Unaffected control_40" "Schiz._34"            
#> [115] "Dep._7"                "BP_35"                 "Dep._8"               
#> [118] "Schiz._35"             "Schiz._36"             "Schiz._37"            
#> [121] "BP_36"                 "Unaffected control_41" "Unaffected control_42"
#> [124] "Unaffected control_43" "BP_37"                 "BP_38"                
#> [127] "Dep._9"                "Schiz._38"             "Dep._10"              
#> [130] "BP_39"                 "Unaffected control_44" "Unaffected control_45"
#> [133] "Schiz._39"             "Schiz._40"             "Unaffected control_46"
#> [136] "Unaffected control_47" "BP_40"                 "BP_41"                
#> [139] "Schiz._41"             "Dep._11"               "BP_42"                
#> [142] "Dep._12"               "BP_43"                 "Schiz._42"            
#> [145] "Unaffected control_48" "Dep._13"               "BP_44"                
#> [148] "Schiz._43"             "Dep._14"               "Dep._15"              
#> [151] "BP_45"                 "BP_46"                 "Schiz._44"
  •  Tags:  
  • r
  • Related