【发布时间】:2021-10-10 05:50:19
【问题描述】:
在 R 中的 read.csv 命令中传递要用作文件名的值时出错
我有一堆 csv 文件,每个文件都分配有一个值 Mx_csv,其中 x 是一个从 1 到 8 的数字。
M3_csv <- "123123_out.csv"
M4_csv <- "178787_out.csv"
我通过更改 1 行代码来选择要处理的 M:
Ma = "M4" // that then tells all following code which M to use
setwd(paste0("D:/RA_",Ma)) // sets the working directory folder RA_M4
filenm <- paste0(Ma,"_csv")
Mn <- read.csv(file = filenm,header = TRUE)
在这种情况下,x 变为“M4_csv”,但不是加载 178787_out.csv 的 read.csv 命令,而是尝试加载名为 M4_csv 和错误的文件。
我确定这是一个简单的解决方法,但谷歌并没有帮助我找到答案
非常感谢您的帮助
【问题讨论】:
-
你可以试试
get(paste0(Ma,"_csv"))。我对 get 函数的理解是它在环境中搜索具有该名称的对象。