Ooops! Sorry, I send it only to Uwe Ligges the first time. Best, Philippe Grosjean This is perhaps another solution, more elegant in the way the block comment is written... but it requires to redefine `!` and slows it a little bit because it tests first its arguments before calling .Primitive(!): It takes advantage of `!` being not defined for character arguments:> !2[1] FALSE> !"some text"Error in !"some text" : invalid argument type So, now, we will define it for character arguments (invisibly returns the argument)> `!`<- function(x)+ if (inherits(x, "character") == FALSE) + .Primitive("!")(x) else invisible(x) Now, `!` can be used to construct a block comment: ==== A R script with block comments ==== 1+1 # This is a line comment !' This is a block comment spread on several lines... ' ls() !!' This is another block comment, possibly of higher importance than the previous one ' search() !!!' For color syntax highlighting and to better detect the end of block comments, one may also decide to use the same code for opening and closing the comments like it is the present case !!!' !' Note that the only constraint is to escape single quotes in block comments (or not to use single quotes) Of course, one could also decide to use double quotes instead of single quotes !' !' Now, it would be nice to have a little patch of .Primitive(!) that simply displays no error message in case the argument of `!`is a character sting. So, the hock would not be required any more !' ==== And of the R script ==== Best, Philippe Grosjean Uwe Ligges wrote:> > Robin Hankin wrote: >> On 5 Oct 2006, at 10:05, Uwe Ligges wrote: >> >>> >>> Wee-Jin Goh wrote: >>>> Hello list, >>>> >>>> Is there any way to perform a block comment in R? In C++, anything in >>>> between a /* and */ is considered a comment, and it allows >>>> programmers to comment out chunks of code for testing and debugging. >>>> Is there such a feature in R? >>> >>> This has frequently been asked on the list. Try to google for it. You >>> will find answers like >>> >>> if(FALSE){" >>> code block >>> commented >>> "} >>> >>> >> >> That method doesn't work for me: >> >> if(FALSE){" >> >> if(1<2) >> print("1<2") >> else >> print("1>2") >> >> "} > > > Use an editor that comments out a whole block which is what I do all the > time, e.g. use Tinn-R, Emacs or WinEdt, to mention just a few of them. > > Or you can go ahead and use > > if(FALSE){ > > if(1<2) > print("1<2") > else > print("1>2") > > } > > > or > > if(FALSE){' > > if(1<2) > print("1<2") > else > print("1>2") > > '} > > > Uwe > > >> returns an error. How would I comment out that block of (incorrect) code? >> >> >> >> >> >> >> >>> or "use a good editor that supports commenting and uncommenting blocks". >>> >>> >>> Uwe Ligges >>> >>> >> -- >> Robin Hankin >> Uncertainty Analyst >> National Oceanography Centre, Southampton >> European Way, Southampton SO14 3ZH, UK >> tel 023-8059-7743 >> >> >> >> > > ______________________________________________ > R-help at 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. > >
Is there any function that divides a sample into N quantiles? For example, for N = 2, this would be the solution: x <- rnorm(100) m <- median(x) q <- ifelse(x <= median, 1, 2) Alberto Monteiro
You might also want to look at the function quantcut in the gtools package (part of the gregmisc bundle). On 05/10/06, Alberto Monteiro <albmont at centroin.com.br> wrote:> Is there any function that divides a sample into N quantiles? > > For example, for N = 2, this would be the solution: > > x <- rnorm(100) > m <- median(x) > q <- ifelse(x <= median, 1, 2) > > Alberto Monteiro > > ______________________________________________ > R-help at 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Philippe Grosjean wrote:> > It takes advantage of `!` being not defined for character arguments: >*gasp* how much R code is destined to feature on www.thedailywtf.com in the future? whats the chances of block commenting being included in a future version? and more generally, is there a feature request system, or discussion of whats going in new releases? the nearest I can find is the 'ideas' file: http://developer.r-project.org/ideas.txt which has a bit of a 1990's feel to it (okay, it is in the 'Older Material' section). Barry
David Barron wrote:> You might also want to look at the function quantcut in the gtools > package (part of the gregmisc bundle).Also, cut2 in Hmisc will do this and will label the intervals compactly. Frank> > > > On 05/10/06, Alberto Monteiro <albmont at centroin.com.br> wrote: >> Is there any function that divides a sample into N quantiles? >> >> For example, for N = 2, this would be the solution: >> >> x <- rnorm(100) >> m <- median(x) >> q <- ifelse(x <= median, 1, 2) >> >> Alberto Monteiro >> >> ______________________________________________ >> R-help at 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. >> > >-- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Sorry, folks. Thanks for the help, but none of the suggestions worked quite as I wanted :-/ So I wrote the code. It seems to work: gera_particao <- function(x, n) { y <- sort(x) icut <- as.integer(seq(1, length(x)+1, length = n + 1)) icut <- icut[c(-(n+1))] ycut <- y[icut] for (i in 1:length(x)) xpart[i] <- sum(x[i] >= ycut) return(xpart) } First, x is sorted to y. Then, I select the minima y of each of the n segments. Then, an ugly and slow loop, counts for each x how many ycut lie below them.> x <- runif(12) > x[1] 0.2971455266 0.6112766485 0.5571073645 0.5886481798 0.7499023860 [6] 0.1681732289 0.6319822536 0.0005354732 0.8055324992 0.8841625380 [11] 0.0726578285 0.6250309648> gera_particao(x, 2)[1] 1 2 1 1 2 1 2 1 2 2 1 2> gera_particao(x, 3)[1] 1 2 2 2 3 1 3 1 3 3 1 2> gera_particao(x, 4)[1] 2 3 2 2 4 1 3 1 4 4 1 3> gera_particao(x, 12)[1] 4 7 5 6 10 3 9 1 11 12 2 8 Alberto Monteiro