0

I'm creating an array from netcdf files and I keep getting this error at the start of the for loop: Error: $ operator is invalid for atomic vectors.

This is my code:

vec0 <- vector()
time <- c(vec0,1:1952)

vec1 <- vector()
temp <- c(vec1,1:1952)

vec2 <- vector()
sphum <- c(vec2,1:1952)

vec3 <- vector() 
rain <- c(vec3,1:28413)

vec4 <- vector()
surfp <- c(vec4,1:1952)

for (i in 1:length(filenames))
  {ncdata=nc_open(filenames[i]) 

 nctime=ncvar_get(ncdata,"time") 
  time[i] = nctime[1]

   nctemp=ncvar_get(ncdata,"Tair_f_inst") 
  temp[i] = nctemp[1]

 nchum=ncvar_get(ncdata,"Qair_f_inst") 
  sphum[i] = nchum[1]

  ncrain=ncvar_get(ncdata,"Rainf_tavg") 
  rain[i] = ncrain[1]

  ncsurfp=ncvar_get(ncdata,"Psurf_f_inst") 
 surfp[i] = ncsurfp[1]

 nc_close(filenames[i])}

I'm not using $ operator so I don't really understand the error.

Ktass
  • 47
  • 1
  • 1
  • 7
  • 1
    One of the functions you use uses $ internally. Usually this error indicates that you are passing a vector or matrix to a function that expects a data.frame. – Roland Mar 04 '20 at 09:00
  • Do you have any suggestions on how I should proceed to avoid the error? – Ktass Mar 05 '20 at 02:58

0 Answers0