首页 > 解决方案 > 在 R 中。如何将文件夹中的多个文件从“旧名称”重命名为“新名称”,这些文件位于 CSV 文件的 2 列中。谢谢

问题描述

我正在尝试使用重命名功能并选择了正确的工作目录并使用 list.files 来获取旧名称,但不知道现在该做什么。我在 CSV 文件中有一个列,它与旧名称和新名称完全匹配:
PS。文件扩展名“.0”是 OPUS 光谱文件附带的。

old-name    new-name
roth_666_1.0    N149_1.0
roth_666_2.0    N124_1.0
roth_666_3.0    N36_1.0
roth_666_4.0    N59_1.0
roth_666_5.0    N140_1.0
roth_666_6.0    N95_1.0
roth_666_7.0    N74_1.0
roth_666_8.0    N81_1.0
roth_666_9.0    N157_1.0
roth_666_10.0   N27_1.0
roth_666_11.0   N66_1.0
roth_666_12.0   N131_1.0
roth_666_13.0   N118_1.0
roth_666_14.0   N15_1.0
roth_666_15.0   N22_1.0
roth_666_16.0   N53_1.0

标签: r

解决方案


因此,假设您有一个数据框 df,其中包含上述列“旧名称”和“新名称”,并假设当前工作目录是文件所在的位置:

# will not work due to hyphens in column names 
# file.rename(from = df$old-name, to = df$new-name)

# better - specify the vector of values held in each column of the dataframe
file.rename(from = df[[1]], to = df[[2]] )

推荐阅读