I know about apply, but I'm not sure how to write it so that it works for embedded for
loops with if
statements and so forth. It gets messy. I am a novice so if you can show me how to properly rewrite the following efficiently it should help a lot.
This is an example of what the dataframe inFile()
should look like. In the code below I subset on the dataframe so that rows are between two dates, abs_start
and abs_end
(you'll notice the CSV has a dates column). I then am trying bin the rows into bins that are input$binning
seconds long. I simply iterate through all the dates and for each date I iterate through all the bins until I find the one that a particular row belongs to. Then I add this bin's number to a list. After doing this for all rows in the dataframe I add the list as a new column to the data.frame
as it now specifies which bin each row belongs to.
This is slow though. The user specifies the bin length and then I want the histrogram to automatically update (using R Shiny framework). How can I speed it up?
# Subset between start and end with bin column and selected mice
subsetTable<-reactive({
subsetMice<-inFile()[inFile()[,TAG_COL]%in%input$tagChooser,]
abs_start <- strptime(input$abs_start,"%Y-%m-%d %H:%M:%S",tz='US/Pacific')
abs_end <- strptime(input$abs_end,"%Y-%m-%d %H:%M:%S",tz='US/Pacific')
if(is.null(subsetMice)||nrow(subsetMice)<=0){
return()
}
# subset between abs_start and abs_end
convertedDates<-strptime(as.character(subsetMice[,DATE_COL]),"%Y-%m-%d %H:%M:%S",tz='US/Pacific')
inF<-subsetMice[convertedDates>abs_start&convertedDates<abs_end,]
convertedDates<-convertedDates[convertedDates>abs_start&convertedDates<abs_end]
bins<-seq(abs_start,abs_end,by=as.integer(input$binning))
bin_no<-length(bins)
bin_nos<-c(NULL)
bin_starts<-c(NULL)
for(i in 1:length(convertedDates)){
for(j in 1:length(bins)){
# Check if at right bin
if(!is.na(bins[j+1])){
if(bins[j]<=convertedDates[i]&&convertedDates[i]<bins[j+1]){
bin_nos[i] <- j
bin_starts[i] <- bins[j]
break
}
}
else{
stopifnot(bins[j]<=convertedDates[i])
bin_nos[i] <- j
bin_starts[i] <- bins[j]
}
}
}
cbind(inF,bin_starts,bin_nos)
})