列出文件列表中的第一个文件

问题描述 投票:0回答:1

我有这个代码来读取多个csv文件。

path <- "C:/Users/cfees/My Box Files/Fitness/"
files <- list.files(path=path, pattern="myfile_.+\\.csv")

我想在每次迭代中加载一个for c look每个csv。

我尝试一种简单的方法从列表中读取第一个csv文件但我收到错误。

df <- read.csv(path+files[1])
Error in path + files[1] : non-numeric argument to binary operator

我能做什么?

r
1个回答
0
投票

好的做法是使用file.path来连接路径,它将处理以/结尾的路径,更强大:

df <- read.csv(file.path(path,files[1]))
© www.soinside.com 2019 - 2024. All rights reserved.