[R] labelling specific points xyplot
Hello R-helpers, I'm trying to add labels to points in xyplot graphs, but I want lo label only those points which have a certain level of my grouping variable, and have encountered a few problems. An example dataframe that goes with the following code is at the end of this message. 1st step, adding labels (from another column in the dataframe) to my chosen points : I've figures this out, but I am passing my whole dataframe as an extra argument (arg1) which I realize is probably useless, so if anybody can give me a way to work around this... xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,...) étiq-rep(,times=length(x)) étiq[groups==arg2]-as.character(arg1[groups==arg2,arg3]) étiq-as.character(étiq) panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) } ) What I would also like is to write a function that is robust for multiple panel displays or subsetting : obviously, the following example doesn't work : xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,...) étiq-rep(,times=length(x)) étiq[groups==arg2]-as.character(arg1[groups==arg2,arg3]) étiq-as.character(étiq) panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) }, subset=pp38 ) so I figured passing on the subscripts argument to my panel function would be the way to go, but I've come across some behavior I can't make sense of : xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,...) #étiq-rep(,times=length(x)) #étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2]) #étiq-as.character(étiq) #panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) }, subset=pp38 ) returns : Erreur dans inherits(x, factor) : l'argument groups est manquant, avec aucune valeur par défaut trying it this way I get a graph, but the groups have gone haywire, and I can't figure out why xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,subscripts,...) #étiq-rep(,times=length(x)) #étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2]) #étiq-as.character(étiq) #panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) }, subset=pp38 ) finally, I ran the same call taking the '#' out : xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,subscripts,...) étiq-rep(,times=length(x)) étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2]) étiq-as.character(étiq) panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) }, subset=pp38 ) which returns : Erreur dans étiq[groups[subscripts] == arg2] - as.character(arg1[subscripts, : NAs interdits dans les affectations indicées I've racked my head and can't make out what's wrong with this line : étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2]) That's a lot of questions in one, so thanks to anyone who can help me out, even for just one of them !! ## dataframe follows : vari-c(3,3,3,3,3,2,3,3,3,3,3,3,1,3,2,2,1,1, 1,1,1,2,3,1,3,3,3,3,3,3,3,3,2,1,3,1, 1,2,2,1,3,3,3,2,1,3,1,2,3,2,1,3,3,3, 1,1,2,1,1,2,1,3,1,2,1,1,1,2,1,1,3,2, 3,3,2,1,1,1,2,3,2,2,2,2,2,2,3,3,3,3, 1,1,1,1,2,1,3,2,3,2) pp-c(NA,49.440,42.900,44.020,48.892,NA,NA,48.800,49.710,49.200, 46.100,44.075,NA,47.855,42.800,NA,32.696,35.900,NA,NA, 43.249,NA,NA,NA,49.405,49.900,47.200,52.600,51.300,51.200, NA,54.200,NA,NA,NA,48.378,44.080,48.000,47.400,NA, 51.013,54.900,53.725,48.605,44.033,NA,43.200,49.000,NA,45.800,
[R] multiple nls - next fit even after convergence problem
Hello R-gurus, I'm trying to adjust different growth curves to a rather extensive dataset. I wrote up a function to go through all of them, but am encountering a problem : among the more than 1000 curves I have, obviously for some of them I encounter conversion problems. I'd like for my function to keep going to the next curve and store the fact that for curve number X I had a convergence problem. This is my original function : comp.fit.2-function(tab) { fit.log-nls(surf.vert.tot ~ 100/(1+exp(((log(81))/a)*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot76)1 | sum(tab$surf.vert.tot15)1,400,-max(tab$sum.T.levee[tab$surf.vert.tot76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-50)==min(abs(tab$surf.vert.tot-50),na.rm=T)]),data=tab,control=list(maxiter=100)) fit.exp-nls(surf.vert.tot ~ ifelse(100-10*exp((log(9)/a)*(sum.T.levee-b))0,0,100-10*exp((log(9)/a)*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot76)1 | sum(tab$surf.vert.tot15)1,400,-max(tab$sum.T.levee[tab$surf.vert.tot76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-90)==min(abs(tab$surf.vert.tot-90),na.rm=T)]),data=tab,control=list(maxiter=100)) fit.gomp-nls(surf.vert.tot ~ 100*exp(-(log(10/9))*exp(-( ( log(log(10/9))-log(log(10)) )/a )*(sum.T.levee-b))), start=list( a=ifelse(sum(tab$surf.vert.tot76)1 | sum(tab$surf.vert.tot15)1,400,-max(tab$sum.T.levee[tab$surf.vert.tot76],na.rm=T)+min(tab$sum.T.levee[tab$surf.vert.tot15],na.rm=T)), b=tab$sum.T.levee[abs(tab$surf.vert.tot-90)==min(abs(tab$surf.vert.tot-90),na.rm=T)]),data=tab,control=list(maxiter=100)) rmse.log-sqrt(mean(residuals(fit.log)^2)) rmse.exp-sqrt(mean(residuals(fit.exp)^2)) rmse.gomp-sqrt(mean(residuals(fit.gomp)^2)) data.frame(rmse.log=rmse.log,rmse.gomp=rmse.gomp,rmse.exp=rmse.exp,semis=unique(tab$semis),densite=unique(tab$densite),traitement=unique(tab$traitement),bloc=unique(tab$bloc),num.feuille.def=unique(tab$num.feuille.def)) } I've thought of just storing the 3 model results in a list, and then going through each model object, but even then, when I have a convergence problem, the function breaks off. What I'd like is to find a way for the function to keep running despite the convergence problem, and store in the model object NA or something like that... Does anybody have an idea for this ? Below is a sample of my data set, called tab.ex, with the points for 3 curves. The 1st and 3rd converge, ans the second fails and breaks off the function. Thanks in advance for your suggestions !!! tab.ex$semis-as.factor(tab.ex$semis) tab.ex$densite-as.factor(tab.ex$densite) tab.ex$bloc-as.factor(tab.ex$bloc) tab.ex$num.feuille.def-as.factor(tab.ex$num.feuille.def) tab.ex$num.not-as.factor(tab.ex$num.not) tab.ex$sum.T.levee-tab.ex$sum.T.semis-117 semis densite traitement bloc num.feuille.def surf.vert.tot sum.T.semis 1 1 NT 1 1 100 1764.95 1 1 NT 1 1 100 1867.3 1 1 NT 1 1 98.5083 1983.25 1 1 NT 1 1 37.91416667 2200 1 1 NT 1 1 0 2308.45 1 1 NT 1 1 0 2436.05 1 1 NT 1 1 0 2549.95 1 1 NT 1 1 0 2678.05 1 2 NT 1 1 100 1641.05 1 2 NT 1 1 100 1764.95 1 2 NT 1 1 100 1867.3 1 2 NT 1 1 99.5833 1983.25 1 2 NT 1 1 0 2200 1 2 NT 1 1 0 2308.45 1 2 NT 1 1 0 2436.05 1 2 NT 1 1 0 2549.95 1 2 NT 1 1 0 2678.05 1 1 T 1 3 100 1440.1 1 1 T 1 3 99.45545455 1525.7 1 1 T 1 3 98.11727273 1641.05 1 1 T 1 3 97.65545455 1764.95 1 1 T 1 3 95.113 1867.3 1 1 T 1 3 93.04545455 1983.25 1 1 T 1 3 87.0067 2200 1 1 T 1 3 50.895 2308.45 1 1 T 1 3 21.67416667 2436.05 1 1 T 1 3 4.3 2549.95 1 1 T 1 3 0 2678.05 __ R-help@stat.math.ethz.ch 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.
[R] regular expressions : extracting numbers
Hello all, I have a vector of character strings, in which I have letters, numbers, and symbols. What I wish to do is obtain a vector of the same length with just the numbers. A quick example - extract of the original vector : lema, rb 2% rb 2% rb 3% rb 4% rb 3% rb 2%,mineuse rb rb rb 12 rb rj 30% rb rb rb 25% rb rb rb rj, rb and the type of thing I wish to end up with : 2 2 3 4 3 2 12 30 25 or, instead of , NA would be acceptable (actually it would almost be better for me) Anyways, I've been battling with gsub() and things of the sort, but I'm drowning in the regular expressions, despite a few hours of looking at Perl tutorials... So if anyone can help me out, it would be greatly appreciated!! In advance, thanks very much. David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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.
[R] RE : reshape question
Thanks very much for your help. I remain puzzled by the original behavior of reshape though... Does anyone have an explanation to this ? Regards, David -Message d'origine- De : Gabor Grothendieck [mailto:[EMAIL PROTECTED] Envoyé : jeudi 3 mai 2007 19:27 À : GOUACHE David Cc : r-help@stat.math.ethz.ch Objet : Re: [R] reshape question Here is a minor improvement (cn and nm as before): reshape(tab, direction = long, varying = nm, v.names = names(nm)) On 5/3/07, Gabor Grothendieck [EMAIL PROTECTED] wrote: Try this: cn - names(tab) nm - list(Date = grep(Date_, cn, value = TRUE), Variable = grep(Variable, cn, value = TRUE), Valeur = grep(Valeur, cn, value = TRUE) ) reshape(tab, direction = long, varying = nm) On 5/3/07, GOUACHE David [EMAIL PROTECTED] wrote: Hello all, A quick question concerning a behavior of reshape I fail tu understand, probably something obvious but I just can't see it. With the following data frame further referred to as tab : Variete;Semis.Date;Date_Mesure.1;Variable_Mesure.1;Valeur_Mesure.1;Date_Mesure.2;Variable_Mesure.2;Valeur_Mesure.2;Date_Mesure.3;Variable_Mesure.3;Valeur_Mesure.3;Date_Mesure.4;Variable_Mesure.4;Valeur_Mesure.4;Date_Mesure.5;Variable_Mesure.5;Valeur_Mesure.5;Date_Mesure.6;Variable_Mesure.6;Valeur_Mesure.6;Date_Mesure.7;Variable_Mesure.7;Valeur_Mesure.7;Date_Mesure.8;Variable_Mesure.8;Valeur_Mesure.8;Date_Mesure.9;Variable_Mesure.9;Valeur_Mesure.9;Date_Mesure.10;Variable_Mesure.10;Valeur_Mesure.10 A;22/10/2004;38527;Partie aérienne.Verse;3.23;38516;Grain.Humidité;0.71;38523;Grain.Humidité;0.67;38526;Grain.Humidité;0.61;38530;Grain.Humidité;0.56;38532;Grain.Humidité;0.53;38537;Grain.Humidité;0.47;38539;Grain.Humidité;0.45;38544;Grain.Humidité;0.44;38551;Grain.Humidité;0.17 B;22/10/2004;38527;Partie aérienne.Verse;3.45;38516;Grain.Humidité;0.76;38523;Grain.Humidité;0.68;38526;Grain.Humidité;0.63;38530;Grain.Humidité;0.59;38532;Grain.Humidité;0.54;38537;Grain.Humidité;0.49;38539;Grain.Humidité;0.48;38544;Grain.Humidité;0.46;38551;Grain.Humidité;0.15 I execute the following command : reshape(tab,direction=long,varying=grep(Mesure,colnames(tab))) This gives me what I need except for a small glitch : I end up with the column Variable_Mesure containing the numerical values which were contained in the Valeur_Mesure.number columns of the wide data frame and conversely I have in the column Valeurs_Mesure column of my long dataframe the factor levels contained in the Variable_Mesure.number columns of the original wide dataframe. What is this inversion due to ? If it is an error on my part, which I suppose it is, how do I correct this ? Thanls in advance. PS - I am running R 2.3.1 on a Windows platform David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
[R] reshape question
Hello all, A quick question concerning a behavior of reshape I fail tu understand, probably something obvious but I just can't see it. With the following data frame further referred to as tab : Variete;Semis.Date;Date_Mesure.1;Variable_Mesure.1;Valeur_Mesure.1;Date_Mesure.2;Variable_Mesure.2;Valeur_Mesure.2;Date_Mesure.3;Variable_Mesure.3;Valeur_Mesure.3;Date_Mesure.4;Variable_Mesure.4;Valeur_Mesure.4;Date_Mesure.5;Variable_Mesure.5;Valeur_Mesure.5;Date_Mesure.6;Variable_Mesure.6;Valeur_Mesure.6;Date_Mesure.7;Variable_Mesure.7;Valeur_Mesure.7;Date_Mesure.8;Variable_Mesure.8;Valeur_Mesure.8;Date_Mesure.9;Variable_Mesure.9;Valeur_Mesure.9;Date_Mesure.10;Variable_Mesure.10;Valeur_Mesure.10 A;22/10/2004;38527;Partie aérienne.Verse;3.23;38516;Grain.Humidité;0.71;38523;Grain.Humidité;0.67;38526;Grain.Humidité;0.61;38530;Grain.Humidité;0.56;38532;Grain.Humidité;0.53;38537;Grain.Humidité;0.47;38539;Grain.Humidité;0.45;38544;Grain.Humidité;0.44;38551;Grain.Humidité;0.17 B;22/10/2004;38527;Partie aérienne.Verse;3.45;38516;Grain.Humidité;0.76;38523;Grain.Humidité;0.68;38526;Grain.Humidité;0.63;38530;Grain.Humidité;0.59;38532;Grain.Humidité;0.54;38537;Grain.Humidité;0.49;38539;Grain.Humidité;0.48;38544;Grain.Humidité;0.46;38551;Grain.Humidité;0.15 I execute the following command : reshape(tab,direction=long,varying=grep(Mesure,colnames(tab))) This gives me what I need except for a small glitch : I end up with the column Variable_Mesure containing the numerical values which were contained in the Valeur_Mesure.number columns of the wide data frame and conversely I have in the column Valeurs_Mesure column of my long dataframe the factor levels contained in the Variable_Mesure.number columns of the original wide dataframe. What is this inversion due to ? If it is an error on my part, which I suppose it is, how do I correct this ? Thanls in advance. PS - I am running R 2.3.1 on a Windows platform David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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.
[R] RE : add arrows to barchart with groups
Hello and thank you for this first response I'll rephrase what I mean by more general: in a case where I have only 2 levels in groups, but in which I have more than 2 levels for variable s, and in a case where the second bar is not necessarily higher than the first. Thanks in advance. Best regards, David Gouache Arvalis - Institut du Végétal -Message d'origine- De : Deepayan Sarkar [mailto:[EMAIL PROTECTED] Envoyé : vendredi 27 avril 2007 19:52 À : GOUACHE David Cc : r-help@stat.math.ethz.ch Objet : Re: [R] add arrows to barchart with groups On 4/27/07, GOUACHE David [EMAIL PROTECTED] wrote: Hello Rhelpers, I am trying to represent the following data (hereafter named donnees) in a barchart with a grouping variable : sitetraitement dates res 1 NT 17/10/2005 normal 76.2 1 T 17/10/2005 normal 103.2 1 NT 23/11/2005 tardif 81.6 1 T 23/11/2005 tardif 98 2 NT 15/10/2005 normal 72.71 2 T 15/10/2005 normal 94.47 2 NT 15/11/2005 tardif 79.65 2 T 15/11/2005 tardif 94.7 barchart(res~s|site,groups=traitement,data=donnees) What I'd like to do is for each site represent with an arrow the difference in value of variable res between normal and tardif values of variable s. I've found one way of doing it: trellis.focus(panel,1,1) xx-trellis.panelArgs()$x yy-trellis.panelArgs()$y panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3) panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],\nq/ha),font=2) trellis.focus(panel,2,1) xx-trellis.panelArgs()$x yy-trellis.panelArgs()$y panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3) panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],\nq/ha),font=2) trellis.unfocus() But I would prefer doing this within a custom panel function so I can apply it more generally, and I haven't been able to figure out how... Could anyone give me a hand? The obvious analog (just copy/pasting your code) is: my.panel - function(x, y, ...) { panel.barchart(x, y, ...) xx - x yy - y panel.arrows(as.numeric(xx)[c(1,3)]-0.1, yy[c(1,3)], as.numeric(xx)[c(1,3)]-0.1, yy[c(2,4)], lwd = 2, code = 3) panel.text(as.numeric(xx)[c(1,3)] - 0.35, c(87,87), paste(yy[c(2,4)] - yy[c(1,3)], \nq/ha), font=2) } and this seems to work: barchart(res~s|site,groups=traitement,data=donnees, panel = my.panel) barchart(res~s|site,groups=traitement,data=donnees, panel = my.panel, origin = 0) I'm not sure what else you are looking for and what you mean by more general. For example, it's not clear what you want to happen If you have more than 2 levels in 'groups', or if the second bar is not always higher than the first. -Deepayan __ R-help@stat.math.ethz.ch 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.
[R] add arrows to barchart with groups
Hello Rhelpers, I am trying to represent the following data (hereafter named donnees) in a barchart with a grouping variable : sitetraitement dates res 1 NT 17/10/2005 normal 76.2 1 T 17/10/2005 normal 103.2 1 NT 23/11/2005 tardif 81.6 1 T 23/11/2005 tardif 98 2 NT 15/10/2005 normal 72.71 2 T 15/10/2005 normal 94.47 2 NT 15/11/2005 tardif 79.65 2 T 15/11/2005 tardif 94.7 barchart(res~s|site,groups=traitement,data=donnees) What I'd like to do is for each site represent with an arrow the difference in value of variable res between normal and tardif values of variable s. I've found one way of doing it: trellis.focus(panel,1,1) xx-trellis.panelArgs()$x yy-trellis.panelArgs()$y panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3) panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],\nq/ha),font=2) trellis.focus(panel,2,1) xx-trellis.panelArgs()$x yy-trellis.panelArgs()$y panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3) panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],\nq/ha),font=2) trellis.unfocus() But I would prefer doing this within a custom panel function so I can apply it more generally, and I haven't been able to figure out how... Could anyone give me a hand? Thanks in advance. David Gouache Arvalis - Institut du Végétal __ R-help@stat.math.ethz.ch 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.
[R] RE : avoiding a loop: cumsum-like
Thanks Petr for taking a stab at it. I have yet to figure out a way to do it, but if I do I'll post it. Cheers David -Message d'origine- De : Petr Pikal [mailto:[EMAIL PROTECTED] Envoyé : vendredi 3 novembre 2006 09:05 À : GOUACHE David; r-help@stat.math.ethz.ch Objet : Re: [R] avoiding a loop: cumsum-like Hi I have not seen any answer yet so I wil try (partly). I believe that the loop can be vectorised but I am a little bit lost in your fors and ifs. I found that first part of res is same as cumsum(tab$x.jour) until about 81st value. However I did not decipher how to compute the remaining part. I tried to add cumsum(tab$posit.lat) (after changing NA to 0) what is not correct. Probably some combination of logical operation and summing can do what you want. I thought that something like ((cumsum(tab$posit.lat)*0.8)*(cumsum(tab$x.jour)30)+cumsum(tab$x.jour )) can do it but the result is defferent from your computation. Not much of help, but maybe you can do better with above suggestion. Petr On 2 Nov 2006 at 11:15, GOUACHE David wrote: Date sent: Thu, 2 Nov 2006 11:15:49 +0100 From: GOUACHE David [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] avoiding a loop: cumsum-like Hello Rhelpers, I need to run the following loop over a large number of data-sets, and was wondering if it could somehow be vectorized. It's more or less a cumulative sum, but slightly more complex. Here's the code, and an example dataset (called tab in my code) follows. Thanks in advance for any suggestions! res-0 for (i in min(tab$Date):max(tab$Date)) { if (is.na(tab$posit.lat[tab$Date==i])==T) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { if (res[tab$posit.lat[tab$Date==i]+1]30) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]+0.8*res[tab$pos it.lat[tab$Date==i]+1]) } } } res[-1] Date x.jour posit.lat 35804 0 NA 35805 0 NA 35806 0 NA 35807 0 NA 35808 0 NA 35809 2.97338883 NA 35810 2.796389915 NA 35811 0 NA 35812 0 NA 35813 1.000711886 NA 35814 0.894422571 NA 35815 0 NA 35816 0 NA 35817 0 NA 35818 0 NA 35819 0 NA 35820 0 NA 35821 0 NA 35822 0 NA 35823 0 NA 35824 0 NA 35825 0 NA 35826 0 NA 35827 0 NA 35828 0 NA 35829 0 NA 35830 0 NA 35831 0 NA 35832 0 NA 35833 0 NA 35834 0 NA 35835 0 NA 35836 0 NA 35837 0 NA 35838 0 NA 35839 0 NA 35840 2.47237455 NA 35841 0 2 35842 0 3 35843 0 4 35844 0 5 35845 0 6 35846 0 7 35847 4.842160488 8 35848 2.432125036 9 35849 0 10 35850 0 12 35851 0 14 35852 0 16 35853 3.739683882 18 35854 1.980214421 20 35855 0 22 35856 0 24 35857 5.953444078 27 35858 6.455722475 29 35859 0 31 35860 3.798690334 32 35861 6.222993364 34 35862 3.746243098 35 35863 0 35 35864 0 36 35865 0 37 35866 0 38 35867 0 38 35868 0 39 35869 0 40 35870 0 41 35871 0 42 35872 0 43 35873 0 44 35874 0 45 35875 0 46 35876 0 47 35877 1.951774892 48 35878 0 49 35879 0 50 35880 1.702837643 50 35881 0 52 35882 0 53 35883 0 54 35884 0 55 35885 5.953444078 57 35886 0 58 35887 5.737515358 59 35888 0 61 35889 6.215941227 63 35890 4.731576675 64 35891 0 66 35892 2.255448314 66 35893 3.782283008 67 35894 3.244474546 68 35895 1.808553193 69 35896 2.622680002 70 35897 0 71 35898 0 72 35899 0 72 35900 1.7084177 73 35901 1.28455982 74 35902 2.320013736 76 35903 0 77 35904 0 78 35905 0 79 35906 0 79 35907 0 80 35908 6.716812458 81 35909 0 82 35910 6.796571531 84 35911 5.573668337 85 35912 5.42513958 86 35913 3.774513877 86 35914 0 87 35915 0 89 35916 0 90 35917 4.208252725 91 35918 0 92 35919 0 93 35920 0 95 35921 5.70023661 97 35922 0 98 35923 0 100 35924 0 102 35925 0 103 35926 0 104 David Gouache Arvalis - Institut du Végétal Station de La Miničre 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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. Petr Pikal [EMAIL PROTECTED] David
[R] avoiding a loop: cumsum-like
Hello Rhelpers, I need to run the following loop over a large number of data-sets, and was wondering if it could somehow be vectorized. It's more or less a cumulative sum, but slightly more complex. Here's the code, and an example dataset (called tab in my code) follows. Thanks in advance for any suggestions! res-0 for (i in min(tab$Date):max(tab$Date)) { if (is.na(tab$posit.lat[tab$Date==i])==T) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { if (res[tab$posit.lat[tab$Date==i]+1]30) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]+0.8*res[tab$posit.lat[tab$Date==i]+1]) } } } res[-1] Datex.jour posit.lat 35804 0 NA 35805 0 NA 35806 0 NA 35807 0 NA 35808 0 NA 35809 2.97338883 NA 35810 2.796389915 NA 35811 0 NA 35812 0 NA 35813 1.000711886 NA 35814 0.894422571 NA 35815 0 NA 35816 0 NA 35817 0 NA 35818 0 NA 35819 0 NA 35820 0 NA 35821 0 NA 35822 0 NA 35823 0 NA 35824 0 NA 35825 0 NA 35826 0 NA 35827 0 NA 35828 0 NA 35829 0 NA 35830 0 NA 35831 0 NA 35832 0 NA 35833 0 NA 35834 0 NA 35835 0 NA 35836 0 NA 35837 0 NA 35838 0 NA 35839 0 NA 35840 2.47237455 NA 35841 0 2 35842 0 3 35843 0 4 35844 0 5 35845 0 6 35846 0 7 35847 4.842160488 8 35848 2.432125036 9 35849 0 10 35850 0 12 35851 0 14 35852 0 16 35853 3.739683882 18 35854 1.980214421 20 35855 0 22 35856 0 24 35857 5.953444078 27 35858 6.455722475 29 35859 0 31 35860 3.798690334 32 35861 6.222993364 34 35862 3.746243098 35 35863 0 35 35864 0 36 35865 0 37 35866 0 38 35867 0 38 35868 0 39 35869 0 40 35870 0 41 35871 0 42 35872 0 43 35873 0 44 35874 0 45 35875 0 46 35876 0 47 35877 1.951774892 48 35878 0 49 35879 0 50 35880 1.702837643 50 35881 0 52 35882 0 53 35883 0 54 35884 0 55 35885 5.953444078 57 35886 0 58 35887 5.737515358 59 35888 0 61 35889 6.215941227 63 35890 4.731576675 64 35891 0 66 35892 2.255448314 66 35893 3.782283008 67 35894 3.244474546 68 35895 1.808553193 69 35896 2.622680002 70 35897 0 71 35898 0 72 35899 0 72 35900 1.7084177 73 35901 1.28455982 74 35902 2.320013736 76 35903 0 77 35904 0 78 35905 0 79 35906 0 79 35907 0 80 35908 6.716812458 81 35909 0 82 35910 6.796571531 84 35911 5.573668337 85 35912 5.42513958 86 35913 3.774513877 86 35914 0 87 35915 0 89 35916 0 90 35917 4.208252725 91 35918 0 92 35919 0 93 35920 0 95 35921 5.70023661 97 35922 0 98 35923 0 100 35924 0 102 35925 0 103 35926 0 104 David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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.
[R] RE : Individual Based Model and/or Cellular automata
try this package: simecol and check out its website: http://hhbio.wasser.tu-dresden.de/projects/simecol/ -Message d'origine- De : Milton Cezar Ribeiro [mailto:[EMAIL PROTECTED] Envoyé : jeudi 2 novembre 2006 03:16 À : r-help@stat.math.ethz.ch Objet : [R] Individual Based Model and/or Cellular automata Hi R-gurus, Is there someone working with Individual-Based-Models (IBM) or Agent-Based-Models (ABM) with our withour Cellular Automata (CA) in R? I´m looking for develop some ecological applications, Kind regards, Miltinho Brazil - [[alternative HTML version deleted]] David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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.
[R] adding multiple fitted curves to xyplot graph
Hello RHelpers, This may already have been answered, but despite days of scouring through the archives I haven't found it. My goal is to add multiple fitted curves to a plot. An example data set (a data frame named df in following code) is: x1 y1 factor1 4 1298.25 0. 1 5 1393.25 0. 1 6 1471.50 0.04597701 1 7 1586.70 2.56908046 1 8 1692.10 11.14080460 1 9 1832.55 45.50459770 1 10 1928.30 65.5600 1 11 2092.40 100. 1 31 1202.90 0. 2 41 1298.25 0. 2 51 1393.25 0.37885057 2 61 1471.50 0.76839080 2 71 1586.70 7.75206897 2 81 1692.10 50.19448276 2 91 1832.55 94.08045977 2 101 1928.30 100. 2 111 2092.40 100. 2 14 1028.50 0. 3 22 1106.40 0.04938272 3 32 1202.90 0.03448276 3 42 1298.25 0.34482759 3 52 1393.25 1.43850575 3 62 1471.50 1.96850575 3 72 1586.70 36.80597701 3 82 1692.10 92.83390805 3 92 1832.55 100. 3 15 1028.50 0.09638554 4 23 1106.40 0.39988506 4 33 1202.90 0.49321839 4 43 1298.25 1.66045977 4 53 1393.25 7.51137931 4 63 1471.50 42.02724138 4 73 1586.70 99.12068966 4 83 1692.10 100. 4 I plot this with xyplot: trellis.par.set(background,white) trellis.par.set(list(superpose.symbol=list(pch=c(15:17,21,25 xyplot(y1 ~ x1, data=df, groups=factor1, type = p, auto.key = list(space = right, points = TRUE, lines = FALSE)) For each level of factor1 I fit a growth curve: fit.curve-function(tab) { res.fit-nls(y1 ~ 100/(1+exp(((-log(81))/a)*(x1-b))), start=list(a=min(tab$x1[tab$y176],na.rm=T)-max(tab$x1[tab$y115],na.rm=T),b=tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)][!is.na(tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)])]),data=tab) coef(res.fit) } by(df,list(df$factor1),fit.curve) I would like to add the 4 curves corresponding to these 4 fits to my graphic. The elegant way would be a custom panel function I suppose, but I haven't been able to write one up... Could someone help me out on this please? In advance thanks very much!!! David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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.