I have tried 1 and 2. Then for i=10, the error came back
> i=1
> thisStamp <- as.POSIXct(strptime(diveCond$timestamp[1], "%Y-%m-%d
%H:%M:%S"))
> ind <- which.min( abs(index(regCond_all) - thisStamp ))
> diveCond$r_wvht[1]<- regCond_all$WVHT[ind]
> diveCond$r_dpd[1]<- regCond_all$DPD[ind]
> diveCond$r_apt[1] <- regCond_all$APD[ind]
> diveCond$r_mwd[1] <- regCond_all$MWD[ind]
> diveCond$r_wtmp[1] <- regCond_all$WTMP[ind]
> i=2
> thisStamp <- as.POSIXct(strptime(diveCond$timestamp[2], "%Y-%m-%d
%H:%M:%S"))
> ind <- which.min( abs(index(regCond_all) - thisStamp ))
> diveCond$r_wvht[2]<- regCond_all$WVHT[ind]
> diveCond$r_dpd[2]<- regCond_all$DPD[ind]
> diveCond$r_apt[2] <- regCond_all$APD[ind]
> diveCond$r_mwd[2] <- regCond_all$MWD[ind]
> diveCond$r_wtmp[2] <- regCond_all$WTMP[ind]
> i=10
> thisStamp <- as.POSIXct(strptime(diveCond$timestamp[10], "%Y-%m-%d
%H:%M:%S"))
> ind <- which.min( abs(index(regCond_all) - thisStamp ))
> diveCond$r_wvht[10]<- regCond_all$WVHT[ind]
Error in diveCond$r_wvht[10] <- regCond_all$WVHT[ind] :
replacement has length zero
In addition: Warning message:
In max(i) : no non-missing arguments to max; returning -Inf
> diveCond$r_dpd[10]<- regCond_all$DPD[ind]
Error in diveCond$r_dpd[10] <- regCond_all$DPD[ind] :
replacement has length zero
In addition: Warning message:
In max(i) : no non-missing arguments to max; returning -Inf
> diveCond$r_apt[10] <- regCond_all$APD[ind]
Error in diveCond$r_apt[10] <- regCond_all$APD[ind] :
replacement has length zero
In addition: Warning message:
In max(i) : no non-missing arguments to max; returning -Inf
> diveCond$r_mwd[10] <- regCond_all$MWD[ind]
Error in diveCond$r_mwd[10] <- regCond_all$MWD[ind] :
replacement has length zero
In addition: Warning message:
In max(i) : no non-missing arguments to max; returning -Inf
Here is a subset of the data
>u=regCond_all
>u1<-data.frame(date=index(u),coredata(u))
> subset(u1,WVHT<0.61)
date WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP
DEWP
408 2008-08-09 12:52:00 999 99 99 0.59 12.50 5.52 247 9999 999 12.8
999
409 2008-08-09 13:22:00 999 99 99 0.60 11.76 5.42 239 9999 999 12.8
999
950 2008-08-20 22:22:00 999 99 99 0.57 15.38 5.93 208 9999 999 15.9
999
2689 2009-10-11 05:33:00 999 99 99 0.59 11.11 6.29 256 9999 999 12.6
999
2691 2009-10-11 06:33:00 999 99 99 0.60 10.53 6.84 278 9999 999 12.7
999
2717 2009-10-11 19:33:00 999 99 99 0.60 10.53 5.28 273 9999 999 12.8
999
2724 2009-10-11 23:03:00 999 99 99 0.60 11.11 5.97 252 9999 999 13.2
999
2726 2009-10-12 00:03:00 999 99 99 0.60 10.53 6.08 252 9999 999 13.2
999
2727 2009-10-12 00:33:00 999 99 99 0.60 9.88 5.61 276 9999 999 13.2
999
2731 2009-10-12 02:33:00 999 99 99 0.57 18.18 6.57 249 9999 999 13.2
999
2732 2009-10-12 03:03:00 999 99 99 0.58 8.33 6.66 304 9999 999 13.2
999
2733 2009-10-12 03:33:00 999 99 99 0.59 7.14 6.80 288 9999 999 13.2
999
VIS TIDE
408 99 99
409 99 99
950 99 99
2689 99 99
2691 99 99
2717 99 99
2724 99 99
2726 99 99
2727 99 99
2731 99 99
2732 99 99
2733 99 99
>
Thanks,
Y
On Fri, Aug 3, 2012 at 4:35 PM, Sarah Goslee <[email protected]> wrote:
> 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 <[email protected]> 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 <[email protected]>
> 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 <[email protected]>
> 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
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.