Skip to content
Prev 305837 / 398506 Next

[newbie] aggregating table() results and simplifying code with loop

That's because I'm much closer to you in knowledge about R.
Rui's approach is "very, very  nice but does seem a bit opaque to us newbies.  

I think the first thing is : Do you undrestand the 'result' list structure? It sounds like you do but basically he is processing each set of conditions that you supplied and storing them indiviually as data frames in each element of the list.  

Secondly, to work through the functions you should be able to step through each function without running the function.  


For example for F5 the working parts are
============================================================
sp <- split(w, w$V8)
	res <- do.call( rbind, lapply(sp, f4) )
	res <- data.frame(res)
	res
=============================================================

We see from the main program that 

result <- lapply(want, f5)

so for the above code want is the "w" value.  Just stick it into the code and see what happens. That is print out values as you go. 

Each call seems to go to another function but bit by bit you should be able to see what is happening.  I suspect it will be messy but it should help a bit.

Another trick might be to make up a very tiny data set that meets all conditions and run the program in, I think , debug mode and stick in print statements all along the way so that you can get a feel for what is happening at each step.  

In any case you should be able to extact each condition from result by something like crop  <-  result[[1]] (note [[1]]  rather than [1] and run ddply() or aggregate() on the resulting data frame to see if the output looks reasonable.

Thanks for the quick summary.  Once I realised that WS = Watershed I thought that was what it was likely to be about but it`s handy to know and sometimes even a slight bit of knowledge about the subject area can help alert one to something strange in the output that may indicate a programing or logic problem.

Good luck.   R is amazingly powerful and can often produce results much more quickly than other stats language but it requires a different mindset. I used to get very funny mind aches when I first started using it after years of SAS or SYSTAT or even SPSS.
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!