1

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"
Alysha
  • 35
  • 1
  • 1
  • 11
  • does `paste0(rep(c("CH", "HU"), each = 12), rep(c("Prefrontal", "Caudate", "Cerebellum", "Broca"), 4), rep(rep(1:3, each = 4), 2))` work for you ? – etienne Oct 26 '16 at 14:39
  • In your description you do not have 1 2 or 3 attached on the end. But in your code you do. Which one are you looking for? – Pierre L Oct 26 '16 at 14:42
  • I don't want the 1, 2, or 3 attached. – Alysha Oct 26 '16 at 14:43
  • @Alysha : so something like `paste0(rep(c("CH", "HU"), each = 12), rep(c("Prefrontal", "Caudate", "Cerebellum", "Broca"), 4))` ? – etienne Oct 26 '16 at 14:44

3 Answers3

0

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"

Community
  • 1
  • 1
Amit Kohli
  • 2,860
  • 2
  • 24
  • 44
0

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"  
Pierre L
  • 28,203
  • 6
  • 47
  • 69
0

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"   
etienne
  • 3,648
  • 4
  • 23
  • 37