[R] Find values from one dataframe between certain values in another dataframe

mwege RProgStuff at gmail.com
Wed Jun 15 12:21:00 CEST 2011


Hi all,

I have a 2 files, one with a series of beginning and end times of animal
dives in (lets call it dives). The other file is readings from a time-depth
recorder, there is a datetime reading for every second, a temperature and
light level reading (lets call it tdr). 

Now I want to say from the file dives, using the start and end time of the
dives, go to the tdr file and find me the temperature values that falls
inbetween the times of the start and end times.

So this is what I have written:

dives<-read.csv(file)        #table with begin and end times of dives
tdr<-read.csv(file2)        #table with every second readings of temp and
light level


dat<-{} #just create empty thing
for (i in 1:nrow(dives)){
st<-x[i,1] # start datetime value for dive[i]. Column one is the start of
dive datetime value
ed<-x[i,4] # end datetime value for dive[i]     Column 4 is the end of the
dive datetime value
f<-which(tdr[,2]>=st & tdr[,2]<=ed) # find row numbers where tdr datetimes
is between start          
                                                    #end times of dive[i].
Column 2 is the datetime value
z1<-tdr[f,5] # extract temperature values
maxtemp<-max(z1) #out of those values find the max value
dat<-rbind(dat,maxtemp) #add that row onto a dat
}

The problem is when I just want to check wat f is it keeps telling me
interger(0). It says that there are no values in tdr that falls between the
start and end of the dives. But there is, I have checked. 

I reckoned that it has to do with the format of the datetime values. I
couldnt find how to convert it to a numeric value. At the moment my datetime
values are in a POSIXct format defined as follows

 as.POSIXct(strptime((dive$begindive),'%Y/%m/%d %H:%M:%S'),tz="GMT")

I have also tried to sort the tdr data first in ascending order
tdr <- tdr[order(tdr$datetime),] #sort z according to dtime

I have even tried to convert to numeric format in excell (all three datetime
values) and then use those numerics in R but it still doesnt want to work.

The problem lies in the f<-which(tdr[,2]>=st & tdr[,2]<=ed)  line.
It doenst find any values from tdr that are between st and ed. But there
definately is, I have done a manual check.

Any help is appreciated, thank you.
Mia 

--
View this message in context: http://r.789695.n4.nabble.com/Find-values-from-one-dataframe-between-certain-values-in-another-dataframe-tp3599033p3599033.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list