Oh I didn't understand the question correctly.
So, if I understand correctly, you have 1 folder for each accession number with two pictures in each, and you want to group them by FOLDER?
I'll use the library {fs}
which comes with the tidyverse and is practical for file manipulation.
library(fs)
Let's create example data, these are not actual jpeg files, just text files with a ".jpg" extension.
basic <- data.frame(ACCESION=c('G 1','G 7A', 'G35015', 'G40897','G27573'),
FOLDER= c( 'P_vulgaris', 'P_vulgaris','P_dumosus', 'P_albescens', 'P_lunatus'))
dir_pre <- "data"
dir_create(dir_pre)
purrr::walk(basic$ACCESION,
~ {
dir_create(path(dir_pre, .x))
writeLines(paste0("This file is for ",.x," sed"),
con = path(dir_pre, .x, .x) |> paste0(" sed.jpg"))
writeLines(paste0("This file is for ",.x," pod"),
con = path(dir_pre, .x, .x) |> paste0(" pod.jpg"))
})
fs::dir_tree(dir_pre)
#> data
#> ├── G 1
#> │ ├── G 1 pod.jpg
#> │ └── G 1 sed.jpg
#> ├── G 7A
#> │ ├── G 7A pod.jpg
#> │ └── G 7A sed.jpg
#> ├── G27573
#> │ ├── G27573 pod.jpg
#> │ └── G27573 sed.jpg
#> ├── G35015
#> │ ├── G35015 pod.jpg
#> │ └── G35015 sed.jpg
#> └── G40897
#> ├── G40897 pod.jpg
#> └── G40897 sed.jpg
Now that should look like the data you currently have. So we will create a new folder hierarchy and move the pictures in there:
dir_post <- "sorted_data"
dir_create(dir_post)
purrr::walk2(basic$ACCESION, basic$FOLDER,
\(acc, fold) {
# create the folders
dir_create(path(dir_post, fold, "sed"),
recurse = TRUE)
dir_create(path(dir_post, fold, "pod"))
# move the files
file_move(path(dir_pre, acc, acc) |> paste0(" sed.jpg"),
new_path = path(dir_post, fold, "sed", acc) |> paste0(" sed.jpg"))
file_move(path(dir_pre, acc, acc) |> paste0(" pod.jpg"),
new_path = path(dir_post, fold, "pod", acc) |> paste0(" pod.jpg"))
})
Now we can check the result, the old folder is empty:
fs::dir_tree(dir_pre)
#> data
#> ├── G 1
#> ├── G 7A
#> ├── G27573
#> ├── G35015
#> └── G40897
And the new folder contains all the images:
fs::dir_tree(dir_post)
#> sorted_data
#> ├── P_albescens
#> │ ├── pod
#> │ │ └── G40897 pod.jpg
#> │ └── sed
#> │ └── G40897 sed.jpg
#> ├── P_dumosus
#> │ ├── pod
#> │ │ └── G35015 pod.jpg
#> │ └── sed
#> │ └── G35015 sed.jpg
#> ├── P_lunatus
#> │ ├── pod
#> │ │ └── G27573 pod.jpg
#> │ └── sed
#> │ └── G27573 sed.jpg
#> └── P_vulgaris
#> ├── pod
#> │ ├── G 1 pod.jpg
#> │ └── G 7A pod.jpg
#> └── sed
#> ├── G 1 sed.jpg
#> └── G 7A sed.jpg
And since my fake images actually contain text, I can check they didn't get mixed up in the process:
readLines("sorted_data/P_dumosus/pod/G35015 pod.jpg")
#> [1] "This file is for G35015 pod"
Created on 2022-11-10 by the reprex package (v2.0.1)