0

I need to manipulate the string so that my files can ascend in the following order: [1,1,1], [1,1,2] [1,1,3]....[1,2,1] and so on. Any insights as to how I can do this?

In Sample.Name I need to organize what's in the square brackets to ascend like so: [1,1,1], [1,1,2] [1,1,3]...[1,2,1] etc.

Table See image for table. I tried using the split function and introducing leading zeros and couldn't get it to work. I am new here so ago easy on me please.

Taufi
  • 1,557
  • 8
  • 14
anon
  • 13
  • 5
  • Can you make your example reproducible? No one wants to click on an image of a table, especially as you can't run it. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Feb 04 '21 at 22:42
  • If the string is always the same except for the contents of the square brackets, you can just `sort()` it and it will return in the desired order, won't it? – AdagioMolto Feb 05 '21 at 12:27

1 Answers1

0

One possible solution is as follows.

Data

df <- data.frame(Sample.Name = c("TMA_1314_Core[1,1,2]_[66421,20799].im3",
                                 "TMA_1314_Core[1,1,1]_[66421,20799].im3", 
                                 "TMA_1314_Core[1,1,4]_[66421,20799].im3",
                                 "TMA_1314_Core[1,1,3]_[66421,20799].im3"))

Code

Index <- stringr::str_extract(df$Sample.Name, "\\[(\\s*(.*?)\\])") %>% 
                     gsub(pattern = "\\]|\\[", replacement = "") %>% 
                                strsplit(",") %>% 
                                           sapply(.,function(x) as.numeric(paste(x, collapse = ""))) %>% 
                                                       order
df[Index,]

Output

> df[Index,]
[1] "TMA_1314_Core[1,1,1]_[66421,20799].im3"
[2] "TMA_1314_Core[1,1,2]_[66421,20799].im3"
[3] "TMA_1314_Core[1,1,3]_[66421,20799].im3"
[4] "TMA_1314_Core[1,1,4]_[66421,20799].im3"
Taufi
  • 1,557
  • 8
  • 14
  • Thank you thats really helpful, but "TMA_1314..." is 188 rows, so is there a simpler way so I don't have to make a vector with 188 names in it? – anon Feb 05 '21 at 18:43
  • also now it is calling TMA_1314...[1,1,10] TMA_1314..[1,1,1] instead...which is not what I want because each sample has an value attached to it which is area in square microns – anon Feb 05 '21 at 18:47
  • I don't understand what you mean. What is the output if you do `df <- mydata2_wide` and then execute the above code? – Taufi Feb 05 '21 at 19:46