R从文件名获取文件路径

von4xj4u  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(172)

我正在尝试将File_names与File_paths匹配。跳过NA值但不删除它们。如果一行有两个匹配项,则返回两个匹配项,并以分号分隔。将结果保存为File_paths。
文件路径〈- c(“C:/temp/1245~b~cat~v4.jpeg”,“C:/temp/1245~b~dog~v4.jpeg”,“C:/temp/1245~b~cow~v4.jpeg”,“C:/temp/1245~b~duck~v4.jpeg”,“C:/temp/1245~b~horsev4.jpeg”,“C:/temp/1245~b~goat~v4.jpeg”)
文件名〈- c(“B~cat”,“b~dog”,“NA”,“b~duck”,“b~goat;B~马”,“NA”,“b~牛”
所需输出dd〈-

File_name       File_path       
b~cat           c:/temp/1245~b~cat~v4.jpeg
b~dog           c:/temp/1245~b~dog~v4.jpeg  
NA              NA                                        
b~duck          c:/temp/1245~b~duck~v4.jpeg 
b~goat; b~horse c:/temp/1245~b~goat~v4.jpeg; c:/temp/1245~b~horse~v4.jpeg
NA              NA                         
b~cow           c:/temp/1245~b~cow~v4.jpeg
nr7wwzry

nr7wwzry1#

stack(sapply(sub("; *", '|', File_names), \(x)paste0(grep(x,File_paths, value = TRUE), collapse = '; ')))[2:1]
             ind                                                    values
1          b~cat                                C:/temp/1245~b~cat~v4.jpeg
2          b~dog                                C:/temp/1245~b~dog~v4.jpeg
3             NA                                                          
4         b~duck                               C:/temp/1245~b~duck~v4.jpeg
5 b~goat|b~horse C:/temp/1245~b~horse~v4.jpeg; C:/temp/1245~b~goat~v4.jpeg
6             NA                                                          
7          b~cow                                C:/temp/1245~b~cow~v4.jpeg
bpsygsoo

bpsygsoo2#

File_paths <- c("C:/temp/1245~b~cat~v4.jpeg", "C:/temp/1245~b~dog~v4.jpeg", "C:/temp/1245~b~cow~v4.jpeg", "C:/temp/1245~b~duck~v4.jpeg", "C:/temp/1245~b~horse~v4.jpeg", "C:/temp/1245~b~goat~v4.jpeg")
File_names <- c("b~cat", "b~dog", "NA", "b~duck", "b~goat; b~horse", "NA", "b~cow")

library(stringr)
library(tidyr)
library(dplyr)

data.frame(files = File_names) |>
  mutate(
    id = row_number(),
    # fix NAs
    files = ifelse(files == "NA", NA, files)
  ) |>
  separate_rows(files, sep = "; ") |>
  mutate(
    path = ifelse(!is.na(files), lapply(files, grep, x = File_paths, value = TRUE), NA_character_),
    path = unlist(path)
  ) |>
  group_by(id) |>
  summarize(across(everything(), paste, collapse = "; "))
# # A tibble: 7 × 3
#      id files           path                                                     
#   <int> <chr>           <chr>                                                    
# 1     1 b~cat           C:/temp/1245~b~cat~v4.jpeg                               
# 2     2 b~dog           C:/temp/1245~b~dog~v4.jpeg                               
# 3     3 NA              NA                                                       
# 4     4 b~duck          C:/temp/1245~b~duck~v4.jpeg                              
# 5     5 b~goat; b~horse C:/temp/1245~b~goat~v4.jpeg; C:/temp/1245~b~horse~v4.jpeg
# 6     6 NA              NA                                                       
# 7     7 b~cow           C:/temp/1245~b~cow~v4.jpeg

相关问题