How put names in columns of a data frame according with other DF ?

Hi community, I have 2 DF but I want put the names of test_nirs columns according the test_data sample ID order.

For example in test_data G1320 have S_ID in 1,2,3,4,5, so Im want to put G1320 in the names of columns in test_nirsin the columns spectrum00001.asd.sco, spectrum00002.asd.sco, spectrum00003.asd.sco,
spectrum00004.asd.sco, spectrum00005.asd.sco.
The same logic in other test_data$Sample.

test_data <- structure(list(Sample = c("WHITE_April24", "G1320", "G1375", 
"G1386", "G3593"), S_ID1 = c(0, 1, 6, 11, 16), S_ID2 = c("NA", 
"2", "7", "12", "17"), S_ID3 = c("NA", "3", "8", "13", "18"), 
    S_ID4 = c("NA", "4", "9", "14", "19"), S_ID5 = c("NA", "5", 
    "10", "15", "20")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

test_nirs<- structure(list(Wavelength = c(350, 351, 352, 353), spectrum00001.asd.sco = c(0.0268770698457956, 
0.0242126975208521, 0.0244444087147713, 0.0284143257886171), 
    spectrum00002.asd.sco = c(0.0218958351761103, 0.0255079846829176, 
    0.0258213002234697, 0.0247935894876719), spectrum00003.asd.sco = c(0.0254370532929897, 
    0.0241091009229422, 0.0243665017187595, 0.0266730226576328
    ), spectrum00004.asd.sco = c(0.0246685761958361, 0.0280944984406233, 
    0.0305429585278034, 0.0299675837159157), spectrum00005.asd.sco = c(0.0240578576922417, 
    0.0237019527703524, 0.0277897827327251, 0.0332123748958111
    ), spectrum00006.asd.sco = c(0.028218038380146, 0.0256956685334444, 
    0.0227752681821585, 0.0246642492711544), spectrum00007.asd.sco = c(0.0256987903267145, 
    0.0242275986820459, 0.0255157072097063, 0.0282048434019089
    ), spectrum00008.asd.sco = c(0.0237200632691383, 0.021919684484601, 
    0.0227903444319963, 0.02577855437994), spectrum00009.asd.sco = c(0.027173949405551, 
    0.0270063206553459, 0.0291362628340721, 0.0306104738265276
    ), spectrum00010.asd.sco = c(0.0231008864939213, 0.0248048417270184, 
    0.0260183699429035, 0.0271991454064846), spectrum00011.asd.sco = c(0.173538714647293, 
    0.175838008522987, 0.176024124026299, 0.177909284830093), 
    spectrum00012.asd.sco = c(0.162097454071045, 0.163041412830353, 
    0.169347420334816, 0.176754340529442), spectrum00013.asd.sco = c(0.168238595128059, 
    0.166785791516304, 0.170181542634964, 0.178847163915634), 
    spectrum00014.asd.sco = c(0.167410090565681, 0.169330105185509, 
    0.171718969941139, 0.173835530877113), spectrum00015.asd.sco = c(0.163075134158134, 
    0.162994638085365, 0.170193448662758, 0.179959163069725), 
    spectrum00016.asd.sco = c(0.0204782746732235, 0.0198289547115564, 
    0.0205852333456278, 0.0218736007809639), spectrum00017.asd.sco = c(0.0232008136808872, 
    0.0268573611974716, 0.023913724347949, 0.0191555842757225
    ), spectrum00018.asd.sco = c(0.014141040854156, 0.0157095864415169, 
    0.0163433793932199, 0.015558622777462), spectrum00019.asd.sco = c(0.0226607900112867, 
    0.0162200666964054, 0.0165540780872107, 0.0239023752510548
    ), spectrum00020.asd.sco = c(0.020750368013978, 0.0222205556929111, 
    0.0202599465847015, 0.0168250408023596), spectrum00021.asd.sco = c(0.011662308126688, 
    0.0124919395893812, 0.0156590361148119, 0.0201825760304928
    ), spectrum00022.asd.sco = c(0.0144296949729323, 0.0174871534109116, 
    0.0200759060680866, 0.0203758589923382), spectrum00023.asd.sco = c(0.00923414342105389, 
    0.0135302674025297, 0.0167161747813225, 0.0171845573931932
    ), spectrum00024.asd.sco = c(0.0111471265554428, 0.0128100030124187, 
    0.0129068372771144, 0.0144085641950369), spectrum00025.asd.sco = c(0.0162104777991772, 
    0.0128545118495822, 0.0142696481198072, 0.0187012422829866
    ), spectrum00026.asd.sco = c(0.0205195695161819, 0.0196031518280506, 
    0.0169941633939743, 0.0162880849093199), spectrum00027.asd.sco = c(0.0196157209575176, 
    0.0180683247745037, 0.0149335954338312, 0.0129424016922712
    ), spectrum00028.asd.sco = c(0.0157131869345903, 0.0138804353773594, 
    0.0157931875437498, 0.0194765254855156), spectrum00029.asd.sco = c(0.0182622727006674, 
    0.0171185899525881, 0.0197099316865206, 0.023563189432025
    )), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))

The final DF look like this, change each 5 column.

Tnks!

I'm not quite sure what you're after, but perhaps this?

names(test_nirs) <- c("Wavelength", rep(test_data$Sample[1:5], each=5))
1 Like

Was a very close solution, tnks!

Im delete the first column in test_nirs and the first row in test_data because I dont need this. Next run your code and was excellent.

test_data <- test_data[ -1, ] 
test_nirs <- test_nirs[,-1]

names(test_nirs) <- c(rep(test_data$Sample, each=5)) 

   G1320  G1320  G1320  G1320  G1320  G1375  G1375  G1375  G1375  G1375
   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 0.0269 0.0219 0.0254 0.0247 0.0241 0.0282 0.0257 0.0237 0.0272 0.0231
2 0.0242 0.0255 0.0241 0.0281 0.0237 0.0257 0.0242 0.0219 0.0270 0.0248

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.