Re: [R] X11 fonts and Ubuntu
Hi, I have observed this as well (Debian etch, R 2.4.0), with "small" cex values when drawing text. My LANG environment variable was set to "en_NZ.UTF-8"; setting this to "C" or "en_NZ" seems to prevent the error occuring. I presume my installation is missing a few fonts, yours may be too. HTH, Rich Setting the environment variable LANG=C before running R seems to work as a workaround (normally this is en_NZ.UTF-8 On 12/17/06, Patrick Giraudoux <[EMAIL PROTECTED]> wrote: > Hi, > > I am moving from Windows XP to Ubuntu 6.10 and installed R 2.4.0. When I > run eg plot.lm (things work fine with plot.default - eg > plot(rnorm(30),rnorm(30))) > > plot(lmobject) > > I can get the first plot and then this message: > > Hit to see next plot: > Error in text.default(x, y, labels.id[ind],cex=cex, xpd=TRUE, : > could not find any X11 fonts > Check that the Font Path is correct > > I have googled through the R-help list and it seems that such troubles > already occured sometimes (see link below and threads) > > http://lists.freebsd.org/pipermail/freebsd-ports/2005-June/024091.html > http://tolstoy.newcastle.edu.au/R/help/06/03/23864.html > > but did not find solution. Some messages claim it is a X11 problem (not > R) some others suggest it may come from R. Some also mention that UTF-8 > may be a problem (though I don't have specific message on this from R). > I have re-installed x11-common via the Synaptic package manager (so I > suppose X11 is well installed) without improvement. I have checked > /etc/X11/ xorg.conf > > Section "Files" > FontPath "/usr/share/X11/fonts/misc" > FontPath "/usr/share/X11/fonts/100dpi/:unscaled" > FontPath "/usr/share/X11/fonts/75dpi/:unscaled" > FontPath "/usr/share/X11/fonts/Type1" > FontPath "/usr/share/X11/fonts/100dpi" > FontPath "/usr/share/X11/fonts/75dpi" > FontPath "/usr/share/fonts/X11/misc" > FontPath "/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType" > Endsection > > but cannot identify where the problem is actually thus no remedy. > > Any idea? > > __ > 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. > -- Rich FitzJohn rich.fitzjohn gmail.com __ 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] functions and strings
Hi, Perhaps try this (based on 'bquote'): rewrite.expression <- function(expr, to, dep) { f <- function(expr) { if ( length(expr) == 1 ) if ( expr == as.name(dep) ) as.name(to) else expr else as.call(lapply(expr, f)) } f(expr) } rewrite <- function(expr, to, dep='x') { rewrite.expression(substitute(expr), to, dep) } > rewrite(1 + sin(cos(x)) + exp(x^2), 'xyz') 1 + sin(cos(xyz)) + exp(xyz^2) > rewrite(sin(x)+exp(x), 'xyz') sin(xyz) + exp(xyz) > rewrite(sin(i) + cos(sin(i^2)), 'tti', 'i') sin(tti) + cos(sin(tti^2)) ## Or, closer to your example, using the name of the argument and body ## of the function: f <- function(r) 2*r/sin(r) - b > rewrite.expression(body(f), 'foo', names(formals(f))) 2 * foo/sin(foo) - b Hope that helps, Rich On 9/13/06, Robin Hankin <[EMAIL PROTECTED]> wrote: > Hello everyone > > I know it looks like I'm making heavy weather of this, but > I don't think I communicated my problem properly. I really > appreciate you guys' help here. > > I am writing a wrapper for a mathematical library to > which I want to send character strings that it can execute, > and then pass the answer back to R. > > Now, I want a user to be able to type _any_ function > and _any_ string. For example: > > f <- function(i){sin(i) + cos(sin(i^2))} > string <- "tti" > > and then I want a function do() such that do(f,string) will return > > "sin(tti) + cos(sin(tti^2))" > > without worrying about whether f()'s arguments include or > do not include a particular letter, and without insisting that "i" > always appears as "(i)" . > > Although thinking about it, it's not > actually that bad to require the user to use some otherwise > rare sequence of letters, say "XxX" as > an argument, and then Dmitris's first method would work. > > Having said that, this is not an ideal solution > and it would be nicer to have some method that could detect > what the argument to f() is, where it is in the body, and substitute > those occurences for "string". > > I want a method that is perfectly general; I posted my > example of abcd...z(), not to be annoying and pedantic > but to illustrate that a simple gsub approach wouldn't work: > one has to know in advance which letters can and cannot > be used, and this information isn't available. > > I don't have a function so named (yet ;-). > > > best wishes > > rksh > > > > > Hi Dmitris, Thierry, > > > > I'm getting there but it's still not quite right if f() includes > > something like x^2: > > > > f <- function(x){exp(x^2)} > >>> > > > > gsub("(x)", "(xyz)", deparse(body(f))[2], fixed = TRUE) > > > > > > [1] "x^2" > > > > [I don't care about the spaces] > > > > > > > > also, > > > > I can't quite see how to implement Thierry's suggestion about > > changing the letter "x" into a letter that does not occur in f(), > > because of the > > following example: > > > > f <- function(x){abcdefghijklmnopqrstuvwxyz(x^2)} > > > > > > > > > > On 13 Sep 2006, at 09:08, Dimitris Rizopoulos wrote: > > > >> yes you're right, maybe this is better > >> > >>> f <- function(x){sin(x)+exp(x)} > >>> strng <- gsub("(x)", "(xyz)", deparse(body(f))[2], fixed = TRUE) > >>> sub('^[[:space:]]+', '', strng) > >> [1] "sin(xyz) + exp(xyz)" > >> > >> > >> Best, > >> Dimitris > >> > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > __ > 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. > -- Rich FitzJohn rich.fitzjohn gmail.com __ 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] time zones, daylight saving etc.
Hi, seq.dates() in the chron package does not allow creating sequences by minutes, so you'd have to roll your own sequence generator. Looks like the tzone attribute of the times is lost when using min(), max() and seq(). You can apply it back manually, but it does not affect the calculation, since POSIXct times are stored as seconds since 1/1/1970 (?DateTimeClasses). ## These dates/times just span the move from NZDT to NZST: dt.dates <- paste(rep(15:16, c(5,7)), "03", "2003", sep="/") dt.times <- paste(c(19:23, 0:6), "05", sep=":") dt <- paste(dt.dates, dt.times) ## No shift in times, or worrying about daylight savings; appropriate ## iff the device doing the recording was not itself adjusting for ## daylight savings, presumably. datetime <- as.POSIXct(strptime(dt, "%d/%m/%Y %H:%M"), "GMT") ## Create two objects with all the times in your range, one with the ## tzone attribute set back to GMT (to match datetimes), and one ## without this. mindata1 <- mindata2 <- seq(from=min(datetime), to=max(datetime), by="mins") attr(mindata2, "tzone") <- "GMT" fmt <- "%Y %m %d %H %M" ## These both do the matching correctly: match(format(datetime, fmt), format(mindata1, fmt, tz="GMT")) match(format(datetime, fmt), format(mindata2, fmt, tz="GMT")) ## However, the first of these will not, as it gets the timezone all ## wrong, since it's neither specified in the call to format(), or as ## an attribute of the POSIXct object. match(format(datetime, fmt), format(mindata1, fmt)) match(format(datetime, fmt), format(mindata2, fmt)) ## It is also possible to run match() directly off the POSIXct object, ## but I'm not sure how this will interact with things like leap ## seconds: match(datetime, mindata1) Time zones do my head in, so you probably want to check this all pretty carefully. Looks like there's lots of gotchas (e.g. subsetting a POSIXct object strips the tzone attribute). Cheers, Rich On 5/12/05, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > You could use the chron package. It represents date times without > using time zones so you can't have this sort of problem. > > On 5/10/05, Carla Meurk <[EMAIL PROTECTED]> wrote: > > Hi, I have a whole bunch of data, which looks like: > > > > 15/03/2003 10:20 1 > > 15/03/2003 10:21 0 > > 15/03/2003 12:02 0 > > 16/03/2003 06:10 0 > > 16/03/2003 06:20 0.5 > > 16/03/2003 06:30 0 > > 16/03/2003 06:40 0 > > 16/03/2003 06:50 0 > > > > 18/03/2003 20:10 0.5 > > etc. (times given on a 24 hour clock) > > > > and goes on for years. I have some code: > > > > data<-read.table("H:/rainfall_data.txt",h=T) > > library(survival) > > datetime <- as.POSIXct(strptime(paste(data$V1, data$V2), "%d/%m/%Y > > %H:%M"), tz="NZST") > > > > which produces: > > > > [10] "2003-03-13 21:13:00 New Zealand Daylight Time" > > [11] "2003-03-15 13:20:00 New Zealand Daylight Time" > > [12] "2003-03-15 22:20:00 New Zealand Daylight Time" > > [13] "2003-03-15 22:21:00 New Zealand Daylight Time" > > [14] "2003-03-16 00:02:00 New Zealand Daylight Time" > > [15] "2003-03-16 18:10:00 New Zealand Standard Time" > > [16] "2003-03-16 18:20:00 New Zealand Standard Time" > > [17] "2003-03-16 18:30:00 New Zealand Standard Time" > > > > My problem is that "15/03/2003 12:02" has become "16/03/2003 00:02" > > i.e. it is 12 hours behind (as is everything else), but also, I do not > > want to change time zones. > > > > The 12 hour delay is not really a problem just an annoyance, but the > > time zone change is a problem because later on I need to match up data > > by time using > > > > mindata<-seq(from=min(datetime),to=max(datetime),by="mins") > > newdata<-matrix(0,length(mindata),1) > > newdata[match(format.POSIXct(datetime,"%Y %m %d %H > > %M"),format.POSIXct(mindata,"%Y %m %d %H %M"))]<-data$V3 > > > > and things go wrong here with matching repeating times/missing times > > around the timezone changes and, my resulting vector is 1 hour shorter > > than my other series. From the R help I see that my OS may be to blame > > but, even if I specify tz="GMT" I still get NZST and NZDT. Can someone > > help? > > > > I hope this all makes sense > > > > Carla > > > >
Re: [R] functions of functions
This has a fairly brutal approach to recycling, and won't warn you if one argument's length is not a multiple of another, etc. But seems to do the trick. f <- function(f, y, x) { n <- max(c(length(f), length(y))) f <- rep(f, length.out=n) y <- rep(y, length.out=n) rowSums(mapply(function(f, y) (match.fun(f))(y + x), f=f, y=y)) } Testing, it matches your examples... x <- rnorm(20) identical(all.equal(sin(x+0) + sin(x+1) + sin(x+2), f("sin",0:2,x)), TRUE) identical(all.equal(sin(x+1) + cos(x+2), f(c("sin","cos"), 1:2,x)), TRUE) identical(all.equal(sin(x+3) + cos(x+3) + exp(x+3), f(c("sin","cos","exp"),3,x)), TRUE) Cheers, Rich On 5/9/05, Robin Hankin <[EMAIL PROTECTED]> wrote: > Hello Uwe > > thanks for this. Unfortunately it doesn't quite do what I want: > > R> x <- c(0.3,0.3,0.5) > R> f(c("sin","cos"),1:2,x) > > 1] 1.266795 > Warning messages: > 1: longer object length > is not a multiple of shorter object length in: x + int > 2: number of rows of result > is not a multiple of vector length (arg 1) in: cbind(foo, x + int) > R> > > [ > > I need > > R> sin(x+1) + cos(x+2) > [1] 0.2972822 0.2972822 0.1963514 > > ] > > best wishes > > Robin > > > On May 9, 2005, at 08:34 am, Uwe Ligges wrote: > > > Robin Hankin wrote: > > > >> Hi > >> I have an application where my difficulty boils down to not > >> being able to define a function f() with the following properties: > >> f("sin",0:2,x) #returns sin(x+0) + sin(x+1) + sin(x+2) > >> f(c("sin","cos"), 1:2,x) #returns sin(x+1) + cos(x+2) > >> f(c("sin","cos","exp"),3,x) #returns sin(x+3) + cos(x+3) + exp(x+3) > >> anyone? > > > > Not really nice, but hopefully works: > > > > f <- function(foo, int, x){ > > # too lazy to think myself about recycling: > > X <- cbind(foo, x + int) > > # mapply-ing over both columns > > values <- mapply(function(foo, x) do.call(foo, list(x)), > >X[,1], as.integer(X[,2])) > > # caculating the sum: > > return(sum(values)) > > } > > > > > > Uwe > > > > > > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] Questions about the intersection area under two kernel densities
Hi, A "density" object's x and y values can be accessed using $x and $y (this is in ?density, under "Value:") obj <- density(rnorm(100, 0, 1)) obj$x obj$y You may find it useful to force the "from" and "to" arguments to be the same for the two calls to density(), if you want to work directly from the x and y values. x <- rnorm(100, 0, 1) y <- rnorm(100, .2, 1) dx <- density(x) dy <- density(y) ## Re-do the densities so they have the same x-values: lim <- range(dx$x, dy$x) dx <- density(x, from=lim[1], to=lim[2]) dy <- density(y, from=lim[1], to=lim[2]) plot(dx, col="blue", ylim=range(dx$y, dy$y)) lines(dy, col="red") polygon(dx$x, pmin(dx$y, dy$y), col="purple") Cheers, Rich On 5/5/05, Grace Clinton <[EMAIL PROTECTED]> wrote: > Hi there, > > I am working on a project which needs the value of the interaction area under > two distributions( eatimated by kernel density estimators). > > For example: > > x<-rnorm(100,0,1) > y<-rnorm(100,0.2,1) > density(x) # This produces the summary of dependent variable and independent > variable. > > How can I get the individual values of variables and reform a curve to > calculate the area under curve? > > Is there a way to get aroud this and give me the solution? > > Thanks a lot. > > Grace > > - > >[[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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] local average
Hi, cut() and tapply() are your friends: tapply(Y, cut(X, 0:100, include.lowest=TRUE), mean) To compute medians, just pass median to tapply(). You will get NAs where no data is found in any bin. Cheers, Rich On 4/21/05, Jens Hainmueller <[EMAIL PROTECTED]> wrote: > Hello, > > probably this isn't hard, but I can't get R to do this. Thanks for your > help! > > Assume I have a matrix of two covariates: > > n<- 1000 > Y<- runif(n) > X<- runif(n,min=0,max=100) > data <- cbind(Y,X) > > Now, I would like to compute the local average of Y for each X interval 0-1, > 1-2, 2-3, ... 99-100. In other words, I would like to obtain 100 (local) > Ybars, one for each X interval with width 1. > > Also, I would like to do the same but instead of local means of Y obtain > local medians of Y for each X interval. > > Best, > Jens > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] Clipboard size?
Hi Chris, >From ?file: Clipboard: ... When writing to the clipboard, the output is copied to the clipboard only when the connection is closed or flushed. There is a 32Kb limit on the text to be written to the clipboard. This can be raised by using e.g. 'file("clipboard-128")' on NT-based versions of Windows, to give 128Kb. So, > write.table(dm, "clipboard", sep="\t") Warning message: clipboard buffer is full and output lost > write.table(dm, "clipboard-128", sep = "\t") > Cheers, Rich On 4/21/05, Chris Bergstresser <[EMAIL PROTECTED]> wrote: > Hi all -- > >I have a matrix of doubles (roughly 30x80) which I'd like to copy to > the clipboard. However, as the following shows: > > > dm = matrix(runif(30 * 80), nrow = 80) > > write.table(dm, "clipboard", sep = "\t") > Warning message: > clipboard buffer is full and output lost > >Is there any way to increase the buffer? Obviously, other programs > don't have the same limitations (i.e., I can copy the same volume of > data from Excel or my text editor and paste it into R without a problem) > > -- Chris > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] controlling the x axis of boxplots
Hi Chris, You can get the desired effect by using the "at" argument to boxplot, and by setting up the plot dimensions manually. men <- data.frame(grp=rep(letters[c(1, 2, 4, 6)], each=10), response=rnorm(40)) women <- data.frame(grp=rep(letters[c(2:5, 7)], each=10), response=rnorm(50)) ## Determine all levels used, the number of levels, and appropriate ## xlim for the plot grp.levs <- sort(unique(c(levels(men$grp), levels(women$grp nlevs <- length(grp.levs) xlim <- c(.5, nlevs+.5) ## Determine which of the levels are present in men and women; these ## are the x coordinates to draw the boxes (this will need tweaking if ## you are using ordered factors). at.men <- match(levels(men$grp), grp.levs) at.women <- match(levels(women$grp), grp.levs) par(mfrow=c(2,1)) ## boxplot() does not take an xlim argument, so you'll have to set up ## the plot yourself (including the axis, since the default is not ## appropriate for a boxplot). plot(NA, type="n", xlim=xlim, ylim=range(men$response), xlab="", ylab="Response", xaxt="n") axis(1, 1:nlevs, grp.levs) boxplot(response ~ grp, men, at=at.men, add=TRUE) plot(NA, type="n", xlim=xlim, ylim=range(women$response), xlab="Group", ylab="Response", xaxt="n") axis(1, 1:nlevs, grp.levs) boxplot(response ~ grp, women, at=at.women, add=TRUE) Cheers, Rich On 4/20/05, Chris Evans <[EMAIL PROTECTED]> wrote: > v 2.0.1 (sh old!) on Win2k > > I think I know the answer to this but I can hope ... > > I have data for continuous variables (measures of residents) by a > categorical variable in range (1,22), the units in which they live. > > I want to plot these data with a pair of boxplots one above another > with same x-axis (1,22) using par(mfrow=c(2,1)) and then plotting > first for the women then for the men. My problem is that some units > have only men, some have only women, and some have both. I'd like > both plots to have the same x axis and the notched, varwidth boxplots > to locate themselves with some gaps so that the units are in the same > place on the x axis on each plot. > > I think that I can't do this with boxplot or bxp as both work out the > x axis from the work that boxplot has done on the data. Although > there also seem to be useful extensions or alternative boxplots in > other packages, I can't see one that does what I want and I think > that rolling my own from bxp is really beyond my skills. > > Am I right that it doesn't exist in the CRAN packages? If not, > apologies and point me where I should look? If I am right (boo hoo!) > I don't suppose anyone has written this or is feeling like > demonstrating their personal genius with R coding?!!! If they were, > I don't think I'd be the only one to end up owing them a great deal > of gratitude! > > Thanks as ever to all who have made and continue to make R what it > is: brilliant! > > Chris > > -- > Chris Evans <[EMAIL PROTECTED]> > Consultant Psychiatrist in Psychotherapy, Rampton Hospital; > Research Programmes Director, Nottinghamshire NHS Trust, > Hon. SL Institute of Psychiatry > *** My views are my own and not representative of those institutions > *** > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] terminate R program when trying to access out-of-bounds array element?
Hi, You could try redefining "[", so that if any element subsetted returned an NA, it would throw an error, e.g.: (Warning: Largely untested! - this will almost certainly cause problems in other classes that use [ to subset. Possibly defining this as "[.default" would be better...) "[" <- function(x, ...) { res <- (base::"[")(x, ...) if ( any(is.na(res)) ) stop("An element was NA in a subset") res } > x <- 1:5 > x[4] [1] 4 > x[7] Error in x[7] : An element was NA in a subset However, you'll probably find this is a little over-zealous, e.g.: > y <- c(1:3, NA, 4) > y[5] [1] 4 > y[4] Error in y[4] : An element was NA in a subset If you just want to check for an NA at printing, defining a function like this might be more appropriate: print.or.stop <- function(x) { if ( any(is.na(x)) ) stop("An element was NA in a subset") print(x) } You could write a more complicated "[" function that does a bunch of testing, to see if the element extracted is going to be out of the extent of the vector (rather than a "genuine" NA), but since there are a number of ways elements can be extracted from vectors (numeric, logical and character indices can all be used to index vectors, and these have recycling rules, etc), this is probably much more work than a few checks in your code where an NA would actually indicate an error. Cheers, Rich On 4/14/05, Vivek Rao <[EMAIL PROTECTED]> wrote: > I want R to stop running a script (after printing an > error message) when an array subscript larger than the > length of the array is used, for example > > x = c(1) > print(x[2]) > > rather than printing NA, since trying to access such > an element may indicate an error in my program. Is > there a way to get this behavior in R? Explicit > testing with the is.na() function everywhere does not > seem like a good solution. Thanks. > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] i param in "for" loop does not takes zeros?
The for loop is not ignoring the zero at all, but the assignment is, since R indexes starting at 1, not zero. > sim <- c() > sim[0] <- 1 > sim numeric(0) To run this loop this way, you need to add one to the index: for ( i in 0:5 ) sim[i+1] <- dbinom(i, 5, p) However, you'd be better off passing your vector of values directly to dbinom(): > dbinom(0:5, 5, p) [1] 0.32768 0.40960 0.20480 0.05120 0.00640 0.00032 > all(dbinom(0:5, 5, p) == sim) [1] TRUE Cheers, Rich On 4/14/05, Francisco J. Zagmutt <[EMAIL PROTECTED]> wrote: > Hi all > > Is there any reason why the parameter i in a "for" loop ignores a value of > zero? For example > > sim=c() > p=.2 > for(i in 0:5) > {sim[i]=dbinom(i,5,p) > } > > sim > [1] 0.40960 0.20480 0.05120 0.00640 0.00032 > > In this example the quantile i= 0 was ignored since > dbinom(0,5,p) > [1] 0.32768 > > The same behaviour occurs if I use a while loop to perform the same > calculation: > sim=c() > p=.2 > i=0 > while(i <6) > {sim[i]=dbinom(i,5,p) > i=i+1 > } > sim > [1] 0.40960 0.20480 0.05120 0.00640 0.00032 > > How can I perform a loop passing a zero value parameter? I know I can use > an if statement for i<=0 but I was wondering why the loop is ignoring the > zero value. > > Many thanks! > > Francisco > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] Re: how to separate a string
To get the first character from a string, use substr(s, 1, 1) To split strings at a period, use strsplit(s, "\\.") (the period must be quoted, as "." matches anything in a regular expression). To get the index for "." in a string, see "?regexpr, but you will not need to do that if you use strsplit(). Cheers, Rich On 4/13/05, Cuichang Zhao <[EMAIL PROTECTED]> wrote: > hello, > i wonder how is string represent in R. if i have a string s= "hello", how > can i refer to first character in the string s? > also if i have s1 = "hello.1", s2 = "ok.1", how can i separate the s1 into > "hello" "1" and s2 into "ok" and "1"? I have tried to use the substring > function, but i don't where i can get the index for "." in the string? > > Thank you so much > > C-Ming > April 12, 2005 > > > - > > > [[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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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 into stata
library(foreign) ?write.dta Cheers, Rich On Apr 13, 2005 8:56 AM, Emre Ozaltin <[EMAIL PROTECTED]> wrote: > What is to command to change a dataset in R format "X.RData" into stata > format "X.dta"? > > Thank you, > > > --- > Emre Özaltin > Epidemiologist > Harvard Initiative for Global Health (HIGH) > 104 Mt. Auburn St. 02138 Cambridge, MA > tel: 1 (617) 495-4884 > fax: 1 (617) 495-8231 > email: [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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] Plotting the occassional second label
Another option is to combine the sprays used as a single string, and plot that directly, rather than subsetting the data. This has the advantages of not having to worry about how much to offset the second label by, and should also work if you got more than two sprays per day. spray$SprayDate <- as.Date(spray$SprayDate) spray.sub <- spray[spray$PD=="Spidermites",] ## Collapse, so there is only a single row per date spray.col <- spray.sub[unique(tapply(spray.sub$SprayDate, spray.sub$SprayDate)),] ## And paste together any treatments used on a single day, separated ## by a newline txt <- tapply(spray.sub$Trt, spray.sub$SprayDate, paste, collapse="\n") plot(spray.col$SprayDate, spray.col$Qwater, xlim=c(as.Date("2005-03-08"), as.Date("2005-03-24")), ylim=c(0,1500)) text(spray.col$SprayDate, spray.col$Qwater, txt, pos=4, srt=45) If you wanted the order of the chemicals used changed, you could insert a new function into the tapply() call, e.g. tapply(spray.sub$Trt, spray.sub$SprayDate, function(x) paste(rev(x), collapse="\n")) Cheers, Rich On Apr 10, 2005 8:24 PM, Lisbeth Riis <[EMAIL PROTECTED]> wrote: > Dear useRs, > > I'm trying to plot spray quantities against dates, and label the points > on the plot. Basically quite simple, but sometimes two chemicals have > been used and are listed in separate rows in the table as below; then > the labels are written on top of each other. ... > Does anyone have a better way of splitting the second reading for each > date off, or another way of printing two labels for one plotted point? -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] vectorized approach to cumulative sampling
Hi, sample() takes a "replace" argument, so you can take large samples, with replacement, like this: (In the sample() call, the 50*target/mean(old) should make it sample 50 times more than likely. This means the while loop will probably get executed only once. This could be tuned easily, and there may be better ways of guessing how much to take). old <- c(1:2000) p <- runif(1:2000) target <- 4000 new <- 0 while ( sum(new) < target ) new <- sample(old, 50*target/mean(old), TRUE, p) i <- which(cumsum(new) >= target)[1] new <- new[1:i] new[i] <- new[i] - (sum(new)-target) Cheers, Rich On Apr 8, 2005 9:19 AM, Daniel E. Bunker <[EMAIL PROTECTED]> wrote: > Hi All, > > I need to sample a vector ("old"), with replacement, up to the point > where my vector of samples ("new") sums to a predefined value > ("target"), shortening the last sample if necessary so that the total > sum ("newsum") of the samples matches the predefined value. > > While I can easily do this with a "while" loop (see below for example > code), because the length of both "old" and "new" may be > 20,000, a > vectorized approach will save me lots of CPU time. > > Any suggestions would be greatly appreciated. > > Thanks, Dan > > # loop approach > old=c(1:10) > p=runif(1:10) > target=20 > > newsum=0 > new=NULL > while (newsumi=sample(old, size=1, prob=p); >new[length(new)+1]=i; >newsum=sum(new) >} > new > newsum > target > if(newsum>target){new[length(new)]=target-sum(new[-length(new)])} > new > newsum=sum(new); newsum > target > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] read.table with header and text data
See ?read.table, especially the argument "as.is". Cheers, Rich On Apr 7, 2005 9:55 AM, Laura Holt <[EMAIL PROTECTED]> wrote: > Hi R! > > I am reading in a text file which has one column of alpha data and 5 columns > of numeric data. > > There is a header row. > > I would like the alpha data column to just be character rather than factor. > > Is there a way to do this, please? I'm thinking that it might be I() but > can't figure out exactly how. > > Thanks, > Laura > mailto: [EMAIL PROTECTED] > R 2.0.1 Windows > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] How to do aggregate operations with non-scalar functions
Hi Itay, Not sure if by() can do it directly, but this does it from first principles, using lapply() and tapply() (which aggregate uses internally). It would be reasonably straightforward to wrap this up in a function. a <- rep(c("a", "b"), c(6,6)) x <- rep(c("x", "y", "z"), c(4,4,4)) df <- data.frame(a=a, x=x, r=rnorm(12)) ## Probabilities for quantile p <- c(.25, .5, .75) ## This does the hard work of calculating the statistics over your ## combinations, and over the values in `p' y <- lapply(p, function(y) tapply(df$r, list(a=a, x=x), quantile, probs=y)) ## Then, we need to work out what combinations of a & x are possible: ## these are the header columns. aggregate() does this in a much more ## complicated way, which may handle more difficult cases than this ## (e.g. if there are lots of missing values points, or something). vars <- expand.grid(dimnames(y[[1]])) ## Finish up by converting `y' into a true data.frame, and ommiting ## all the cases where all the values in `y' are NA: these are ## combinations of a and x that we did not encounter. y <- as.data.frame(lapply(y, as.vector)) names(y) <- paste(p, "%", sep="") i <- colSums(apply(y, 1, is.na)) != ncol(y) y <- cbind(vars, y)[i,] Cheers, Rich On Apr 6, 2005 10:59 AM, Itay Furman <[EMAIL PROTECTED]> wrote: > > Hi, > > I have a data set, the structure of which is something like this: > > > a <- rep(c("a", "b"), c(6,6)) > > x <- rep(c("x", "y", "z"), c(4,4,4)) > > df <- data.frame(a=a, x=x, r=rnorm(12)) > > The true data set has >1 million rows. The factors "a" and "x" > have about 70 levels each; combined together they subset 'df' > into ~900 data frames. > For each such subset I'd like to compute various statistics > including quantiles, but I can't find an efficient way of > doing this. Aggregate() gives me the desired structure - > namely, one row per subset - but I can use it only to compute > a single quantile. > > > aggregate(df[,"r"], list(a=a, x=x), quantile, probs=0.25) > a x x > 1 a x 0.1693188 > 2 a y 0.1566322 > 3 b y -0.2677410 > 4 b z -0.6505710 > > With by() I could compute several quantiles per subset at > each shot, but the structure of the output is not > convenient for further analysis and visualization. > > > by(df[,"r"], list(a=a, x=x), quantile, probs=c(0, 0.25)) > a: a > x: x > 0%25% > -0.7727268 0.1693188 > -- > a: b > x: x > NULL > -- > > [snip] > > I would like to end up with a data frame like this: > > a x 0%25% > 1 a x -0.7727268 0.1693188 > 2 a y -0.3410671 0.1566322 > 3 b y -0.2914710 -0.2677410 > 4 b z -0.8502875 -0.6505710 > > I checked sweep() and apply() and didn't see how to harness > them for that purpose. > > So, is there a simple way to convert the object returned > by by() into a data.frame? > Or, is there a better way to go with this? > Finally, if I should roll my own coercion function: any tips? > >Thank you very much in advance, >Itay > > > [EMAIL PROTECTED] / +1 (206) 543 9040 / U of Washington > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] summing columns using partial labels
Gidday, Perhaps try something along these lines: ## Establish which 4-letter group each row belongs to prefix <- substr(names(d), 1, 4) gp <- match(prefix, unique(prefix)) gp[regexpr("\\.total$", names(d)) > -1] <- NA # Exclude `*.total' rows ## Sum up each of the groups d.sums <- lapply(split(seq(along=d), gp), function(x) rowSums(d[x])) names(d.sums) <- paste(unique(prefix), "sum", sep=".") ## Append to the end of the original data.frame d.new <- cbind(d, d.sums) Cheers, Rich On Apr 6, 2005 6:05 AM, T Petersen <[EMAIL PROTECTED]> wrote: > I have a dataset of the form > > Year tosk.fai tosk.isd tosk.gr ... tosk.total hysa.fai > hysa.isd ... > > and so on. I want to sum all the columns using the first four letters in > the columns label(e.g. 'tosk', 'hysa' etc.). How can you do that? Also, > the sums should be without the '.total'column (e.g. 'tosk.total') as > this serves as a check that everything was done right. > > Kind regards > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] plotting mathematical notation and values substitution
Gidday, See ?plotmath and demo(plotmath) for lots of information on plotting with mathematical symbols. This produces what you seem to be after (paste() being the missing ingredient): plot(1:10, main=substitute(paste("Monotonic Multigamma run ( *", list(n==len, theta==t1), " * )"), list(len=len, t1=t1))) This does seem to be a lot of work just to get a theta symbol, and this seems just as informative: plot(1:10, main=paste("Monotonic Multigamma run ( * n =", len, "theta =", t1, " * )")) Your second example gives a syntax error for me. Cheers! Rich On Apr 5, 2005 6:50 AM, Luca Scrucca <[EMAIL PROTECTED]> wrote: > # I add this to let you run the example by copy and paste > > t1 <- 0.5; len <- 1 > # then > > plot(1:10, >main = substitute("Monotonic Multigamma run (" * n == len * ", " * > theta == t1 * ").", list(len = len, t1 = t1))) > > but I got the following: > > Error: syntax error > > I also tried with just one value substitution: > > > plot(1:10, >main = substitute("Monotonic Multigamma run (" theta == t1 * ").", > list(len = len, t1 = t1))) > > which works fine. How can I have more than one value substitution, > together with mathematical notation and text? -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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: Re[2]: [R] need any advises for code optimization.
Hi again, The arguments to %in% are in the wrong order in your version. Because of that, the statement row.names(to.drop) %in% row.names(whole) will be TRUE for the first nrow(to.drop) elements, and FALSE for the remainder. To fix it, just switch the order around, or use the simpler version: whole[!row.names(whole) %in% row.names(to.drop),] The fact that your row names are different to the row indices in whole will be what is causing the error when trying my variant. Cheers, Rich On Apr 4, 2005 10:21 PM, Wladimir Eremeev <[EMAIL PROTECTED]> wrote: > Dear Rich, > > Thank you for reply. I think, optimization, you offered will satisfy > my needs. > I don't completely understand the following. > > RF> ## And wrap the original in a function for comparison: > RF> ## This does not subset the way you want: > RF> ## whole[-which(row.names(to.drop) %in% row.names(whole)),] > RF> whole[-as.integer(row.names(to.drop)),] > > Why doesn't my subset work properly? > > My data frame 'whole' was created from 3 another data frames by rbind, > if it makes sense... > > Moreover, your variant gives the error: > > > as.integer(row.names(to.drop)[120:220]) > [1] 2761 3616 3629 5808 7204 7627 8192 10851 20275 273611 > 4492 256691 8797 > [14] 11756 46673 246981 250401 335591773774786993995 > 1454 2715 6990 > [27] 7951 7962 8185 8662 9406 442100 478100 528100 208710 211710 > 215910 19846 28660 > [40] 28661 28691 28806 28878 450611 497411 81672 91572 119232 166191 > 166281 203981 204201 > [53] 255171 255212 255301 300651 331212 371761 397651 405241 415331 8779 > 195510 197910 203210 > [66] 205410 205510 211810 220610 19615 27165 28581 28640 28641 28642 > 28662 28714 48692 > [79] 449611 449911 497211 81702 195451 202491 202551 253931 255071 259102 > 266971 303341 331831 > [92] 353912 371931 374612 394461 397641 412671 9227 464100 1558 2161 > > whole[-as.integer(row.names(to.drop)[120:220]),] > Error in "[.data.frame"(whole, -as.integer(row.names(to.drop)[120:220]), : > subscript out of bounds > > Row names don't coincide with row order numbers in my case. > > -- > Best regards > Wladimir Eremeev mailto:[EMAIL PROTECTED] > > -- -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] need any advises for code optimization.
ut droppped points. > whole.flt.3<-whole[-which(row.names(to.drop) %in% row.names(whole)),] > === > > The problem is: the 'whole' data set is large, more than 10 > rows, and the script runs several hours. > The running time becomes greater, if I build a sphere instead of a > cube. > > I would like to optimize it in order to make it run faster. > Is it possible? > Will a sorting take effect? > Thank you for attention and any good feedback. > > -- > Best regards > Wladimir Eremeev mailto:[EMAIL PROTECTED] > > == > Research Scientist, PhD > Russian Academy of Sciences > > __ > 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 > -- Rich FitzJohn rich.fitzjohn gmail.com |http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike __ 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] Re: is there a function like %in% for characters?
Or, using the %foo%-style functions: "%charin%" <- function(x, y) regexpr(x, y) != -1 > "a" %charin% "asdf" [1] TRUE > "a" %charin% "bsdf" [1] FALSE Cheers, Rich On Sat, 2 Apr 2005 22:54:35 -0500, "Liaw, Andy" <[EMAIL PROTECTED]> wrote: > I suppose here's one way: > > > hasChar <- function(x, y) { length(grep(x, y)) > 0 } > > hasChar("a", "abcd") > [1] TRUE > > hasChar("e", "abcd") > [1] FALSE > > Andy > > > From: Terry Mu > > > > like: > > > > "a" %in% "abcd" > > TRUE > > > > Thanks. > > > > __ > > 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-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