[R] Odp: Loop overwrite and data output problems

Petr PIKAL petr.pikal at precheza.cz
Mon Mar 1 12:33:08 CET 2010


Hi

again I believe you are fishing a wrong pond.

r-help-bounces at r-project.org napsal dne 01.03.2010 11:46:12:

> 
> Hi Petr,
> 
> Thank you for your post - I really appreciate you taking the time over 
my
> problem. 
> 
> Apologies for not posting more data, it is just that the data set is 
rather
> large, and I don't like posting the whole thing on the website for that
> reason. 
> 
> I have managed to random sample the 98 individuals so that I effectively 
get
> 98 data points from the data set, I do this with the script below, which 
I
> appreciate may not be the best way to do it:
> 
> for (S in 1:1){
>    Sample.dat<-ALL.R[1,]
>    for (I in 1:98){
>       tmp<-ALL.R[ALL.R$ID2==I,]
>       max<-dim(tmp)[1]
>       if (I==1) Sample.dat[1,]<-tmp[sample(1:max,1),] else {
> Sample.dat<-rbind(Sample.dat,tmp[sample(1:max,1),])

I suggested to you basically this (based on sorted ID2),

# how many unique ids
len<-rle(ALL.R$ID2)$lengths

# how many values are from beginning
shift.len<-c(0,cumsum(len))[-(length(len)+1)]

# get one value from each id - this you can put inside your cycle

samp<-sapply(sapply(split(ALL.R$ID2, ALL.R$ID2), function(x) 1:length(x)), 
sample, 1)

Sample.dat <- ALL.R[shift.len+sample,]

You shall get your 98 random rows, one for each animal.

If ID2 is not sorted you could use the other option

ss<-sample(1:nrow(ALL.R))

sort data.frame according those randomised indices and select let say 
first one

Sample.dat <- sapply(split(ALL.R[ss,], ALL.R[ss,1]), function(x) x[1,])

<snip>

> 
> I then run the random sample of data through a model then using the 
MuMIn
> package and the dredge command I get a list of all of the possible 
subsets
> of the model in order of lowest to highest AIC values, the script below 
is
> shown for that:
> 

you can use those lines without problems

>       m1.R<-glm(cbind(Sample.dat$BEH_T, Sample.dat$BEH_F) ~ 
Sample.dat$SITE +
> Sample.dat$YEAR + Sample.dat$PRECIP_MM_DAY + Sample.dat$PUP_AGE_EST +
> Sample.dat$MO_AIR_TEMP, family="binomial") 


I do not know type of dredge output. So if it is number use

mod[i] <- 

if it is vector use

mod[,i]

if it is list use

mod[[i]]

but you need to initialise mod before cycle

If this is not relevant to your question please try to invent some simple 
dataset for testing and put it to your question.

Regards
Petr



> mod<-dredge(m1.R)
> 
> The problem occurs here, manually I can do this fine, but for a 100 or 
so
> simulations you don't want to do it manually. So I want to take mod[1,] 
for
> each simulation and store that to look at later, but any way in which I 
try
> to do this results in the data being overwritten.
> 
> I'm not sure if this is any clear, I am sorry, It is taking me a little
> longer to get my head around looping scripts!
> 
> Best wishes,
> 
> Ross
> 
> 
> 
> 
> 
> -- 
> View this message in context: 
http://n4.nabble.com/Loop-overwrite-and-data-
> output-problems-tp1570593p1573407.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list