Alysha Alysha - 5 months ago 26
R Question

using rep command in r studio to rename specific columns

I'm trying to group together chimp and human brain samples by renaming them. There are 24 samples total, the first 12 are chimp, the last are human. It repeats by brain area 3 times (3 different chimp/human samples of 4 different brain regions). I have been able to produce all 24 sample names, but they were not in the correct order. It should be CHPrefrontal,CHCaudate,CHCerebellum, CHBroca then repeat those for 2 more times, then same setup for the human samples. Any suggestions? This is what I have so far:

trts=paste(rep(c("CHPrefrontal","CHCaudate","CHCerebellum","CHBroca","HUPre frontal","HUCaudate","HUCerebellum","HUBroca"),each=3),rep(c(1:3, 1:3, times=3)),sep="")

The output is:

[1] "CHPrefrontal1" "CHPrefrontal2" "CHPrefrontal3"
[4] "CHCaudate1" "CHCaudate2" "CHCaudate3"
[7] "CHCerebellum3" "CHCerebellum1" "CHCerebellum2"
[10] "CHBroca3" "CHBroca1" "CHBroca2"
[13] "HUPrefrontal3" "HUPrefrontal3" "HUPrefrontal1"
[16] "HUCaudate2" "HUCaudate3" "HUCaudate1"
[19] "HUCerebellum2" "HUCerebellum3" "HUCerebellum3"
[22] "HUBroca1" "HUBroca2" "HUBroca3"


You can do:

paste0(rep(c("CH", "HU"), each = 12), rep(c("Prefrontal", "Caudate", "Cerebellum", "Broca"), 4))

# [1] "CHPrefrontal" "CHCaudate"    "CHCerebellum" "CHBroca"      "CHPrefrontal" "CHCaudate"   
# [7] "CHCerebellum" "CHBroca"      "CHPrefrontal" "CHCaudate"    "CHCerebellum" "CHBroca"     
# [13] "HUPrefrontal" "HUCaudate"    "HUCerebellum" "HUBroca"      "HUPrefrontal" "HUCaudate"   
# [19] "HUCerebellum" "HUBroca"      "HUPrefrontal" "HUCaudate"    "HUCerebellum" "HUBroca"