本文介绍了Sys.glob扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sys.glob打开一个名为"apcp_sfc_latlon_subset_19940101_20071231.nc"的文件.以下命令有效:

I am trying to use Sys.glob to open a file called "apcp_sfc_latlon_subset_19940101_20071231.nc". The following command works:

> Sys.glob(file.path("data/train", "apcp*"))
[1] "data/train/apcp_sfc_latlon_subset_19940101_20071231.nc"

但是此命令不返回任何内容.我不知道为什么它不起作用.

But this command doesn't return anything. I'm don't know why it doesn't work.

> Sys.glob(file.path("data/train", "apcp", "*"))
character(0)

我希望将"apcp"位作为其自己的参数,因为我将传递变量而不是硬编码字符串.

I want the "apcp" bit as it's own argument because I will be passing a variable instead of a hard coded string.

谢谢.

推荐答案

file.path("data/train","apcp","*")返回"data/train/apcp/*",而 file.path(" data/train," apcp *)返回" data/train/apcp *".

因此,在第一种情况下,您正在寻找子目录 apcp 中的文件,在(有效)情况下,您正在寻找以 apcp 开头的文件code> data \ train 目录.

Thus in the first case you are looking for files in the subdirectoy apcp, and in the (working) case you are looking for files which begin apcp within the data\train directory.

如果您希望能够将 apcp 组件作为参数传递,则可以使用 paste0

If you want to be able to pass the apcp component as a argument, using paste0 will work

starting <- "apcp"

file.path("data/train", paste0(starting, '*', collapse =''))

# "data/train/apcp*"

这篇关于Sys.glob扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:13