【问题标题】:Match file names that are close, but not exact匹配接近但不准确的文件名
【发布时间】:2020-06-20 01:37:31
【问题描述】:

我有多个列表名称相似,但扩展名除外。我能够使用简单的括号方法对它们进行索引 - L1[1]L2[2] 将是正确的匹配。但是,我有很多文件要处理,其中一些文件与 index 编号与 index 编号不匹配。

在我的示例中,一种类型缺少一些文件。在我真实的第一个案例中,我有 122 个 .json 文件和 119 个 .description 文件。这抛弃了我正在使用的索引方法。在这种情况下,如何将正确的列表元素匹配在一起?我使用字符串匹配和字符串拆分尝试了一些不同的选项,但我没有运气。

为了以防万一,是的,这个元数据是使用 youtube-dl 提取的,但我是视频的作者。

最终目标是拥有两个变量VTTDESC,稍后我可以在我的R 脚本中使用它们。例如,VTT 将等于 L1[2],而 DESC 将等于 L2[标题紧密匹配的索引],例如不带扩展名或 L2[3] 的文件名。

这两个列表都来自使用list.files();但是,在程序的后面,我没有 full.names = TRUE 并且我只使用文件名本身。

L1 <- c("c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/01 - Statistical Programming with R - Estimating f (Notation)/Statistical Programming with R - Estimating f (Notation).mp4.en.txt", 
+         "c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/02 - Statistical Programming - Expected Value/Statistical Programming - Expected Value.mp4.en.txt", 
+         "c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/03 - Linear Regression with R 01/Linear Regression with R 01.mp4.en.txt"
+ )

L2 <- c("c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/01 - Statistical Programming with R - Estimating f (Notation)/Statistical Programming with R - Estimating f (Notation).mp4.info.json", 
, 
"c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/03 - Linear Regression with R 01/Linear Regression with R 01.mp4.info.json",
"c:/ytdl//CradleToGraveR/Absolute Beginners Guide to Statistical Programming/02 - Statistical Programming - Expected Value/Statistical Programming - Expected Value.mp4.info.json"
)

总的来说,也许我的方法是错误的。我认为我的下一个方法是将列表放入 data.frame 并剥离扩展名。然后只解析目录路径之后的结尾。最后,对两个 data.frame 进行连接或合并?我觉得我让这种方式变得比它应该的更复杂。

建议?

【问题讨论】:

    标签: r string filenames


    【解决方案1】:

    我认为最好只保留字符串中完全匹配的部分并进行比较。

    对于共享的示例,如果我们只保留文件名而不保留完整路径,则去掉"." 之后的所有内容并比较它是否有效。

    inds <- match(sub('\\..*', '', basename(L1)), sub('\\..*', '', basename(L2)))
    inds
    #[1] 1 3 2
    

    您可以按正确的顺序创建两个文件名的数据框

    data.frame(L1 = L1, L2 = L2[inds])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 2010-09-24
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多