Skip to content

replacement has length zero. In addition: Warning message: In max(i) : no non-missing arguments to max; returning -Inf

7 messages · Sarah Goslee, Yolande Tra

#
Yolande,

Please send your message to the R-help email list too, not just to me.
On Fri, Aug 3, 2012 at 4:28 PM, Yolande Tra <yolande.tra at gmail.com> wrote:
Which has nothing whatsoever in common with the "reproducible" code
you provided originally, since that only creates diveCond full of
zeros and tries to use it, without success.

You need to give us actual data for all data frames involved, as you
did for just the one, eg.
dput(head(diveCond, 10))
dput(head(regCond_all))

otherwise we can't run your code and see what happens.

But failing that, here's how to diagnose it yourself. Set i <- 1 and
then run the first few lines of your loop. Look at ind and see if it's
what you think it should be. Look at diveCond$r_wvht[i] and
regCond_all$WVHT[ind] to see if they exist and are the same size.

I'm guessing that ind isn't what you expect, but there's no way I can
be certain from what you've given us.

Sarah

  
    
#
When i == 10, what is ind?
diveCond$r_apt[10]
regCond_all$APD[ind]

Providing a subset is unhelpful. I gave you explicit dput() code to
use; that's how we need data to be provided.

I've given you various suggestions; if you don't implement them then
there's not much point in me trying to help.

Sarah
On Fri, Aug 3, 2012 at 4:54 PM, Yolande Tra <yolande.tra at gmail.com> wrote:

  
    
  
#
This runs, so there must be something else going on that you haven't
told us about.


diveCond <- structure(list(dive_id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
timestamp = c("2008-08-06 08:49:00",
"2008-08-06 10:03:00", "2008-08-06 10:25:00", "2008-08-08 09:42:00",
"2008-08-08 10:53:00", "2008-08-08 12:42:00", "2008-08-11 10:10:00",
"2008-08-11 11:01:00", "2008-08-11 11:16:00", "2008-08-11 14:12:00"
), visability = c(3.5, 3.5, NA, 4, NA, NA, NA, 7, 6.5, NA), r_wvht = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), r_dpd = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), r_apt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), r_mwd = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), r_wtmp = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), l_salinity = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), l_o2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), l_hs = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), l_tp = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), l_wdir = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), l_along = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), l_cross = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), l_mab = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), l_depth = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0)), .Names = c("dive_id", "timestamp", "visability", "r_wvht",
"r_dpd", "r_apt", "r_mwd", "r_wtmp", "l_salinity", "l_o2", "l_hs",
"l_tp", "l_wdir", "l_along", "l_cross", "l_mab", "l_depth"), row.names = c(NA,
10L), class = "data.frame")

regCond_all <-
structure(c(999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 1.9, 2, 1.97, 1.99, 1.83, 1.78, 1.6, 1.52, 1.52,
1.36, 10.53, 9.88, 9.88, 10.53, 10.53, 10.53, 5.26, 9.88, 10.53,
10.53, 5.4, 5.57, 5.46, 5.34, 5.5, 5.59, 5.62, 5.76, 6.23, 6.19,
295, 294, 292, 294, 283, 288, 305, 287, 294, 295, 9999, 9999,
9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 999, 999, 999,
999, 999, 999, 999, 999, 999, 999, 12.7, 12.5, 12.3, 12.6, 12.6,
12.5, 12.5, 12.5, 12.7, 12.7, 999, 999, 999, 999, 999, 999, 999,
999, 999, 999, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99), class = c("xts", "zoo"), .indexCLASS
= c("POSIXct",
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "",
index = structure(c(1217564520,
1217566320, 1217568120, 1217569980, 1217571720, 1217573520, 1217575320,
1217577120, 1217578920, 1217580720), tzone = "", tclass = c("POSIXct",
"POSIXt")), .Dim = c(10L, 13L), .Dimnames = list(NULL, c("WDIR",
"WSPD", "GST", "WVHT", "DPD", "APD", "MWD", "PRES", "ATMP", "WTMP",
"DEWP", "VIS", "TIDE")))

for(i in 1:dim(diveCond)[1]){
 thisStamp <- as.POSIXct(strptime(diveCond$timestamp[i], "%Y-%m-%d %H:%M:%S"))
 ind <- which.min( abs(index(regCond_all) - thisStamp ))
 diveCond$r_wvht[i]<- regCond_all$WVHT[ind]
 diveCond$r_dpd[i]<- regCond_all$DPD[ind]
 diveCond$r_apt[i] <- regCond_all$APD[ind]
 diveCond$r_mwd[i] <- regCond_all$MWD[ind]
 diveCond$r_wtmp[i] <- regCond_all$WTMP[ind]
 }

# this also runs
i <- 10
 thisStamp <- as.POSIXct(strptime(diveCond$timestamp[i], "%Y-%m-%d %H:%M:%S"))
 ind <- which.min( abs(index(regCond_all) - thisStamp ))
 diveCond$r_wvht[i]<- regCond_all$WVHT[ind]
 diveCond$r_dpd[i]<- regCond_all$DPD[ind]
 diveCond$r_apt[i] <- regCond_all$APD[ind]
 diveCond$r_mwd[i] <- regCond_all$MWD[ind]
 diveCond$r_wtmp[i] <- regCond_all$WTMP[ind]

What happens when you do:
i <- 10
 thisStamp <- as.POSIXct(strptime(diveCond$timestamp[i], "%Y-%m-%d %H:%M:%S"))
thisStamp
 ind <- which.min( abs(index(regCond_all) - thisStamp ))
ind
diveCond$r_wvht[i]
regCond_all$WVHT[ind]
On Fri, Aug 3, 2012 at 5:02 PM, Yolande Tra <yolande.tra at gmail.com> wrote:

  
    
#
On Fri, Aug 3, 2012 at 5:14 PM, Yolande Tra <yolande.tra at gmail.com> wrote:
I don't see any reason why that should give the error you report.
diveCond$r_wvht[i] exists, as does regCond_all$WVHT[ind].