Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r预先指定部分文件路径_R - Fatal编程技术网

r预先指定部分文件路径

r预先指定部分文件路径,r,R,我目前在分析中对每种物种都有单独的脚本,我想尝试将其放入一个脚本中,在开始时我可以指定我正在分析的物种/年份等数据。 我想知道是否可以在脚本的开头设置某些对象,然后在文件中读取更多的通用代码。比如, 现行守则 ABR <- read.csv("Y:/Pelagic Work/FIN Data/2015/HOM/Abroad.csv",sep=",",skip = 2,header=T) SCO <- read.csv("Y:/Pelagic Work/FIN Data/2015/HO

我目前在分析中对每种物种都有单独的脚本,我想尝试将其放入一个脚本中,在开始时我可以指定我正在分析的物种/年份等数据。 我想知道是否可以在脚本的开头设置某些对象,然后在文件中读取更多的通用代码。比如,

现行守则

ABR <- read.csv("Y:/Pelagic Work/FIN Data/2015/HOM/Abroad.csv",sep=",",skip = 2,header=T)
SCO <- read.csv("Y:/Pelagic Work/FIN Data/2015/HOM/Scotland.csv",sep=",",skip = 2,header=T)

ABR您可以为此使用
sprintf

Year <- 2015
Species <- HOM

abroad.path <- sprintf("Y:/Pelagic Work/FIN Data/%s/%s/Abroad.csv", Year, Species)
scotland.path <- sprintf("Y:/Pelagic Work/FIN Data/%s/%s/Scotland.csv", Year, Species)

ABR <- read.csv(abroad.path, sep=",", skip=2, header=T)
SCO <- read.csv(scotland.path, sep=",", skip=2, header=T)

Year您可以为此使用
sprintf

Year <- 2015
Species <- HOM

abroad.path <- sprintf("Y:/Pelagic Work/FIN Data/%s/%s/Abroad.csv", Year, Species)
scotland.path <- sprintf("Y:/Pelagic Work/FIN Data/%s/%s/Scotland.csv", Year, Species)

ABR <- read.csv(abroad.path, sep=",", skip=2, header=T)
SCO <- read.csv(scotland.path, sep=",", skip=2, header=T)

Year我定义了一个helper函数
getFileName()
,它负责构建要读取的文件的路径

getFileName <- function(root, year, species, file) {
    return (paste(root, year, species, file, sep="/"))
}

root <- "Y:/Pelagic Work/FIN Data"
year <- "2015"
species <- "HOM"
file <- "Abroad.csv"

fileName <- getFileName(root, year, species, file, sep="/")

ABR <- read.csv(fileName,sep=",",skip = 2,header=T)

getFileName我定义了一个helper函数
getFileName()
,用于构建要读取的文件的路径

getFileName <- function(root, year, species, file) {
    return (paste(root, year, species, file, sep="/"))
}

root <- "Y:/Pelagic Work/FIN Data"
year <- "2015"
species <- "HOM"
file <- "Abroad.csv"

fileName <- getFileName(root, year, species, file, sep="/")

ABR <- read.csv(fileName,sep=",",skip = 2,header=T)

getFileName
file.path
是您要查找的函数

root <- "Y:/Pelagic Work/FIN Data"
year <- "2015"
species <- "HOM"

file <- "Abroad.csv"
ABR <- file.path(root, year, species, file)

root
file.path
是您要查找的函数

root <- "Y:/Pelagic Work/FIN Data"
year <- "2015"
species <- "HOM"

file <- "Abroad.csv"
ABR <- file.path(root, year, species, file)

root谢谢蒂姆。我在“}”中得到了error-error:unexpected'}',所以我在return()中添加了。我得到了error-error:unexpected'}'在“}”中,所以我在return附近添加了()