Re: [R] Repeat if
Birgit Lemcke wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Hi Birgit, This may be what you want: for(i in 1:85) print(do.call(range,list(as.name(paste(V,i,sep=) Jim __ 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.
Re: [R] Repeat if
Hello Jim, thanks for your answer. At the moment I am using this code: Range0-sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= and it works really fine. The code you sent me is also fine but how can I implement, that missing values are TRUE? Thanks a lot for your help Birgit Am 28.06.2007 um 13:01 schrieb Jim Lemon: Birgit Lemcke wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Hi Birgit, This may be what you want: for(i in 1:85) print(do.call(range,list(as.name(paste(V,i,sep=) Jim Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
On 6/29/07, Birgit Lemcke [EMAIL PROTECTED] wrote: Hello Jim, thanks for your answer. At the moment I am using this code: Range0-sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= It is a matter of taste, but I tend to prefer: a - list() for (i in 1:85) a[[i]] = get(paste(V, i, spe=)) sapply(a, range, na.rm=T) The first two lines put your variables inside a list (assuming they are not already in one, e.g., in a data.frame). This is especially interesting if you have other operations to perform on the V1..V85 variables. Christophe and it works really fine. The code you sent me is also fine but how can I implement, that missing values are TRUE? Thanks a lot for your help Birgit Am 28.06.2007 um 13:01 schrieb Jim Lemon: Birgit Lemcke wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Hi Birgit, This may be what you want: for(i in 1:85) print(do.call(range,list(as.name(paste(V,i,sep=) Jim Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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. -- Christophe Pallier (http://www.pallier.org) [[alternative HTML version deleted]] __ 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] Repeat if
Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Thanks that was really a quick answer. It works but I get this warning message anyway: 1: kein nicht-fehlendes Argument für min; gebe Inf zurück (None not- lacking argument for min; give Inf back) 2: kein nicht-fehlendes Argument für max; gebe -Inf zurück what does this mean? Greeting and thanks for your help! Birgit Am 28.06.2007 um 12:05 schrieb Jacques VESLOT: sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] - --- __ 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. Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Hi I think a for loop would be more what you want. Something along the lines of: V-list(a=c(1,2,3), b=c(2,3,4)) # list of 2 vectors for ( i in 1:2 ) { # 2 vectors (replace with 85 ...) print(range (V[i], na.rm = TRUE)) } Regards JS --- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Birgit Lemcke Sent: 28 June 2007 10:48 To: R Hilfe Subject: [R] Repeat if Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } __ 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.
Re: [R] Repeat if
Your V1 to V85 are probably coming from a data.frame, aren't they? If yes, and if this data.frame is named 'a', you can use 'sapply(a,range)' Otherwise, see ?get (get(paste(V,1,sep=)) returns V1) Christophe On 6/28/07, Birgit Lemcke [EMAIL PROTECTED] wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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. -- Christophe Pallier (http://www.pallier.org) [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
you may have a vector with only NA values in it... max(c(NA,NA), na.rm=T) [1] -Inf Warning message: aucun argument pour max ; -Inf est renvoyé Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Thanks that was really a quick answer. It works but I get this warning message anyway: 1: kein nicht-fehlendes Argument für min; gebe Inf zurück (None not-lacking argument for min; give Inf back) 2: kein nicht-fehlendes Argument für max; gebe -Inf zurück what does this mean? Greeting and thanks for your help! Birgit Am 28.06.2007 um 12:05 schrieb Jacques VESLOT: sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailto: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. Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] __ 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.
Re: [R] Repeat if
Birgit Lemcke wrote: Thanks that was really a quick answer. It works but I get this warning message anyway: 1: kein nicht-fehlendes Argument f�r min; gebe Inf zur�ck (None not- lacking argument for min; give Inf back) 2: kein nicht-fehlendes Argument f�r max; gebe -Inf zur�ck what does this mean? Same as this max(c(NA, NA), na.rm=T) [1] -Inf Warning message: no non-missing arguments to max; returning -Inf which is related to the issues of empty sum(), prod(), any(), and all() in that it allows a consistent concatenation rule: max(c(x1,x2)) == max(max(x1), max(x2)) -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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.
Re: [R] Repeat if
Thats not the case. I have two vectosr with a lot of NAs, but not only NAs. But nevertheless as i saw the results are accurate. Birgit Am 28.06.2007 um 15:17 schrieb Jacques VESLOT: you may have a vector with only NA values in it... max(c(NA,NA), na.rm=T) [1] -Inf Warning message: aucun argument pour max ; -Inf est renvoyé Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Thanks that was really a quick answer. It works but I get this warning message anyway: 1: kein nicht-fehlendes Argument für min; gebe Inf zurück (None not-lacking argument for min; give Inf back) 2: kein nicht-fehlendes Argument für max; gebe -Inf zurück what does this mean? Greeting and thanks for your help! Birgit Am 28.06.2007 um 12:05 schrieb Jacques VESLOT: sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] [[alternative HTML version deleted]] --- - __ R-help@stat.math.ethz.ch mailto: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. Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Hello John, I tried this code. But I got only the ranges of V1 and V2 what is easily understandable. Do I have to write in all 85 vectors in the first line? V-list(a=c(V1), b=c(V2)) for ( i in 1:85 ) { # 2 vectors (replace with 85 ...) + print(range (V[i], na.rm = TRUE)) + } sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= But thanks anyway. Greetings Birgit Am 28.06.2007 um 12:23 schrieb john seers ((IFR)): Hi I think a for loop would be more what you want. Something along the lines of: V-list(a=c(1,2,3), b=c(2,3,4)) # list of 2 vectors for ( i in 1:2 ) { # 2 vectors (replace with 85 ...) print(range (V[i], na.rm = TRUE)) } Regards JS --- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Birgit Lemcke Sent: 28 June 2007 10:48 To: R Hilfe Subject: [R] Repeat if Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Thanks Christophe, I received already an answer with a similar suggestion. But thanks for your answer. Birgit Am 28.06.2007 um 12:34 schrieb Christophe Pallier: Your V1 to V85 are probably coming from a data.frame, aren't they? If yes, and if this data.frame is named 'a', you can use 'sapply (a,range)' Otherwise, see ?get (get(paste(V,1,sep=)) returns V1) Christophe On 6/28/07, Birgit Lemcke [EMAIL PROTECTED] wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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. -- Christophe Pallier (http://www.pallier.org) Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Hello Patrick, this does not work and gives following warning message: for(i in 1:85) range(get(paste(V, i, sep=)), na.rm=TRUE) Warning messages: 1: kein nicht-fehlendes Argument für min; gebe Inf zurück 2: kein nicht-fehlendes Argument für max; gebe -Inf zurück This works but also with the warning message: Vranges - array(NA, c(85, 2)) for(i in 1:85) Vranges[i,] - range(get(paste(V, i, sep=)), na.rm=TRUE) Greetings Birgit Am 28.06.2007 um 12:27 schrieb Patrick Burns: In your code the 'range' call is only seen when i is 85. I think the following is what you want: for(i in 1:85) range(get(paste(V, i, sep=)), na.rm=TRUE) Except that nothing is done with the results of 'range'. More likely is: Vranges - array(NA, c(85, 2)) for(i in 1:85) Vranges[i,] - range(get(paste(V, i, sep=)), na.rm=TRUE) See S Poetry for details. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) Birgit Lemcke wrote: Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] - --- __ 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. Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Hi Birgit No, you do not have to write all 85 vectors in the first line. I just did not fully appreciate what you were trying to do. You could use the get option as was suggested somewhere else. So, if your vectors are V1 to V2 (i.e. 85) say, something like: V1-c(1,2,3) V2-c(5,2,7) ... V-paste(V, 1:2, sep=) for ( i in 1:length(V) ) { print(range (get(V[i]), na.rm = TRUE)) } Regards JS --- Web sites: www.ifr.ac.uk www.foodandhealthnetwork.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Birgit Lemcke Sent: 28 June 2007 15:12 To: john seers (IFR) Cc: R Hilfe Subject: Re: [R] Repeat if Hello John, I tried this code. But I got only the ranges of V1 and V2 what is easily understandable. Do I have to write in all 85 vectors in the first line? V-list(a=c(V1), b=c(V2)) for ( i in 1:85 ) { # 2 vectors (replace with 85 ...) + print(range (V[i], na.rm = TRUE)) + } sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= But thanks anyway. Greetings Birgit Am 28.06.2007 um 12:23 schrieb john seers ((IFR)): Hi I think a for loop would be more what you want. Something along the lines of: V-list(a=c(1,2,3), b=c(2,3,4)) # list of 2 vectors for ( i in 1:2 ) { # 2 vectors (replace with 85 ...) print(range (V[i], na.rm = TRUE)) } Regards JS --- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Birgit Lemcke Sent: 28 June 2007 10:48 To: R Hilfe Subject: [R] Repeat if Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.
Re: [R] Repeat if
Sorry Jacques and all the other helpful people! I made a mistake because I didn´t realize that I have a vector only containing NAs. That happened during standardization and i didn´t check this. I apologize for that. Greetings Birgit Am 28.06.2007 um 15:17 schrieb Jacques VESLOT: you may have a vector with only NA values in it... max(c(NA,NA), na.rm=T) [1] -Inf Warning message: aucun argument pour max ; -Inf est renvoyé Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Thanks that was really a quick answer. It works but I get this warning message anyway: 1: kein nicht-fehlendes Argument für min; gebe Inf zurück (None not-lacking argument for min; give Inf back) 2: kein nicht-fehlendes Argument für max; gebe -Inf zurück what does this mean? Greeting and thanks for your help! Birgit Am 28.06.2007 um 12:05 schrieb Jacques VESLOT: sapply(1:85, function(i) eval(parse(text=paste(range(V, i, , na.rm=T), sep= Jacques VESLOT INRA - Biostatistique Processus Spatiaux Site Agroparc 84914 Avignon Cedex 9, France Tel: +33 (0) 4 32 72 21 58 Fax: +33 (0) 4 32 72 21 84 Birgit Lemcke a écrit : Hello, (Power Book G4, Mac OS X, R 2.5.0) I would like to repeat the function range for 85 Vectors (V1-V85). I tried with this code: i-0 repeat { + i-i+1 + if (i85) next + range (Vi, na.rm = TRUE) + if (i==85) break + } I presume that the Vi is wrong, because in this syntax i is not known as a variable. But I don´t know how to say that it is a variable here. Would be nice if somebody could help me. Perhaps I´m thinking too complicated and there is an easier way to do this. Thanks in advance Greetings Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] [[alternative HTML version deleted]] --- - __ R-help@stat.math.ethz.ch mailto: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. Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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] Repeat dataframe
Hi R list: How can I repeat a data frame n times (with n1000), and obtain a new data frame where all the n data frames are binded by rows? Thank you for your help Kenneth -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ __ 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
Re: [R] Repeat dataframe
Maybe the function merge is what you need. Xiaohua On 5/1/06, Kenneth Cabrera [EMAIL PROTECTED] wrote: Hi R list: How can I repeat a data frame n times (with n1000), and obtain a new data frame where all the n data frames are binded by rows? Thank you for your help Kenneth -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ __ 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 [[alternative HTML version deleted]] __ 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
Re: [R] Repeat dataframe
Sorry, I give a wrong answer. X On 5/1/06, Xiaohua Dai [EMAIL PROTECTED] wrote: Maybe the function merge is what you need. Xiaohua On 5/1/06, Kenneth Cabrera [EMAIL PROTECTED] wrote: Hi R list: How can I repeat a data frame n times (with n1000), and obtain a new data frame where all the n data frames are binded by rows? Thank you for your help Kenneth -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ __ 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.htmlhttp://www.r-project.org/posting-guide.html [[alternative HTML version deleted]] __ 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
Re: [R] Repeat dataframe
On Mon, 1 May 2006, Kenneth Cabrera wrote: Hi R list: How can I repeat a data frame n times (with n1000), and obtain a new data frame where all the n data frames are binded by rows? Perhaps my_df[rep(1:nrow(my_df), times=n), ] is what you want? -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] Repeat dataframe
Well, I find a solution! If DFe is a data frame and n is an integer then DFr-data.frame(t(matrix(rep(t(DFe),n),dim(DFe)[2],dim(DFe)[1]*n))) names(DFr)-names(DFe) Will work!! Maybe somebody has a more elegant solution. Again, thank you for your help. On Mon, 01 May 2006 11:23:14 -0500, Kenneth Cabrera [EMAIL PROTECTED] wrote: Hi R list: How can I repeat a data frame n times (with n1000), and obtain a new data frame where all the n data frames are binded by rows? Thank you for your help Kenneth -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ __ 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
Re: [R] Repeat dataframe
As I said, a very more elegant solution! Thank you! On Mon, 01 May 2006 11:52:44 -0500, Prof Brian Ripley [EMAIL PROTECTED] wrote: my_df[rep(1:nrow(my_df), times=n), ] -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ __ 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
Re: [R] repeat { readline() }
--- Henrik Bengtsson [EMAIL PROTECTED] a écrit : Hi. Using Rterm v2.2.1 on WinXP, is there a way to interrupt a call like repeat { readline() } without killing the Command window? Ctrl+C is not interrupting the loop: To interupt the loop add and condition for a break like this : if () break R : Copyright 2006, The R Foundation for Statistical Computing Version 2.2.1 Patched (2006-01-01 r36947) snip/snip repeat { readline() } ^C ^C ^C ^C On Unix it works. The problem seems to get the interrupt signal to occur outside the readline() call so that repeat is interrupted. Doing repeat { readline(); Sys.sleep(3) } and it is likely that can generate an interrupt signal outside readline(). It seem like readline()/readLines(n=1) or an underlying method catches the interrupt signal quietly and just waits for a symbol to come through. Try readline() by itself and press Ctrl+C. Maybe this is a property of the Windows Command terminal, I don't know, but is it a wanted feature and are R core aware of it? Note that, in Rgui it is possible to interrupting such a loop by pressing ESC. Cheers Henrik __ 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 __ 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
Re: [R] repeat { readline() }
Use tryCatch; try behaves the way it does with respect to interrupts for historical compatibility. luke On Sun, 8 Jan 2006, hadley wickham wrote: On a related note, does anyone know how to exit: repeat { try( readline() ) } The try block captures Ctrl-C. Hadley __ 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 -- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics andFax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: [EMAIL PROTECTED] Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu __ 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
Re: [R] repeat { readline() }
Ctrl-Break works: see the rw-FAQ and README.rterm. (You'll need a return to see a new prompt.) It is related to your reading directly from the console, so Ctrl-C is getting sent to the wrong place, I believe. (There's a comment from Guido somewhere in the sources about this, and this seems corroborated by the fact that Ctrl-C will interrupt under Rterm --ess.) On Sun, 8 Jan 2006, Henrik Bengtsson wrote: Hi. Using Rterm v2.2.1 on WinXP, is there a way to interrupt a call like repeat { readline() } without killing the Command window? Ctrl+C is not interrupting the loop: R : Copyright 2006, The R Foundation for Statistical Computing Version 2.2.1 Patched (2006-01-01 r36947) snip/snip repeat { readline() } ^C ^C ^C ^C On Unix it works. The problem seems to get the interrupt signal to occur outside the readline() call so that repeat is interrupted. Doing repeat { readline(); Sys.sleep(3) } and it is likely that can generate an interrupt signal outside readline(). It seem like readline()/readLines(n=1) or an underlying method catches the interrupt signal quietly and just waits for a symbol to come through. Try readline() by itself and press Ctrl+C. Maybe this is a property of the Windows Command terminal, I don't know, but is it a wanted feature and are R core aware of it? Note that, in Rgui it is possible to interrupting such a loop by pressing ESC. Cheers Henrik __ 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] repeat { readline() }
On a related note, does anyone know how to exit: repeat { try( readline() ) } The try block captures Ctrl-C. Hadley __ 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
Re: [R] repeat { readline() }
On Sun, 8 Jan 2006, Prof Brian Ripley wrote: Ctrl-Break works: see the rw-FAQ and README.rterm. (You'll need a return to see a new prompt.) It is related to your reading directly from the console, so Ctrl-C is getting sent to the wrong place, I believe. (There's a comment from Guido somewhere in the sources about this, and this seems corroborated by the fact that Ctrl-C will interrupt under Rterm --ess.) Although the comment is there (in psignal.c), on closer examination the cause is a change Guido made to getline.c, so Ctrl-C is treated as a character during keyboard input. I doubt if that was intentional (0 is not the default state) and I have changed it for R-devel.\ On Sun, 8 Jan 2006, Henrik Bengtsson wrote: Hi. Using Rterm v2.2.1 on WinXP, is there a way to interrupt a call like repeat { readline() } without killing the Command window? Ctrl+C is not interrupting the loop: R : Copyright 2006, The R Foundation for Statistical Computing Version 2.2.1 Patched (2006-01-01 r36947) snip/snip repeat { readline() } ^C ^C ^C ^C On Unix it works. The problem seems to get the interrupt signal to occur outside the readline() call so that repeat is interrupted. Doing repeat { readline(); Sys.sleep(3) } and it is likely that can generate an interrupt signal outside readline(). It seem like readline()/readLines(n=1) or an underlying method catches the interrupt signal quietly and just waits for a symbol to come through. Try readline() by itself and press Ctrl+C. Maybe this is a property of the Windows Command terminal, I don't know, but is it a wanted feature and are R core aware of it? Note that, in Rgui it is possible to interrupting such a loop by pressing ESC. Cheers Henrik __ 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] repeat { readline() }
Prof Brian Ripley wrote: On Sun, 8 Jan 2006, Prof Brian Ripley wrote: Ctrl-Break works: see the rw-FAQ and README.rterm. (You'll need a return to see a new prompt.) It is related to your reading directly from the console, so Ctrl-C is getting sent to the wrong place, I believe. (There's a comment from Guido somewhere in the sources about this, and this seems corroborated by the fact that Ctrl-C will interrupt under Rterm --ess.) Although the comment is there (in psignal.c), on closer examination the cause is a change Guido made to getline.c, so Ctrl-C is treated as a character during keyboard input. I doubt if that was intentional (0 is not the default state) and I have changed it for R-devel.\ Thank you very much this. Henrik On Sun, 8 Jan 2006, Henrik Bengtsson wrote: Hi. Using Rterm v2.2.1 on WinXP, is there a way to interrupt a call like repeat { readline() } without killing the Command window? Ctrl+C is not interrupting the loop: R : Copyright 2006, The R Foundation for Statistical Computing Version 2.2.1 Patched (2006-01-01 r36947) snip/snip repeat { readline() } ^C ^C ^C ^C On Unix it works. The problem seems to get the interrupt signal to occur outside the readline() call so that repeat is interrupted. Doing repeat { readline(); Sys.sleep(3) } and it is likely that can generate an interrupt signal outside readline(). It seem like readline()/readLines(n=1) or an underlying method catches the interrupt signal quietly and just waits for a symbol to come through. Try readline() by itself and press Ctrl+C. Maybe this is a property of the Windows Command terminal, I don't know, but is it a wanted feature and are R core aware of it? Note that, in Rgui it is possible to interrupting such a loop by pressing ESC. Cheers Henrik __ 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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 __ 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
[R] repeat { readline() }
Hi. Using Rterm v2.2.1 on WinXP, is there a way to interrupt a call like repeat { readline() } without killing the Command window? Ctrl+C is not interrupting the loop: R : Copyright 2006, The R Foundation for Statistical Computing Version 2.2.1 Patched (2006-01-01 r36947) snip/snip repeat { readline() } ^C ^C ^C ^C On Unix it works. The problem seems to get the interrupt signal to occur outside the readline() call so that repeat is interrupted. Doing repeat { readline(); Sys.sleep(3) } and it is likely that can generate an interrupt signal outside readline(). It seem like readline()/readLines(n=1) or an underlying method catches the interrupt signal quietly and just waits for a symbol to come through. Try readline() by itself and press Ctrl+C. Maybe this is a property of the Windows Command terminal, I don't know, but is it a wanted feature and are R core aware of it? Note that, in Rgui it is possible to interrupting such a loop by pressing ESC. Cheers Henrik __ 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
[R] R: repeat loops
To whoever this may concern I am trying to write a repeat loop and can't make out from the documentation on the website how exactly to construct the repeat, break structure of the loop. Below is the function sim2.dat that I am trying to create, in which firstly I create n random uniform(0,1) numbers. From there I assign values to k, P and F. From then on for each random number u[j] of u, I try to set up a repeat loop so that if u[j]=F I alter P, k, and F and then compare u[j] to F again. I want to stop the loop when u[j]F and then assign the present value of k to u[j]. However my effort below seems to fail! Any help would be much appreciated, either with respect to repeat loops or better ways to conquer my problem. Thank you for your time, Regards, Michael Gray R text: sim2.dat-function(n,theta){ + u-runif(n) + k-1 + P--(1-theta)/log(theta) + F-P + for(j in 1:n){repeat{if(u[j]=F){P-k*(1-theta)*P/(k+1) + k-k+1 + F-F+P}} + if(u[j]F){break( + repeat{if(u[j]=F){P-k*(1-theta)*P/(k+1) + k-k+1 + F-F+P}}) + u[j]-k}} + u} __ 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
Re: [R] R: repeat loops
The mistakes in your function are: 1. the functions repeat and break do not have any arguments 2. the break function should be used insed the repear loop (or if statment with break function should be inside the repeat loop) Correcting for these mistakes I corrected your function. It is at the end of my reply. I also belive that it might be better to use while function. Look at ?Control (if you haven't already) I hope this helps! Ale sim2.dat-function(n,theta){ u-runif(n) k-1 P--(1-theta)/log(theta) F-P for(j in 1:n){ repeat { P-k*(1-theta)*P/(k+1) k-k+1 F-F+P if(u[j]F){break} } u[j]-k } u } - Original Message - From: Michael Gray [EMAIL PROTECTED] To: R-help@stat.math.ethz.ch Sent: Tuesday, December 28, 2004 12:47 PM Subject: [R] R: repeat loops To whoever this may concern I am trying to write a repeat loop and can't make out from the documentation on the website how exactly to construct the repeat, break structure of the loop. Below is the function sim2.dat that I am trying to create, in which firstly I create n random uniform(0,1) numbers. From there I assign values to k, P and F. From then on for each random number u[j] of u, I try to set up a repeat loop so that if u[j]=F I alter P, k, and F and then compare u[j] to F again. I want to stop the loop when u[j]F and then assign the present value of k to u[j]. However my effort below seems to fail! Any help would be much appreciated, either with respect to repeat loops or better ways to conquer my problem. Thank you for your time, Regards, Michael Gray R text: sim2.dat-function(n,theta){ + u-runif(n) + k-1 + P--(1-theta)/log(theta) + F-P + for(j in 1:n){repeat{if(u[j]=F){P-k*(1-theta)*P/(k+1) + k-k+1 + F-F+P}} + if(u[j]F){break( + repeat{if(u[j]=F){P-k*(1-theta)*P/(k+1) + k-k+1 + F-F+P}}) + u[j]-k}} + u} __ 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 __ 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
[R] repeat until function
Hi, I'm in this situation: I what to generate N random numbers(integer) that are different from each other. One suggestion: tabel - rep(NULL, N) for (i in 1:N){ temp - as.integer(runif(1,1,max)) if(temp in tabel) { repeat (?) (temp - as.integer(runif(i,i,max))) until (?) ((temp in tabel) ==FALSE) } else{ tabel[i] - temp} I can't use repeat/until - don't exist Any suggestions? Thanks *Ragnhild* _ MSN Messenger http://www.msn.no/messenger Den korteste veien mellom deg og dine venner __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] repeat until function
I what to generate N random numbers(integer) that are different from each other. One suggestion: tabel - rep(NULL, N) for (i in 1:N){ temp - as.integer(runif(1,1,max)) if(temp in tabel) { repeat (?) (temp - as.integer(runif(i,i,max))) until (?) ((temp in tabel) ==FALSE) } else{ tabel[i] - temp} I can't use repeat/until - don't exist repeat ... until loops can be rewritten as while loops. The easiest way is this: # n unique random numbers between min and max sample(min:max, n) But maybe you don't want to generate the array min:max for some reason (because it is really large?). Then you can do the whole thing yourself: # another way to the same result left - n tab - NULL while (left 0) { new - as.integer(runif(left, min, max)) tab - unique(c(tab, new)) left - n - length(tab) } If max-min is really large and n is not much smaller than max-min you may want to generate more numbers than you actually need so unique() leaves you with more results and you don't have to iterate so often... cu Philipp -- Dr. Philipp PagelTel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] repeat until function
As others already pointed out, the fast way is to use sample(). What I'd like to add is the following, which I learned from peeking at the C code underneath sample(): To draw n samples without replacement from 1:N (N=n), you only need a loop from 1 to n that used up n random numbers. The algorithm is simple, but IMHO very clever: pop - 1:N samp - rep(NA, n) soFar - N for (i in 1:n) { idx - ceiling(runif(1) * soFar) samp[i] - pop[idx] ## swap the soFar-th and idx-th element of pop. tmp - pop[soFar] pop[soFar] - pop[idx] pop[idx] - tmp ## decrease the population by 1. soFar - soFar - 1 } This is obviously not efficient in high-level languages like R, but in terms of algorithm, it is a lot more efficient than check-and-reject. IMHO this should be described in some book, but I have not seen any book describing it. Just my $0.02... Andy -Original Message- From: Ragnhild Sørum [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 12, 2003 9:26 AM To: [EMAIL PROTECTED] Subject: [R] repeat until function Hi, I'm in this situation: I what to generate N random numbers(integer) that are different from each other. One suggestion: tabel - rep(NULL, N) for (i in 1:N){ temp - as.integer(runif(1,1,max)) if(temp in tabel) { repeat (?) (temp - as.integer(runif(i,i,max))) until (?) ((temp in tabel) ==FALSE) } else{ tabel[i] - temp} I can't use repeat/until - don't exist Any suggestions? Thanks *Ragnhild* _ MSN Messenger http://www.msn.no/messenger Den korteste veien mellom deg og dine venner __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo /r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] repeat until function
On Wed, 12 Nov 2003, Liaw, Andy wrote: As others already pointed out, the fast way is to use sample(). What I'd like to add is the following, which I learned from peeking at the C code underneath sample(): To draw n samples without replacement from 1:N (N=n), you only need a loop from 1 to n that used up n random numbers. The algorithm is simple, but IMHO very clever: pop - 1:N samp - rep(NA, n) soFar - N for (i in 1:n) { idx - ceiling(runif(1) * soFar) samp[i] - pop[idx] ## swap the soFar-th and idx-th element of pop. tmp - pop[soFar] pop[soFar] - pop[idx] pop[idx] - tmp ## decrease the population by 1. soFar - soFar - 1 } This is obviously not efficient in high-level languages like R, but in terms of algorithm, it is a lot more efficient than check-and-reject. IMHO this should be described in some book, but I have not seen any book describing it. Ripley (1987) Stochastic Simulation, pp.80-1 for one. I am pretty sure it is Knuth's book, although I don't have that to hand. I attribute it to Moses Oakford (1963). -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] repeat until function
Liaw, Andy [EMAIL PROTECTED] writes: This is obviously not efficient in high-level languages like R, but in terms of algorithm, it is a lot more efficient than check-and-reject. IMHO this should be described in some book, but I have not seen any book describing it. I'm pretty sure I have - way back. Can't recall exactly where though. Either in a CS book or as an exercise in a stats book (proving that it actually works). -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] repeat until function
On Wed, 12 Nov 2003 12:00:06 -0500, Liaw, Andy [EMAIL PROTECTED] wrote : This is obviously not efficient in high-level languages like R, but in terms of algorithm, it is a lot more efficient than check-and-reject. IMHO this should be described in some book, but I have not seen any book describing it. That's a variation on the standard shuffling algorithm, which is described in Knuth (but is not original to him). What you described takes time n but uses storage N. There's also a very similar variation that takes time N and storage n; this is equivalent to the McLeod and Bellhouse (1983) sampling algorithm. Duncan murdoch __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] repeat until function
From: Prof Brian Ripley [mailto:[EMAIL PROTECTED] Ripley (1987) Stochastic Simulation, pp.80-1 for one. I am pretty sure it is Knuth's book, although I don't have that to hand. I attribute it to Moses Oakford (1963). Thanks to Brian, Peter and Duncan for the info. And I have both Ripley (1987) and Knuth (1981), just not smart enough to look there... The shuffling algorithm is described on pp. 139-140 in Knuth's book (vol. 2, 2nd ed.), and in addition to Moses Oakford, he also referenced R. Durstenfeld, CACM 7 (1964), 420. Best, Andy -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help