using rep command in r studio to rename specific columns

407 Views Asked by At

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="")
paste(trts)

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

There are 3 best solutions below

0
etienne On BEST ANSWER

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"   
0
Amit Kohli On

From: Extracting the last n characters from a string in R

we have the function:

substrRight <- function(x, n){ substr(x, nchar(x)-n+1, nchar(x)) }

which we can use to extract the last number. Then we use that to figure out the order, and finally reorder the vector using this order:

trts[order(as.numeric(substrRight(trts,1)))]

Output:

[1] "CHPrefrontal1" "CHCaudate1"
[3] "CHCerebellum1" "CHBroca1"
[5] "HUPre frontal1" "HUCaudate1"
[7] "HUBroca1" "CHPrefrontal2"
[9] "CHCaudate2" "CHCerebellum2"
[11] "CHBroca2" "HUCaudate2"
[13] "HUCerebellum2" "HUBroca2"
[15] "CHPrefrontal3" "CHCaudate3"
[17] "CHCerebellum3" "CHBroca3"
[19] "HUPre frontal3" "HUPre frontal3" [21] "HUCaudate3" "HUCerebellum3"
[23] "HUCerebellum3" "HUBroca3"

0
Pierre L On

If you do not want the numbers attached, try:

rep(paste0(rep(c("CH", "HU"), each=3), c("Prefrontal", "Caudate", "Cerebellum", "Broca")), 3)
# [1] "CHPrefrontal" "CHCaudate"    "CHCerebellum" "HUBroca"      "HUPrefrontal"
# [6] "HUCaudate"    "CHPrefrontal" "CHCaudate"    "CHCerebellum" "HUBroca"     
# [11] "HUPrefrontal" "HUCaudate"    "CHPrefrontal" "CHCaudate"    "CHCerebellum"
# [16] "HUBroca"      "HUPrefrontal" "HUCaudate"