|
View:
New views
13 Messages
—
Rating Filter:
Alert me
|
|
|
Random number generationColleagues,
I have encountered behavior of random number generation that eludes me. I generate a random integer in a particular range using the following code: sample(1000:9999, size=1) This code exists within a script that starts with the command: remove(list=ls()) Each time that I run the script, it yields the same random number: 6420. I thought that the problem might result from deleting the random seed. However, list=ls() does not include .RandomSeed. To my surprise, I can't replicate the problem with a 2-line script: remove(list-ls()) sample(1000:9999, size=1) Also, the same problem occurs if I use runif instead of sample. Thoughts? Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]] ______________________________________________ R-help@... 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: Random number generationDennis,
I assume that there is a set.seed() somewhere in your script, possibly in something you source()d (hopefully not in anything library()d). Have you tried successively removing/commenting parts of the script before the sample() command until the problem goes away? That way you should be able to pinpoint the offending script command. Good luck, Stephan Dennis Fisher schrieb: > Colleagues, > > I have encountered behavior of random number generation that eludes me. > > I generate a random integer in a particular range using the following > code: > sample(1000:9999, size=1) > > This code exists within a script that starts with the command: > remove(list=ls()) > > Each time that I run the script, it yields the same random number: > 6420. > > I thought that the problem might result from deleting the random > seed. However, list=ls() does not include .RandomSeed. > > To my surprise, I can't replicate the problem with a 2-line script: > remove(list-ls()) > sample(1000:9999, size=1) > > Also, the same problem occurs if I use runif instead of sample. > > Thoughts? > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@... 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@... 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: Random number generationStephan Kolassa wrote:
> > Have you tried successively removing/commenting parts of the script > before the sample() command until the problem goes away? That way you > should be able to pinpoint the offending script command. Hi, This brings up a question I have .. is there a way to do *block* comments with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment more than just one line at a time. From what I have read this is not possible in R (at least not easily), but I am eager for someone to contradict me :-) Thanks, Esmail ______________________________________________ R-help@... 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: Random number generationOn Sun, 11 May 2008, Esmail Bonakdarian wrote:
> Stephan Kolassa wrote: >> >> Have you tried successively removing/commenting parts of the script before >> the sample() command until the problem goes away? That way you should be >> able to pinpoint the offending script command. > > Hi, > > This brings up a question I have .. is there a way to do *block* comments > with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment > more than just one line at a time. > > From what I have read this is not possible in R (at least not easily), but > I am eager for someone to contradict me :-) if(FALSE) { ... } Any good editor can do block commenting, e.g. ESS. You didn't tell us what you read, but I have never seen this in a reputable source. > Thanks, > Esmail -- Brian D. Ripley, ripley@... 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, UK Fax: +44 1865 272595 ______________________________________________ R-help@... 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: Random number generationHello there,
Prof Brian Ripley wrote: > On Sun, 11 May 2008, Esmail Bonakdarian wrote: > >> Stephan Kolassa wrote: >>> >>> Have you tried successively removing/commenting parts of the script >>> before the sample() command until the problem goes away? That way you >>> should be able to pinpoint the offending script command. >> >> Hi, >> >> This brings up a question I have .. is there a way to do *block* comments >> with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment >> more than just one line at a time. >> >> From what I have read this is not possible in R (at least not easily), >> but >> I am eager for someone to contradict me :-) > > if(FALSE) { > ... > } > > Any good editor can do block commenting, e.g. ESS. > > You didn't tell us what you read, but I have never seen this in a > reputable source. I don't remember the source as I was reading widely all over the place trying to get up to speed with R in a hurry (having found this group was one of the best sources). What I read doesn't seem to be incorrect however (it may even have been an archived message here), the *language* itself does not seem to support block *comments*. Using conditional constructs, or an IDE/editor to achieve similar results is a work around - but not the same. I don't mean to nitpick, but as a computer scientist I see this as different :-) I'll have to look at ESS though. Thanks, Esmail ps: Ah, I seem Meta-; works as a toggle in emacs/ESS .. thanks for encouraging me to look at that some more. ______________________________________________ R-help@... 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: Random number generation> -----Original Message----- > From: r-help-bounces@... > [mailto:r-help-bounces@...] On Behalf Of Esmail Bonakdarian > Sent: Sunday, May 11, 2008 7:25 AM > To: Prof Brian Ripley > Cc: r-help@... > Subject: Re: [R] Random number generation [snip] > What I read doesn't seem to be incorrect however (it may even > have been an archived message here), the *language* itself > does not seem to support block *comments*. Using conditional > constructs, or an IDE/editor to achieve similar results is a > work around - but not the same. I don't mean to nitpick, but > as a computer scientist I see this as different :-) I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor replaces the comments with a single space and the compilor does not even see them. Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option. However as an additional work around you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow@... (801) 408-8111 ______________________________________________ R-help@... 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. |
|
|
|
|
|
add horizontal line "(ABLINE(V=))" to xyplot latticeHi,
In a lattice plot like this: win.graph() xyplot(tmx~frequ|as.factor(as.numeric(spf)),groups=as.factor(blm),data=tmx,type="l",pch=16,xlab="frequency (N)",ylab="Area held (ha)", auto.key=list(blm,points=F,lines=T,title="Blm factor",cex.title=0.7,cex=0.7,corner=c(1,1)),main="Mangroves target=1573ha",layout=c(2,3)) I would like to add a horizontal line to each one of the 5 graphs with a single value, v=1573 Any idea will be welcome Paulo ________________________________ De: r-help-bounces@... em nome de Jim Lemon Enviada: ter 13-05-2008 10:56 Para: r-help@... Assunto: Re: [R] Random number generation On Behalf Of Esmail Bonakdarian wrote: > ... > What I read doesn't seem to be incorrect however (it may even > have been an archived message here), the *language* itself > does not seem to support block *comments*. Using conditional > constructs, or an IDE/editor to achieve similar results is a > work around - but not the same. I don't mean to nitpick, but > as a computer scientist I see this as different :-) For those using NEdit, the R block comment macro: replace_in_selection("^","#","regex") and the corresponding R block uncomment macro: replace_in_selection("^#","","regex") are both trivial. Just highlight the block and detonate the macro. Jim ______________________________________________ R-help@... 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. [[alternative HTML version deleted]] ______________________________________________ R-help@... 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: Random number generationGreg Snow wrote:
>> -----Original Message----- >> From: r-help-bounces@... >> [mailto:r-help-bounces@...] On Behalf Of Esmail Bonakdarian >> Sent: Sunday, May 11, 2008 7:25 AM >> To: Prof Brian Ripley >> Cc: r-help@... >> Subject: Re: [R] Random number generation > > [snip] > >> What I read doesn't seem to be incorrect however (it may even >> have been an archived message here), the *language* itself >> does not seem to support block *comments*. Using conditional >> constructs, or an IDE/editor to achieve similar results is a >> work around - but not the same. I don't mean to nitpick, but >> as a computer scientist I see this as different :-) > > I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor Hi there, Yes, while that may be true, many other languages that don't use an explicit pre-processor mechanism (though internally will go through a similar process) do support block comments. It's a moot point anyway, since when you compile a C program the preprocessor also runs (just like the linker etc) unless you specifically ask for something differently via command line switches. As far as the programmer is concerned the semantics are those of a language that provides block comments with the simple use of /* */ , and not dependent on if-statements or editor/IDE tricks. > Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option. However as an additional work around My use then may be not typical since I am trying to write scripts in R and only use R directly to try out functions/language constructs. > you could always run your R scripts through the C preproccessor and > have it strip the block comments for you. Too much work, call me old school, but I like the computer do work for me, rather than the other way around :-) > Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. I couldn't possibly offer an opinion on that .. I'll happily defer to you and the other experts here for this. Cheers, Esmail ______________________________________________ R-help@... 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: Random number generationEsmail Bonakdarian:
>> you could always run your R scripts through the C preproccessor and > > have it strip the block comments for you. > > Too much work, call me old school, but I like the computer do work for me, > rather than the other way around Most editors (and every editor worth using) supports block-commenting via a keyboard shortcut. For instance, in KDE-based editors, such as Kwrite, Kate and RKward (the latter is a KDE-based GUI for R), you can just select the lines you want to comment and press 'Ctrl + D' to comment them, or 'Shift + Ctrl + D' to uncomment them. -- Karl Ove Hufthammer ______________________________________________ R-help@... 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: Random number generation> -----Original Message-----
> From: Esmail Bonakdarian [mailto:esmail.js@...] > Sent: Tuesday, May 13, 2008 8:13 AM > To: Greg Snow > Cc: Prof Brian Ripley; r-help@... > Subject: Re: [R] Random number generation > > Greg Snow wrote: > >> -----Original Message----- [snip] > > you could always run your R scripts through the C preproccessor and > > have it strip the block comments for you. > > Too much work, call me old school, but I like the computer do > work for me, rather than the other way around :-) So, have the computer do the work. Here is a simple function that tells the computer to do the work: cppsource <- function(file) { tmp <- paste(tempfile(), '.R', sep='') system(paste('cpp',file,tmp)) source(tmp) unlink(tmp) } Now, instead of doing source('myfile.R') do cppsource('myfile.R') and it will automatically do the preproccing to remove the c-style comments (assuming that cpp is on your path, etc.). If typing the extra 3 characters is too much work, then just name it something else. Or if you just want to strip the comments in R and don't want to use the c preprocessor then the function: cssource <- function(file) { tmp <- readLines(file) tmp2 <- paste(tmp, collapse='\n') tmp3 <- gsub('(?s)/\\*.*?\\*/','',tmp2,perl=TRUE) source(textConnection(tmp3)) } Will do that (it will not deal with nested comments or some other special cases, but both the above functions worked for my quick test case). > > > Given the complexity of implementing block commenting (even > deciding on the syntax) and the ease of current work arounds, > the cost benefit ratio probably puts this very near the > bottom of the priority list. > > I couldn't possibly offer an opinion on that .. I'll happily > defer to you and the other experts here for this. I am not really an expert on this. I just remember being bitten when writing C and trying to comment out a section of code that already had a comment in it. I would rather have the R core team spending their time on their current priorities than even get involved in a discussion of whether block comments should be able to be nested or not. You now have at least 4 possible work arounds, hopefully one of them is satifactory, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow@... (801) 408-8111 ______________________________________________ R-help@... 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: Random number generationOn 5/13/2008 1:38 PM, Greg Snow wrote:
>> -----Original Message----- >> From: Esmail Bonakdarian [mailto:esmail.js@...] >> Sent: Tuesday, May 13, 2008 8:13 AM >> To: Greg Snow >> Cc: Prof Brian Ripley; r-help@... >> Subject: Re: [R] Random number generation >> >> Greg Snow wrote: >> >> -----Original Message----- > [snip] > >> > you could always run your R scripts through the C preproccessor and >> > have it strip the block comments for you. >> >> Too much work, call me old school, but I like the computer do >> work for me, rather than the other way around :-) > > So, have the computer do the work. Here is a simple function that tells the computer to do the work: > > cppsource <- function(file) { > tmp <- paste(tempfile(), '.R', sep='') > system(paste('cpp',file,tmp)) > source(tmp) > unlink(tmp) > } > > Now, instead of doing source('myfile.R') do cppsource('myfile.R') and it will automatically do the preproccing to remove the c-style comments (assuming that cpp is on your path, etc.). If typing the extra 3 characters is too much work, then just name it something else. > > Or if you just want to strip the comments in R and don't want to use the c preprocessor then the function: > > cssource <- function(file) { > tmp <- readLines(file) > tmp2 <- paste(tmp, collapse='\n') > tmp3 <- gsub('(?s)/\\*.*?\\*/','',tmp2,perl=TRUE) > source(textConnection(tmp3)) > } > > Will do that (it will not deal with nested comments or some other special cases, but both the above functions worked for my quick test case). One problem with this idea is that it makes the very helpful syntax error messages from source() less useful: > cppsource('test.R') Error in source(tmp) : c:\temp\Rtmpmyoc5a\file678418be.R: unexpected symbol at 8: 9: if (y > 1:2 cat Duncan Murdoch >> > Given the complexity of implementing block commenting (even >> deciding on the syntax) and the ease of current work arounds, >> the cost benefit ratio probably puts this very near the >> bottom of the priority list. >> >> I couldn't possibly offer an opinion on that .. I'll happily >> defer to you and the other experts here for this. > > I am not really an expert on this. I just remember being bitten when writing C and trying to comment out a section of code that already had a comment in it. I would rather have the R core team spending their time on their current priorities than even get involved in a discussion of whether block comments should be able to be nested or not. > > You now have at least 4 possible work arounds, hopefully one of them is satifactory, > > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow@... > (801) 408-8111 > > ______________________________________________ > R-help@... 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@... 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. |
| Free Forum Powered by Nabble | Forum Help |