【问题标题】:How to match file names in directory on R with names in CSV column如何将 R 目录中的文件名与 CSV 列中的名称匹配
【发布时间】:2022-08-17 07:34:02
【问题描述】:

我正在尝试编写一个 r 脚本,该脚本将匹配目录中的文件名并将其与位于 csv 文件中的文件名进行比较。这样我就可以知道已经下载了哪些文件以及需要下载哪些数据。我编写的代码将从目录中读取文件并将它们列为 df 以及读取 csv 文件。但是,我无法更改文件名以提取我想要的字符串以及将文件名与 csv 文件中的名称列匹配。理想情况下,我还希望创建一个新的电子表格,它可以告诉我哪些文件匹配,这样我就知道下载了什么。这就是我到目前为止所拥有的。

# read files from directory and list as df
file_names <-list.files(path=\"peaches/\", 
                        pattern=\"jpg\",
                        all.files=TRUE,
                        full.names=TRUE,
                        recursive=TRUE) %>%
# turn into df
as.data.frame(x = file_names)

# read in xl file 
name_data <- read_excel(\"peaches/all_data.xlsx\")

# change the file_name from the string peaches//fruit/1234/12pink.jpg.txt  to -> 12pink
# match the file name with the name column in name_data
# create a new spread sheet that pulls the id and row if it has been downloaded [enter image description here][1]
  • 我提出了一个解决方案。将来,请发布您的文件结构的reproducible example。或者,更多描述会有所帮助。鉴于问题中缺乏细节,我制作了一组示例文件/结构。

标签: r dataframe file download


【解决方案1】:

示例文件/目录

让我们创建一个包含一些示例文件的示例目录。这将使我们证明该解决方案有效,并且是可重现解决方案的关键。

library(dplyr)
library(writexl)
library(readxl)

# Example directory with example files
dir.create(path = "KOMP")
write.csv(data.frame(x = 5), file = "KOMP/foo.csv")
write.csv(data.frame(x = 20), file = "KOMP/foo.nrrd.csv")
write.csv(data.frame(x = 1), file = "KOMP/foo2.nrrd.csv")
write.csv(data.frame(z = 2), file = "KOMP/bar.csv")
write.csv(data.frame(z = 5), file = "KOMP/bar.rrdr.csv")

# Example Excel file
write_xlsx(data.frame(name = c("foo", "hotdog")),
           path = "KOMP/all_data.xlsx")

解决方案

我们现在可以使用我们的示例文件和目录来展示问题的解决方案。

# Get file paths in a data.frame for those that contain ".nrrd"
# Use data.frame() to avoid row names instead of as.data.frame()
# Need to use \\ to escape the period in the regular expression
file_names <- list.files(
  path = "KOMP/",
  pattern = "\\.nrrd",
  all.files = TRUE,
  full.names = TRUE,
  recursive = TRUE
) %>%
  data.frame(paths = .)

# Extract part of file name (i.e. removing directory substrings) that
# comes before .nrrd and add a column. Can get file name with basename()
# and use regular expressions for the other part.
file_names$match_string <- file_names %>%
  pull(paths) %>%
  basename() %>%
  gsub(pattern = "\\.nrrd.*", replacement = "")

file_names$match_string
#> [1] "foo"  "foo2"

# Read in excel file with file names to match (if possible)
name_data <- read_excel("KOMP/all_data.xlsx")

name_data$name
#> [1] "foo"    "hotdog"

# Create match indicator and row number
name_data <- name_data %>%
  mutate(
    matched = case_when(name %in% file_names$match_string ~ 1,
                        TRUE ~ 0),
    rowID = row_number()
  )

# Create excel spreadsheet of files already downloaded
name_data %>%
  filter(matched == 1) %>%
  write_xlsx(path = "KOMP/already_downloaded.xlsx")

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2021-10-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多