【发布时间】:2023-02-22 04:32:37
【问题描述】:
我有一个数据框,其中包含一个名为 sample_id 的列以及其他元数据。
df <- data.frame(sample_id = c('x1', 'x2', 'x3', 'x4'))
我还有一个文件列表,其中包含各自的文件名,这些文件名可能并不总是具有相同的后缀。
files <- list('x1_001.txt', 'x2_003.txt', 'x4_abc.txt', 'x3_bg.txt')
我想在数据框中创建一个列,从文件列表中获取文件名并将其映射到相应的 sample_id。
但是,当我尝试使用 df <- df %>% mutate(filename = grep(sample_id, files, value = TRUE) 执行此操作时,它不起作用。
是否有具有此功能的功能,或者我需要创建一些自定义功能?
感谢您的任何帮助。
df <- df %>% mutate(filename = grep(sample_id, files, value = TRUE)
【问题讨论】:
标签: r dplyr data-wrangling