On Nov 25, 2011, at 12:50 PM, Robin CorriĆ  wrote:




Dear all,

I have a
big data frame:


str(data1)

'data.frame':   18272 obs. of  11 variables:

$ tag  :
int  100001 100002 100003 100005 100007
100008 100009 100011 100012 100014 ...

$ sp   :
Factor w/ 18 levels "acassp","acocar",..: 13 5 7 14 14 18 3
11 13 10 ...

$ gx   :
num  20 10 35 68 88 63 123 115 137 136
...

$ gy   :
num  30 25 24 1 10 40 45 25 23 45 ...

$ d0   :
int  0 0 0 0 0 0 0 0 0 0 ...

$ d1   :
int  395 395 395 395 395 395 395 395 395
395 ...

$ d2   :
int  751 751 751 751 751 751 751 751 751
751 ...

$ d3   :
int  1515 1515 1515 1515 1515 1515 1515
1515 1515 1515 ...

$ d4   :
int  2562 2562 2562 2562 2562 2562 2562
2562 2562 2562 ...

$ block: int
1 1 1 1 1 1 1 1 1 1 ...

$ treat: Factor w/ 4 levels
"I","M","N","T": 1 1 1 1 1 1 1 1 1 1
...


And I need to
do multiple selections of gx and gy for all the levels of: sp, block, treat, and when d0!="NA". Then calculate some spatial functions with the selected gx and gy coordinates, and
save the results with a name according to the selection.


One single
selection could be done and named like that:

acocar1I=subset(data1,(treat=="I"
& data1$block==1 & data1$sp=="acocar" & data1$d0!="NA"))

You should watch out since that is not a valid test for NA. If the values are NA rahter than "NA" you need to use !is.na()

You could set up a condition list which would really be a list of logical vectors and then loop through it like this:

consList[[1]] <- with(data1, treat=="I"&
                             block==1 &
                             sp=="acocar" &
                             d0!="NA")

(Then add more vectors to the list.)

for (cond in condList) { sel.spp[[i]] <- with(
subset(data1, condList[[i]]), spp(x=gx, y=gy, window=wA) )

acocar1I.dp[[i]] <-dval(sel.spp[[i]], 25,2.5,18,20)
                          }

(You could then assemble the various calculations in a dataframe. All of this obviously untested.)

If you really are doing this for "all levels of sp, bloc, treat then there might be further option in the plyr pkg or with aggregate since they provide features for running through all existing combinations using the interaction function. The `expand.grid` function is sometimes useful for building data.frames as scaffolding to hold repeated calculations.

--
David.




These are some
of the functions I have to calculate:

acocar1I.spp<-spp(x=acocar1I$gx,
y=acocar1I$gy, window=wA)

acocar1I.dp<-dval(acocar1I.spp,25,2.5,18,20)


And I want
to create a 'results' object to access easily all the results:

acocar1I.res<-alist()

acocar1I.res$data1<-acocar1I

acocar1I.res$spp<-acocar1I.spp

acocar1I.res$dp<-acocar1I.dp


Can I do everything
in a single loop or a single function?

Depends on specifics.

Many
thanks!











                                        
        [[alternative HTML version deleted]]

______________________________________________
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.

David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to