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

Sarah Goslee sarah.goslee at gmail.com
Fri Aug 3 22:35:35 CEST 2012


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:
> Here is the description
> I am trying to build the data diveCond from two datasets diveData_2008,
> diveData_2009 and RegCond_all.   How would I dput each data for anyone who
> helps why the code is giving error. The code selects index i of the data
>
> The code starts like this
> diveCond <- data.frame(matrix(0, nrow=97, ncol=17))
> names(diveCond) <- 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")
> dive_id <- 0
> for(i in unique(as.character(index(diveData_2008)))){
>  dive_id <- dive_id+1
>
>  diveCond$dive_id[dive_id] <- dive_id
>  diveCond$timestamp[dive_id] <- as.character(i)
>  diveCond$visability[dive_id] <- as.numeric(diveData_2008[i][1,12])
>
> }
> for(i in unique(as.character(index(diveData_2009)))){
>  dive_id <- dive_id+1
>
>  diveCond$dive_id[dive_id] <- dive_id
>  diveCond$timestamp[dive_id] <- as.character(i)
>  diveCond$visability[dive_id] <- as.numeric(diveData_2009[i][1,5])
>
> }

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


>
> I id not get error so far. Then
>
>> 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]
> + }
> Error in diveCond$r_wvht[i] <- regCond_all$WVHT[ind] :
>   replacement has length zero
> In addition: Warning message:
> In max(i) : no non-missing arguments to max; returning -Inf
>
>
> Thank you for your time
> Y
> On Fri, Aug 3, 2012 at 4:02 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>>
>> Hi Yolande,
>>
>> What's index() ?
>>
>> I get
>> >  ind <- which.min( abs(index(regCond_all) - thisStamp ))
>> Error in which.min(abs(index(regCond_all) - thisStamp)) :
>>   could not find function "index"
>>
>> There's probably an easier way to do whatever you're trying to
>> accomplish, but I'm afraid I can't tell what that is from just your
>> code, especially without being able to figure out how ind is
>> calculated. Can you describe what you want to do and what the result
>> should look like when run with the first 10 rows of your data, as you
>> provided with dput()?
>>
>> Sarah
>>
>> On Fri, Aug 3, 2012 at 3:49 PM, Yolande Tra <yolande.tra at gmail.com> wrote:
>> > Hi,
>> >
>> > Here is my data, the first 10 rows
>> >  > u=regCond_all[1:10,]
>> >> dput(u)
>> > 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")))
>> >
>> > I tried to copy some fields from this data to another data. I got error.
>> > Could you please help? Thanks.
>> > diveCond <- data.frame(matrix(0, nrow=97, ncol=17))
>> > names(diveCond) <- 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")
>> > dive_id <- 0
>> >  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]
>> > }
>> >
>> > Error in diveCond$r_wvht[i] <- regCond_all$WVHT[ind] :
>> >   replacement has length zero
>> > In addition: Warning message:
>> > In max(i) : no non-missing arguments to max; returning -Inf
>> > If you could explain the error that would help prevent future errors.
>> >
>> > Thanks,
>> > Y


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list