【问题标题】:R-project error message - cannot read tableR-project 错误消息 - 无法读取表
【发布时间】:2013-01-15 22:26:06
【问题描述】:

我正在努力学习 R 教程。

目标是创建一个必须: 1) 将工作目录设置为我桌面上包含大量 .csv 文件的文件夹。 2)取一个参数来标识要使用/加载哪个文件。 2b) 参数必须制成特定格式。调用该函数的人可以输入一个长度在 1 到 3 位之间的数字)。有几百个潜在文件要加载,它们必须以“001.csv”、“050.csv”和“200.csv”格式输出。 3) 加载输入文件的数据。

这是我的代码:

getmonitor <- function(id){

setwd("C:/Users/myname/Desktop/specdata") #set the directory

csvfile <- function(id) # set the file number format
    if (id < 10) { 
        paste0(0,0,id,".csv")
    } else if (id < 100) {
        paste0(0,id,".csv")
    } else paste0(id,".csv")
}

foo <-read.csv(csvfile) #load the appropriate csv file

}

那没用。我收到的错误消息是:“read.table 中的错误(file = file,header = header,sep = sep,quote = quote,: 'file' 必须是字符串或连接"

我已经在 Google 上进行了大量搜索,但无法理解如何解决这个问题。我的方法可以吗?我是 R 和一般编程的新手。

【问题讨论】:

    标签: r function


    【解决方案1】:

    您将函数csvfile 作为文件参数传递,而不是该函数在调用时返回的字符串。你想要的

    foo <- read.csv(csvfile(id))
    

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2020-08-25
      • 2017-10-01
      • 2014-05-28
      相关资源
      最近更新 更多