如何在R中的read.csv的文件路径中包含字符串和变量? [英] How to include strings and variables in the file path for read.csv in R?

查看:598
本文介绍了如何在R中的read.csv的文件路径中包含字符串和变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在函数中输入文件路径,其中函数的一部分从函数中指定的文件夹中的.csv文件中读取数据.

I am trying to enter a file path into a function where part of the function reads data from .csv files from a folder specified in the function.

脚本的这一部分:

prep_data <- function("filepath", Year_First, Year_Last)  {
...
FileName <- paste0(filepath,"/details", Year_Index, "moredetails", Year_Index, ".csv")
Tbl_Year <- read.csv(FileName)

哪里

prep_data("/users/me/etc", 1980, 2014)

给我这个错误:

In file(file, "rt") :
cannot open file 'filepath/details1980moredetails1980.csv': No such file or directory

我想要的文件路径为:

/users/me/etc/details1980moredetails1980.csv

此行位于for循环中,该循环正在读取指定年份范围内的.csv文件.

This line is in a for loop that is reading .csv files for the range of specified years.

推荐答案

read.csv(paste0(getwd(),'/details/, Year_Index, 'moredetails', Year_Index, '.csv'),header=TRUE,sep=',',FactorAsString=TRUE)

如果文件保存在目录中,建议尝试使用getwd()作为文件路径.

Suggest try getwd() for filepath if your files saved in directory.

这篇关于如何在R中的read.csv的文件路径中包含字符串和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆