在 R 中打开 .nc 文件时出错

Error opening .nc files in R

我不熟悉 R 和 NetCDF 文件。我正在尝试打开来自 here 的 .nc 文件中的海面温度数据。我的代码如下:

rm(list=ls())
#install.packages(c("netcdf", "chron", "RColorBrewer", "lattice"))
library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)

# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"
ncname <- "20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0"  
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "tmp"  # note: tmp means temperature (not temporary)

# open a NetCDF file
ncin <- nc_open(ncfname)

但我收到以下错误:

Error in nc_open(ncfname) : 
Error in nc_open trying to open file C:/Users/Mihir Sharma/Dropbox/1 EPIC/MPA/data/20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0.nc

我遵循了 here and here 中的代码。我做错了什么?

非常感谢, 米希尔

使用别人代码时的路径问题:

# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"

当您关注博客或教程中的代码时,如果代码写得好,他们会使用独立于平台的方式来描述,但通常不会。

独立于平台的写路径方式是: file.path("Users", "Mihir Sharma", "Dropbox", "data")。这将始终为您的平台使用正确的文件分隔符,该值存储在 .Platform$file.sep