Dear List, I need some help in coming up with a function that will take two data sets, determine if a value is missing in one, find a value in the second that was taken at about the same time, and substitute the second value in for where the first should have been. My problem is from a fish tracking study. We put acoustic tags in fish and track them for several days. Location data is supposed to be automatically recorded every time we detect a "ping" from the fish. Unfortunately the GPS had some problems and sometimes the fishes depth was recorded but not its location. I fortunately had a back-up GPS that was taking location data every five minutes. I would like to merge the two files, replacing the missing value in the vscan (automatic) file with the location from the garmin file. Since we were getting vscan records every 1-2 seconds and garmin records every 5 minutes, I need to find the right place in the vscan file to place the garmin record - i.e. the closest in time, but not greater than 5 minutes. I have written a function that does this. However, it works with my test data but locks up my computer with my real data. I have several million vscan records and several thousand garmin records. Is there a better way to do this?
My function and test data: myvscan<-data.frame(c(1,NA,1.5),times(c("12:00:00","12:14:00","12:20:00"))) names(myvscan)<-c("Latitude","DateTime") mygarmin<-data.frame(c(20,30,40),times(("12:00:00","12:10:00","12:15:00"))) names(mygarmin)<-c("Latitude","DateTime") minute.diff<-1/24/12 #Time diff is in days, so this is 5 minutes for (k in 1:nrow(myvscan)) { if (is.na(myvscan$Latitude[k])) { if ((min(abs(mygarmin$DateTime-myvscan$DateTime[k]))) < minute.diff ) { index.min.date<-which.min(abs(mygarmin$DateTime-myvscan$DateTime[k])) myvscan$Latitude[k]<-mygarmin$Latitude[index.min.date] }}} I appreciate your help and advice. Aloha, Tim Tim Clark Department of Zoology University of Hawaii ______________________________________________ R-help@r-project.org 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.