Re: [R] Concat two rows
Hi, Try this, within(data.frame(A=letters[1:5], B=1:5), C - paste(A, B, sep=.)) HTH, baptiste On Nov 30, 2010, at 9:30 AM, alcesgabbo wrote: I have a table like this: A B aaa 111 bbb 222 ccc 333 ddd 444 and I would like a new row C: A B C aaa 111 aaa.111 bbb 222 bbb.222 ccc 333 ccc.333 ddd 444 ddd.444 How can I do this ??? Thanks Alberto -- View this message in context: http://r.789695.n4.nabble.com/Concat-two-rows-tp3064998p3064998.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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@r-project.org 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] Help me with R plotting
Hi, Try this, robots - data.frame(id=letters[1:20], x=rnorm(20), y=rnorm(20), consumption=runif(20, 10, 100)) library(ggplot2) ggplot(robots) + geom_point(aes(x, y, colour=cut(consumption, c(0, 30, 50, 100 + geom_text(aes(x, y, colour=cut(consumption, c(0, 30, 50, 100)), label=round(consumption, 2)), vjust=1, legend=FALSE) + labs(colour=consumption) HTH, baptiste On Nov 18, 2010, at 4:18 PM, Alaios wrote: Thanks a lot for your help I tried first this test-(myvalues)-mean(myvalues) so to put the values close to the 0...30 scale... then I passed this as an argument plot(x,y,col=test) which plots the places where the robots are with a small color. I think this is ok for now. What I want next is also to plot some color bar so the reader can by inspecting first this color bar to say Ah the read is for the range of 5-10 watts Also I tried legend... which creates small boxes below the dots which is Do not what I want exactly. I would like below every plot to write some text but not inside any box that will cover part of the image. Best Regards Alex --- On Thu, 11/18/10, Tal Galili tal.gal...@gmail.com wrote: From: Tal Galili tal.gal...@gmail.com Subject: Re: [R] Help me with R plotting To: Alaios ala...@yahoo.com Cc: Rhelp r-help@r-project.org Date: Thursday, November 18, 2010, 2:13 PM ?plotWill give a good solution #Example:set.seed(5)xx - data.frame(x = runif(50), y = runif(50), z = sample(c(1:3), 50, T))with(xx, plot(x, y, col = z, pch = 19)) Now you can start going into how to play with the colors, and how to add ?legend to the image... Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- Hello everyone. I want some help with plots. I have some robots in an area. Every robot is placed on x,y coordinates and every robot has a power consumption of some watts. I would like to show where are the robots by showing in a map dots (where every dots is the x,y coordinate). Below that dot it would be nice to write the watt consumption but also use some colors to denote different classes of consumption. So 10-20watt should be denoted by blue 20-30 with red, 30-40 with pink and so on. Which is the appropriate plot function for that? Regards Alex [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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@r-project.org 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] Plotting a grid of directly specified colours
Hi, try this, library(grid) grid.raster(matrix(colors(),ncol=50),interp=F) HTH, baptiste On Nov 4, 2010, at 5:00 PM, Peter Davenport wrote: Dear R-help, Could any of you direct me to a function for plotting a grid of colours, directly specified by a matrix of hex colour codes? In other words I'm looking for a heatmap() or image()-like function to which I can specify the colour of each grid location directly, rather than providing a numerical matrix and a 1D-colour scale (heatmap, image, levelplots,NeatMap...). I'm surprised I haven't found anything simple with RSiteSearch, help.search, net. I'd like to use this function to encode one variable as chroma and a second as luminance (hcl colour space), so that the two variables can be visualised in a single heatmap (the variable are fold-change and a q-value, a significance measure). If anyone has any thoughts/warnings to offer re this idea then I'd love to hear them (it must have been tried before, but I've not come across any examples) . Best wishes and thank you, Peter Davenport [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] lattice centre a diverging colour scale
Great, I don't know how I missed that, thanks! baptiste On Sep 23, 2010, at 10:39 AM, Deepayan Sarkar wrote: On Thu, Sep 23, 2010 at 12:33 PM, baptiste auguie baptiste.aug...@googlemail.com wrote: Dear list, I'm using lattice::levelplot to plot a coloured image of 3D data. The range of the z values goes from negative to positive, but is not exactly centred around 0. I would however like to map a diverging colour scale with white falling exactly at 0, and both extremes being symmetrical in the legend to better contrast the opposite change in colour saturation. The following dummy example illustrates my problem, library(lattice) d - transform(expand.grid(x=seq(0, 10, length=100), y=seq(0, 10, length=100)), z = sin(x/pi)*cos(0.5*y/pi) - 0.2) levelplot(z~x*y, data=d, panel=panel.levelplot.raster, cuts = 100, interpolate = TRUE) The colour scale goes from -0.3 to 0.9 with a middle (white) value of 0.3 approximately. I'd like it to be from -1 (most saturated blue) to 1 (most saturated pink), say, with 0 being white. I read the entry in ?levelplot and in ?level.colors but could not find a solution to this particular case. Specify the cut-points explicitly: levelplot(z~x*y, data=d, panel = panel.levelplot.raster, at = do.breaks(c(-1, 1), 100)) -Deepayan __ R-help@r-project.org 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] puzzle with integrate over infinite range
Hi, Thanks for the tip, but it's still mysterious to me. Reading ?integrate did not give me much hint as to what relative accuracy means in this context. I looked at the source of integrate.c but it's still not clear to me how the default value of rel.tol (10^-4 for me) is not enough to prevent a completely wrong answer (the error is much larger than this). Obviously, I'm worried now that I may not always choose a good value of ref.tol, if picked arbitrarily without my understanding of what it means. Thanks, baptiste On Sep 21, 2010, at 3:38 PM, Ravi Varadhan wrote: There is nothing mysterious. You need to increase the accuracy of quadrature by decreasing the error tolerance: # I scaled your function to a proper Gaussian density shiftedGauss - function(x0=500){ integrate(function(x) 1/sqrt(2*pi * 100^2) * exp(-(x-x0)^2/(2*100^2)), 0, Inf, rel.tol=1.e-07)$value } shift - seq(500, 800, by=10) plot(shift, sapply(shift, shiftedGauss)) Hope this helps, Ravi. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of baptiste auguie Sent: Tuesday, September 21, 2010 8:38 AM To: r-help Subject: [R] puzzle with integrate over infinite range Dear list, I'm calculating the integral of a Gaussian function from 0 to infinity. I understand from ?integrate that it's usually better to specify Inf explicitly as a limit rather than an arbitrary large number, as in this case integrate() performs a trick to do the integration better. However, I do not understand the following, if I shift the Gauss function by some amount the integral should not be affected, shiftedGauss - function(x0=500){ integrate(function(x) exp(-(x-x0)^2/100^2), 0, Inf)$value } shift - seq(500, 800, by=10) plot(shift, sapply(shift, shiftedGauss)) Suddenly, just after 700, the value of the integral drops to nearly 0 when it should be constant all the way. Any clue as to what's going on here? I guess it's suddenly missing the important part of the range where the integrand is non-zero, but how could this be overcome? Regards, baptiste sessionInfo() R version 2.11.1 (2010-05-31) x86_64-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] inline_0.3.5RcppArmadillo_0.2.6 Rcpp_0.8.6 statmod_1.4.6 loaded via a namespace (and not attached): [1] tools_2.11.1 __ R-help@r-project.org 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@r-project.org 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] puzzle with integrate over infinite range
Thanks, I'll do that too from now on. It strikes me that in a case such as this one it may be safer to use a truncated, finite interval around the region where the integrand is non-zero, rather than following the advice of ?integrate to use Inf as integration limit. At least one wouldn't risk to get an entirely wrong result depending on a choice of rel.tol. Regarding this parameter, is there a simple interpretation of how it affected the result in the context of my example? Thanks again, baptiste On Sep 21, 2010, at 4:04 PM, Ravi Varadhan wrote: You are dealing with functions that are non-zero over a very small interval, so you have to be very careful. There is no method that is going to be totally foolproof. Having said that, I have always felt that the default tolerance in integrate is too liberal (i.e. too large). I always use rel.tol of 1.e-08 (roughly, sqrt(machine epsilon)) in my computations, and I also increase subdivisions to 500. Ravi. From: baptiste Auguié [mailto:baptiste.aug...@googlemail.com] Sent: Tuesday, September 21, 2010 9:58 AM To: Ravi Varadhan Cc: 'baptiste auguie'; 'r-help' Subject: Re: [R] puzzle with integrate over infinite range Hi, Thanks for the tip, but it's still mysterious to me. Reading ?integrate did not give me much hint as to what relative accuracy means in this context. I looked at the source of integrate.c but it's still not clear to me how the default value of rel.tol (10^-4 for me) is not enough to prevent a completely wrong answer (the error is much larger than this). Obviously, I'm worried now that I may not always choose a good value of ref.tol, if picked arbitrarily without my understanding of what it means. Thanks, baptiste On Sep 21, 2010, at 3:38 PM, Ravi Varadhan wrote: There is nothing mysterious. You need to increase the accuracy of quadrature by decreasing the error tolerance: # I scaled your function to a proper Gaussian density shiftedGauss - function(x0=500){ integrate(function(x) 1/sqrt(2*pi * 100^2) * exp(-(x-x0)^2/(2*100^2)), 0, Inf, rel.tol=1.e-07)$value } shift - seq(500, 800, by=10) plot(shift, sapply(shift, shiftedGauss)) Hope this helps, Ravi. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of baptiste auguie Sent: Tuesday, September 21, 2010 8:38 AM To: r-help Subject: [R] puzzle with integrate over infinite range Dear list, I'm calculating the integral of a Gaussian function from 0 to infinity. I understand from ?integrate that it's usually better to specify Inf explicitly as a limit rather than an arbitrary large number, as in this case integrate() performs a trick to do the integration better. However, I do not understand the following, if I shift the Gauss function by some amount the integral should not be affected, shiftedGauss - function(x0=500){ integrate(function(x) exp(-(x-x0)^2/100^2), 0, Inf)$value } shift - seq(500, 800, by=10) plot(shift, sapply(shift, shiftedGauss)) Suddenly, just after 700, the value of the integral drops to nearly 0 when it should be constant all the way. Any clue as to what's going on here? I guess it's suddenly missing the important part of the range where the integrand is non-zero, but how could this be overcome? Regards, baptiste sessionInfo() R version 2.11.1 (2010-05-31) x86_64-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] inline_0.3.5RcppArmadillo_0.2.6 Rcpp_0.8.6 statmod_1.4.6 loaded via a namespace (and not attached): [1] tools_2.11.1 __ R-help@r-project.org 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@r-project.org 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] HOW to create image like this?
Should anyone feel like reinventing that coloured wheel in R, the arcTextGrob() function in gridExtra answered a more basic query on R-help a few months ago: draw text labels on a circle and connect them with arcs. It might be a starting point. baptiste On Sep 21, 2010, at 3:14 PM, Barry Rowlingson wrote: On Tue, Sep 21, 2010 at 8:58 AM, zcrself zcrs...@gmail.com wrote: http://r.789695.n4.nabble.com/file/n2548152/25jfmyx.jpg HOW to create image like this? **tp://i55.tinypic.com/25jfmyx.jpg[/IMG] My first response is On an empty stomach, with a handy supply of anti-migraine tablets. I don't known how to create the image above or which function can create this image? Is this a standard representation of some genetic thing? If so, then it might be worth someone writing a function to do it, but that would mean first defining a precise specification for the graphic. There's nothing you can't do in R with only the lines and points functions Barry __ R-help@r-project.org 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@r-project.org 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] plot symbol +, but with variable bar lenghts
Hi, The TeachingDemos package has a my.symbols() function that you could use with you own glyph. HTH, baptiste On Sep 9, 2010, at 9:36 AM, (Ted Harding) wrote: On 09-Sep-10 06:41:34, Rainer Machne wrote: Hi, does anybody know of some plotting function or an easy way to generate + symbols with individually settable bar lengths? I tried just combining | and - as pch and setting the size via cex, but that doesn't really work since the two symbols have different default lengths. Is there a horizontal | or a longer - available? Thanks, Rainer I tried this using pch=_ for the horizontal bar, but it is only about half the length of the pch=| bar. However, compared with the same plot using pch=+, at least the resulting cross went through the centre of the + cross. To increase the length of the _ to equal that of the | would require some empirical fiddling with 'cex=... and would increase the thickness of the _. I don't know of any way to create a symbol using drawing commands, and assigning the result to a character which could be evoked using 'pch=...', which would seem to be the sort of thing you would like to be able to do. This could be a useful extension to the plot() function and friends. You can of course define an auxiliary function, say mycross(), on the lines of mycross - function(x,y,L,U,R,D){ lines(c(x,x-L),c(y,y)) lines(c(x,x),c(y,y+U)) lines(c(x,x+R),c(y,y)) lines(c(x,x),c(y,y-D)) } but then you would have to explicitly apply this to the data, rather than delegate it to the plot() function's pch option. Ted. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 09-Sep-10 Time: 08:36:36 -- XFMail -- __ R-help@r-project.org 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@r-project.org 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] large files produced from image plots?
Hi, Have you tried the recent rasterImage() function? HTH, baptiste On Sep 8, 2010, at 7:30 PM, Stephen T. wrote: Hi list, I wonder if anyone has thoughts on making image plots in R [using image() or image.plot(), or filled.contour()]- I've made quite a bit now, but they seem quite large in size when exported to pdf file format (even after compressing with pdftk or ghostscript, which I regularly do). I know that for images, raster graphics output (png, tiff) may be the way to go, but often the ones I make are multi-panel plots with other graphics on them, and are usually included in a LaTeX document (PDFLaTeX does accept png) and require stretching/shrinking (and/or possibly editing with Adobe Illustrator). I have had some luck exporting image plots from Matlab (to postscript or pdf) before in the sense that the files seem smaller and less pixelated. Is this a difference in the way image() plots are produced, or with the way the image is written to the pdf() device (if anyone is familiar with other image-exporting programs...)? The other day I had a 13MB dataset, and probably plotted 3/4 of ! it! using image() and the compressed pdf output was about 8 MB (it contained other stuff but was an addition of a few KB). I tried filled.contour(), as I understand that it colors polygons to fill contours instead of coloring rectangles at each pixel - and it has saved me before - but this time the contours may have been too sharp as as its compressed pdf came out to be 62 MB... (ouch!). I have not tested this data set with other software programs so it may just have been a difficult data set. Is there a good solution to this (or is it simply not to use a vector-graphics format in these instances), and just for my curiosity, are you aware of any things that other software (data analysis) programs do uder the hood to make their exported images smaller/smoother? Thanks much! Stephen [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] export 4D data as povray density files
Thanks – figure 3 is actually pretty close to what I had in mind. I had forgotten about this package. I still hope to work out the povray route as the end result look really good (and that's probably the main point of such graphics). Best, baptiste On Aug 27, 2010, at 12:37 PM, Deepayan Sarkar wrote: On Fri, Aug 27, 2010 at 3:41 PM, baptiste auguie baptiste.aug...@googlemail.com wrote: Dear list, I wish to visualise some 4D data as a kind of colour / translucent cloud in 3D. I haven't seen such plots in R (but perhaps I missed a feature of rgl). The easiest option I found would be to export the data in povray's df3 (density file) format and visualise it with povray. This is not answering your question, but might the misc3d package be relevant for you? http://www.jstatsoft.org/v28/i01/ -Deepayan __ R-help@r-project.org 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] help usin scan on large matrix (caveats to what has been discussed before)
Hi, I don't know if this can be useful to you, but I recently wrote a small function to read a large datafile like yours in a number of steps, with the possibility to save each intermediate block as .Rdata. This is based on read.table --- not as efficient as lower-level scan() but it might be good enough, file - 'test.txt' ## write.table(matrix(rnorm(1e6*14), ncol=14), file=file,row.names = F, ## col.names = F ) n - as.numeric(gsub([^0123456789],, system(paste(wc -l , file), int=TRUE))) n blocks - function(n=18, size=5){ res - c(replicate(n%/%size, size)) if(n%%size) res - c(res, n%%size) if(!sum(res) == n) stop(ERROR!!!) res } ## blocks(1003, 500) readBlocks - function(file, nbk=1e5, out=tmp, save.inter=TRUE, classes= c(numeric, numeric, rep(NULL, 6), numeric, numeric, rep(NULL, 4))){ n - as.numeric(gsub([^0123456789],, system(paste(wc -l , file), int=TRUE))) ncols - length(grep(NULL, classes, invert=TRUE)) results - matrix(0, nrow=n, ncol=ncols) Nb - blocks(n, nbk) skip - c(0, cumsum(Nb)) for(ii in seq_along(Nb)){ d - read.table(file, colClasses = classes, nrows=Nb[ii], skip=skip[ii], comment.char = ) if(save.inter){ save(d, file=paste(out, ., ii, .rda, sep=)) } print(ii) results[seq(1+skip[ii], skip[ii]+Nb[ii]), ] - as.matrix(d) rm(d) ; gc() } save(results, file=paste(out, .rda, sep=)) invisible(results) } ## test - readBlocks(file) HTH, baptiste On Aug 12, 2010, at 1:34 PM, Martin Tomko wrote: Hi Peter, thank you for your reply. I still cannot get it to work. I have modified your code as follows: rows-length(R) cols - max(unlist(lapply(R,function(x) length(unlist(gregexpr( ,x,fixed=TRUE,useBytes=TRUE)) c-scan(file=f,what=rep(c(list(NULL),rep(list(0L),cols-1),rows-1)), skip=1) m-matrix(c, nrow = rows-1, ncol=cols+1,byrow=TRUE); the list c seems ok, with all the values I would expect. Still, length(c) gives me a value = cols+1, which I find odd (I would expect =cols). I thine repeated it rows-1 times (to account for the header row). The values seem ok. Anyway, I tried to construct the matrix, but when I print it, the values are odd: m[1:10,1:10] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [2,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [3,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [4,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [5,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [6,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [7,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [8,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [9,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 [10,] NULL Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Integer,15 Any idea where the values are gone? Thanks Martin Hence, I filled it into the matrix of dimensions On 8/12/2010 12:24 PM, peter dalgaard wrote: On Aug 12, 2010, at 11:30 AM, Martin Tomko wrote: c-scan(file=f,what=list(c(,(rep(integer(0),cols, skip=1) m-matrix(c, nrow = rows, ncol=cols,byrow=TRUE); for some reason I end up with a character matrix, which I don't want. Is this the proper way to skip the first column (this is not documented anywhere - how does one skip the first column in scan???). is my way of specifying integer(0) correct? No. Well, integer(0) is just superfluous where 0L would do, since scan only looks at the types not the contents, but more importantly, what= wants a list of as many elements as there are columns and you gave it list(c(,(rep(integer(0),5 [[1]] [1] I think what you actually meant was c(list(NULL),rep(list(0L),5)) And finally - would any sparse matrix package be more appropriate, and can I use a sparse matrix for the image() function producing typical heat,aps? I have seen that some sparse matrix packages produce different looking outputs, which would not be appropriate. Thanks Martin __ R-help@r-project.org 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. -- Martin Tomko Postdoctoral Research Assistant Geographic Information Systems Division Department of Geography University of Zurich - Irchel Winterthurerstr. 190 CH-8057 Zurich, Switzerland email:martin.to...@geo.uzh.ch site: http://www.geo.uzh.ch/~mtomko mob: +41-788 629 558 tel: +41-44-6355256 fax: +41-44-6356848
[R] FAQ package, anyone?
Dear list, Here's a reproducible example that fails, library(faq) Error in library(faq) : there is no package called 'faq' faq(lattice sweave) Error: could not find function faq As a fun challenge, I propose to remedy this by creating a fortune-like package dedicated to Frequently Asked Questions (FAQ) entries. The aim is multifold, 1- Provide a generic template for use in a variety of situations. I imagine that any package could then define its own FAQ entries and use the faq package to display them, search, etc. 2- Improve the visibility of the current FAQ: currently directing someone to a FAQ entry involves some web-browsing, which is not as convenient as it could be (compare with fortune()). 3- Once stored in a R-like data format, the FAQ could be straight-forwardly converted into a wide variety of output formats, including the original html page (thus made easier to maintain, perhaps). 4- Extend and generalise the fortune package to similar kinds of problems. In practical terms, I can imagine the following distinct steps, - check the FAQ license, and whether we can borrow its content - use some kind of html parsing package (XML?, i forget) - clean up the data and store it as a data.frame or higher class - modify / generalise the fortune functions to deal with the different fields and categories - write output formatting functions et voilà! Comments, contributions, etc. welcome Regards, Baptiste __ R-help@r-project.org 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] Partial Function Application
Hi, I think you mean Currying, there's a function in roxygen that looks like, Curry function (f, ..., .left = TRUE) { .orig = list(...) function(...) { if (.left) { args - c(.orig, list(...)) } else { args - c(list(...), .orig) } do.call(f, args) } } HTH, baptiste On Aug 6, 2010, at 3:56 PM, Mog wrote: Hi. I would like to partially apply a function to a list of arguments, and I don't know how to do this in R, without perhaps writing default values to the formals() of my function, or writing to the environment object of a function. For context, my definition of partially apply is: fix some of the arguments, leaving the others as variables, return a new function that takes the un-fixed arguments from the original function as arguments The issue is that I would like several different sets of arguments for the partially applied phase, and I think that would involve copying the function to several different places with the above methods. Alternately, I could hard-code the partial applications using a function() form, but I would prefer not to do this, as there will be a sizable number of these closures needed and I'd prefer to automate the process. Thanks! --Mog __ R-help@r-project.org 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@r-project.org 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] A %nin% operator?
For curiosity's sake, and perhaps closer in keystrokes to R home, here's another version, `%ni%` - Negate(`%in%`) baptiste On Aug 5, 2010, at 5:30 PM, Ken Williams wrote: Ha! Thanks. I should have a closer look at Hmisc in general. -Ken On 8/5/10 10:25 AM, David Huffer david.huf...@csosa.gov wrote: See Harrell's Hmisc package -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ken Williams Sent: Thursday, August 05, 2010 11:20 AM To: r-help@r-project.org Subject: [R] A %nin% operator? [...] So I write a %nin% operator which I define as: `%nin%` - function (x, table) match(x, table, nomatch = 0L) == 0L -- Ken Williams Sr. Research Scientist Thomson Reuters Phone: 651-848-7712 ken.willi...@thomsonreuters.com __ R-help@r-project.org 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@r-project.org 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] grid.table and expression in table body?
Try this, source(http://gridextra.googlecode.com/svn/trunk/inst/test/expressions.r;) library(grid) e = expression(p[Wilcoxon], *2.2%*%10^{-16}) grid.expr(e) HTH, baptiste On Aug 4, 2010, at 9:56 AM, Johannes Graumann wrote: Hi Baptiste, This is, I fear a bit beyond my level of competency ... What I want to be able to do is things like put 2.2%*%10^{-16} in a table cell, who's name I can already set to p[Wilcoxon] ... Joh On Wednesday 04 August 2010 09:15:43 you wrote: Hi, I don't know the answer to your question (how to make a data.frame with expressions), but if you have a list of expressions you could try the following, http://code.google.com/p/gridextra/wiki/testExpressions I'm open to suggestions for your original query (what is the best way to do it – parse each string and coerce it as an expression?) HTH, baptiste On Aug 4, 2010, at 12:05 AM, Johannes Graumann wrote: Hi, Is there any way to get an expression into a data.frame, such that grid.table from gridExtra will plot it evaluated in the table body? The docu does it for the header, but is the body possible? Thanks, Joh __ R-help@r-project.org 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@r-project.org 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] grid.table and expression in table body?
I added a parse argument to grid.table so that when switched to TRUE (default FALSE) all the text strings are interpreted as expressions (inspired by ggplot2::geom_text), d - data.frame(alpha, beta) grid.table(d, parse=T) you'll need revision 258 of gridExtra for this to work (googlecode now, r-forge in the following days, CRAN in the next stable version). HTH, baptiste On Aug 4, 2010, at 9:56 AM, Johannes Graumann wrote: Hi Baptiste, This is, I fear a bit beyond my level of competency ... What I want to be able to do is things like put 2.2%*%10^{-16} in a table cell, who's name I can already set to p[Wilcoxon] ... Joh On Wednesday 04 August 2010 09:15:43 you wrote: Hi, I don't know the answer to your question (how to make a data.frame with expressions), but if you have a list of expressions you could try the following, http://code.google.com/p/gridextra/wiki/testExpressions I'm open to suggestions for your original query (what is the best way to do it – parse each string and coerce it as an expression?) HTH, baptiste On Aug 4, 2010, at 12:05 AM, Johannes Graumann wrote: Hi, Is there any way to get an expression into a data.frame, such that grid.table from gridExtra will plot it evaluated in the table body? The docu does it for the header, but is the body possible? Thanks, Joh __ R-help@r-project.org 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@r-project.org 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] ggplot2 legend for vertical lines
Dear all, The following example code produces a graph with ggplot2, to which I add several vertical lines of arbitrary colors. I am not satisfied with the legend: it automatically adds some vertical lines which I'd rather not see (they confuse the reader rather than add information in this case). library(ggplot2) dfr - data.frame(values = sin(1:50/10), fact = rep(c(short, long), each=25, length=50), fact2 = rep(1:10, length=50)) p - qplot(fact2, values, data=dfr, colour=fact) p + geom_vline(intercept=c(5), colour = 1) + scale_shape_manual(name=fact, values=1) # tried this, but useless here Any tips on changing these settings would be welcome! Sincerely, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] include S4 class and methods in a package
Thanks Haris, I eventually got this to work thanks to some off-list help and lots of trials and error. I have to admit I still don't understand all the details of the procedure (the generic paradigm in particular escapes me), but at least I have achieved a working example that I can alter and study step by step. Thanks again, Baptiste On 28 Jun 2008, at 19:13, Charilaos Skiadas wrote: On Jun 27, 2008, at 1:44 PM, baptiste Auguié wrote: DeaR list, Pardon the stupidity of this question but I've been trying this for a while now without success. I've followed the example given in the green book programming with data, and I now have a working example of a S4 class with a few methods (plot, summary, as.data.frame). It's all very nice in one file, but I cannot find the way to put this information in a package. I've created several simple packages (without S4 classes) in the past using package.skeleton and manual editing / adding of some files. The question I have now is where do the setClass and setMethod live in the folder tree? I sort of believe they should be defined in a source file in the sub-directory R/, but is there a special name to give to these files, how do they get recognised during installation or build? I've tried to find the answer in writing R extensions but I must have missed the relevant section. I don't think there's any particular name needed for the file. I had a look at Matrix for an example when I was writing an S4 package, and kind of followed what they were doing. In my example, I had one file that had all the setGeneric calls, and another one named after my S4 class containing setClass and setMethod. And then you need to work on the NAMESPACE file. I think to some extent this is described in 1.6.6 Name spaces with formal classes and methods. Hopefully someone more experienced than me can offer a more authoritative answer. Many thanks, Sincerely, Baptiste Haris Skiadas Department of Mathematics and Computer Science Hanover College PS: I'm not sure a minimal example is relevant, I could provide one if requested. OS is MacOS 10.4, sessionInfo() R version 2.7.0 RC (2008-04-21 r45421) powerpc-apple-darwin8.10.1 locale: en_GB.UTF-8/en_US.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] datasets utils stats graphics grDevices splines grid methods base _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] include S4 class and methods in a package
DeaR list, Pardon the stupidity of this question but I've been trying this for a while now without success. I've followed the example given in the green book programming with data, and I now have a working example of a S4 class with a few methods (plot, summary, as.data.frame). It's all very nice in one file, but I cannot find the way to put this information in a package. I've created several simple packages (without S4 classes) in the past using package.skeleton and manual editing / adding of some files. The question I have now is where do the setClass and setMethod live in the folder tree? I sort of believe they should be defined in a source file in the sub-directory R/, but is there a special name to give to these files, how do they get recognised during installation or build? I've tried to find the answer in writing R extensions but I must have missed the relevant section. Many thanks, Sincerely, Baptiste PS: I'm not sure a minimal example is relevant, I could provide one if requested. OS is MacOS 10.4, sessionInfo() R version 2.7.0 RC (2008-04-21 r45421) powerpc-apple-darwin8.10.1 locale: en_GB.UTF-8/en_US.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] datasets utils stats graphics grDevices splines grid methods base _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] expression, strsplit, ...
On 25 Jun 2008, at 19:45, Gabor Grothendieck wrote: Try this: plot(1, xlab = ~ alpha / V * m^-3 * kg ^-2 * l^4) Thanks, I would never have expected this code to work, this is a mystery to me! Actually, I thought xlab wanted an expression, but it seems to be happy with a formula. Also, the handling of exponents is cleverer than I naively assumed. I think an example like the one above would make a nice addition to the plotmath documentation. One small thing bothers me: can one use a similar syntax and use something like bquote to substitute a variable? say, a - some text plot(1, xlab = ~ alpha * `a` / V * m^-3 * kg ^-2 * l^4) to produce, plot(1, xlab = ~ alpha * some text / V * m^-3 * kg ^-2 * l^4) but I can't get either bquote, deparse, substitute, `` to produce the desired substitution in this formula. Many thanks, baptiste On Wed, Jun 25, 2008 at 1:06 PM, baptiste Auguié [EMAIL PROTECTED] wrote: DeaR list, I'm a bit lost in the behavior of substitute and co. I often use fairly long axis labels in my graphs (long to write, that is). Typically, they would contain some greek letters and units with exponents, as in: xlab=expression(paste(text , alpha, / , V,., m^ {-3}, ., kg^{-2}, ., l^{4})) To make this a bit prettier, I've attempted to mimic the behavior of the SIstyle latex package which defines a macro that cleverly parses an expression for units, exponents, etc. My code fails in putting together the unit, as seen in the example below: makeUnits - function(expr){ # formats the units units.list - strsplit(expr, [[:blank:]], perl=F) expr.list - strsplit(unlist(units.list), \\^, perl=T) units - unlist(lapply(expr.list, function(unit) { if (length(unit) == 2) paste(unit[1],^{,unit[2],},sep=) else paste(unit,sep=) })) cat(units, sep=.) } expr - V m^-3 kg^-2 l^4 makeUnits(expr) # this works, and produces: # V.m^{-3}.kg^{-2}.l^{4} silab - function(..., units=NULL){ # creates a valid expression for xlab and co. dotCalls - substitute(list(...)) nArgs - length(dotCalls) if (!is.null(units))myUnits - makeUnits(units) if (!is.null(units)) return(substitute(paste(..., / , myUnits))) if (is.null(units)) return(substitute(paste(...))) } silab(text, alpha, units = V m^-3 kg^-2 l^4) # the result is obviously not what I want par(mfrow=c(2, 1)) # comparison of the desired output and the current one plot(1:10, 1:10, xlab=silab(text , alpha, units = V m^-3 kg^-2 l^4), ylab=silab(simple text)) plot(1:10, 1:10, xlab=expression(paste(text , alpha, / , V,., m^ {-3}, ., kg^{-2}, ., l^{4})), ylab=simple text) Any thoughts welcome! Sincerely, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] expression, strsplit, ...
OK, thanks to both of you for the clarifications. I guess part of my confusion came from the numerous functions and concepts involved in producing such labels: - call vs string vs formula vs expression ... - substitute, bquote, expression, ~, .(), ... I take it as a good thing once you have some experience, as it probably gives several ways to cat a string. The expense of clarity in the use of ~ was useful for me complaining about the rather verbose syntax of some plotmath examples. I may try to produce some examples in the future for the R wiki to illustrate these particular annotation details. Thanks again, Baptiste On 26 Jun 2008, at 12:07, Prof Brian Ripley wrote: On Thu, 26 Jun 2008, baptiste Auguié wrote: On 25 Jun 2008, at 19:45, Gabor Grothendieck wrote: Try this: plot(1, xlab = ~ alpha / V * m^-3 * kg ^-2 * l^4) Thanks, I would never have expected this code to work, this is a mystery to me! Actually, I thought xlab wanted an expression, but it seems to be happy with a formula. From ?plotmath In most cases other language objects (names and calls) are coerced to expressions and so can also be used. A formula is a call: mode(~ alpha / V * m^-3 * kg ^-2 * l^4) [1] call That was just GG saving himself a few key strokes at the expense of clarity: plot(1, xlab = expression(alpha / V * m^-3 * kg ^-2 * l^4)) is what this is coerced to. Also, the handling of exponents is cleverer than I naively assumed. It is exactly as documented. I think an example like the one above would make a nice addition to the plotmath documentation. I don't see what it adds to those already there. One small thing bothers me: can one use a similar syntax and use something like bquote to substitute a variable? You use substitute() to do that: substitute(alpha * aaa / V * m^-3 * kg ^-2 * l^4, list(aaa=a)) alpha * some text /V * m^-3 * kg^-2 * l^4 say, a - some text plot(1, xlab = ~ alpha * `a` / V * m^-3 * kg ^-2 * l^4) to produce, plot(1, xlab = ~ alpha * some text / V * m^-3 * kg ^-2 * l^4) but I can't get either bquote, deparse, substitute, `` to produce the desired substitution in this formula. You can't 'substitute' in a formula, but you can in the RHS of a formula, form[[2]] in your case. Many thanks, baptiste On Wed, Jun 25, 2008 at 1:06 PM, baptiste Auguié [EMAIL PROTECTED] wrote: DeaR list, I'm a bit lost in the behavior of substitute and co. I often use fairly long axis labels in my graphs (long to write, that is). Typically, they would contain some greek letters and units with exponents, as in: xlab=expression(paste(text , alpha, / , V,., m^ {-3}, ., kg^{-2}, ., l^{4})) To make this a bit prettier, I've attempted to mimic the behavior of the SIstyle latex package which defines a macro that cleverly parses an expression for units, exponents, etc. My code fails in putting together the unit, as seen in the example below: makeUnits - function(expr){ # formats the units units.list - strsplit(expr, [[:blank:]], perl=F) expr.list - strsplit(unlist(units.list), \\^, perl=T) units - unlist(lapply(expr.list, function(unit) { if (length(unit) == 2) paste(unit[1],^{,unit[2],},sep=) else paste(unit,sep=) })) cat(units, sep=.) } expr - V m^-3 kg^-2 l^4 makeUnits(expr) # this works, and produces: # V.m^{-3}.kg^{-2}.l^{4} silab - function(..., units=NULL){ # creates a valid expression for xlab and co. dotCalls - substitute(list(...)) nArgs - length(dotCalls) if (!is.null(units))myUnits - makeUnits(units) if (!is.null(units)) return(substitute(paste(..., / , myUnits))) if (is.null(units)) return(substitute(paste(...))) } silab(text, alpha, units = V m^-3 kg^-2 l^4) # the result is obviously not what I want par(mfrow=c(2, 1)) # comparison of the desired output and the current one plot(1:10, 1:10, xlab=silab(text , alpha, units = V m^-3 kg^-2 l^4), ylab=silab(simple text)) plot(1:10, 1:10, xlab=expression(paste(text , alpha, / , V,., m^ {-3}, ., kg^{-2}, ., l^{4})), ylab=simple text) Any thoughts welcome! Sincerely, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org
[R] expression, strsplit, ...
DeaR list, I'm a bit lost in the behavior of substitute and co. I often use fairly long axis labels in my graphs (long to write, that is). Typically, they would contain some greek letters and units with exponents, as in: xlab=expression(paste(text , alpha, / , V,., m^{-3}, ., kg^{-2}, ., l^{4})) To make this a bit prettier, I've attempted to mimic the behavior of the SIstyle latex package which defines a macro that cleverly parses an expression for units, exponents, etc. My code fails in putting together the unit, as seen in the example below: makeUnits - function(expr){ # formats the units units.list - strsplit(expr, [[:blank:]], perl=F) expr.list - strsplit(unlist(units.list), \\^, perl=T) units - unlist(lapply(expr.list, function(unit) { if (length(unit) == 2) paste(unit[1],^{,unit[2],},sep=) else paste(unit,sep=) })) cat(units, sep=.) } expr - V m^-3 kg^-2 l^4 makeUnits(expr) # this works, and produces: # V.m^{-3}.kg^{-2}.l^{4} silab - function(..., units=NULL){ # creates a valid expression for xlab and co. dotCalls - substitute(list(...)) nArgs - length(dotCalls) if (!is.null(units))myUnits - makeUnits(units) if (!is.null(units)) return(substitute(paste(..., / , myUnits))) if (is.null(units)) return(substitute(paste(...))) } silab(text, alpha, units = V m^-3 kg^-2 l^4) # the result is obviously not what I want par(mfrow=c(2, 1)) # comparison of the desired output and the current one plot(1:10, 1:10, xlab=silab(text , alpha, units = V m^-3 kg^-2 l^4), ylab=silab(simple text)) plot(1:10, 1:10, xlab=expression(paste(text , alpha, / , V,., m^{-3}, ., kg^{-2}, ., l^{4})), ylab=simple text) Any thoughts welcome! Sincerely, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] highest eigenvalues of a matrix
Hi, On 18 Jun 2008, at 23:52, Moshe Olshansky wrote: Hi Baptiste, If the rank of your matrix is 3 than all the eigenvalues, except for 3, are 0. Good point! I obviously mixed up the rank of the matrix with the degeneracy (multiplicity) of its eigenvalues. What I meant is that out of 200 eigenvalues returned by eigen(), only 2 or 3 are noticeably different and these are the ones I am looking for. None are 0, which means the rank is 200. The high degeneracy comes from a very high symmetry in the particular problem, so most eigenvectors will describe the same state, I think. Sorry for the confusion, I hope this makes sense now. Baptiste How do you know that the rank is so low? If your matrix is A and it is of order N (NxN) and it's rank is 3 then there exist matrices B and C such that B is Nx3 and C is 3xN and A = B*C (if A is symmetric then C = t(B)). Can you get that representation? If yes then any non-zero eigenvector of A is a linear combination of columns of B and your problem can be reduced to finding eigenvalues and eigenvectors of a 3x3 matrix, which is trivial. Regards, Moshe. --- On Thu, 19/6/08, baptiste Auguié [EMAIL PROTECTED] wrote: From: baptiste Auguié [EMAIL PROTECTED] Subject: [R] highest eigenvalues of a matrix To: R Help [EMAIL PROTECTED] Received: Thursday, 19 June, 2008, 6:56 AM DeaR list, I happily use eigen() to compute the eigenvalues and eigenvectors of a fairly large matrix (200x200, say), but it seems over-killed as its rank is limited to typically 2 or 3. I sort of remember being taught that numerical techniques can find iteratively decreasing eigenvalues and corresponding orthogonal eigenvectors, which would provide a nice alternative (once I have the first 3, say, I stop the search). Looking at the R source code for eigen and some posts on this list, it seems that the function uses a LAPACK routine, but obviously all the options are not available through the R wrapper. I'm not experienced enough to try and make my own interface with Fortran code, so here are two questions: - is this option (choosing a desired number of eigenvectors) already implemented in some function / package that I missed? - is the range of indices option in DSYEVR.f http:// www.netlib.org/lapack/double/dsyevr.f what I think, the indices of the desired eigenvalues ordered from the highest to lowest? Many thanks in advance for any piece of advice, Sincerely, Baptiste dummy example if needed: test - matrix(c(1, 2, 0, 4, 5, 6, 1.1, 2, 0), ncol=3) eigen(test) _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] highest eigenvalues of a matrix
Dear all, Thank you for the suggestions and pointers. It looks like I'll need to do some interface with Fortran/C code. The igraph package seems to provide an interface with ARPACK, albeit not with the options I need, so it could be a good starting point. Best regards, baptiste On 19 Jun 2008, at 10:50, Katharine Mullen wrote: On Thu, 19 Jun 2008, Simon Wood wrote: I happily use eigen() to compute the eigenvalues and eigenvectors of a fairly large matrix (200x200, say), but it seems over-killed as its rank is limited to typically 2 or 3. I sort of remember being taught that numerical techniques can find iteratively decreasing eigenvalues and corresponding orthogonal eigenvectors, which would provide a nice alternative (once I have the first 3, say, I stop the search). Lanczos iteration will do this efficiently (see e.g. Golub van Loan Matrix Computations), but I don't think that there are any such routines built into R or LAPACK (although I haven't checked the latest LAPACK release). When I looked it seemed that the LAPACK options that allow you to select eigen-values/vectors still depend on an initial O(n^3) decomposition of the matrix, rather than the O(n^2) that a Lanczos based method would require. ARPACK (http://www.caam.rice.edu/software/ARPACK/) uses a Lanczos method for symmetric matrics; otherwise it uses an Arnoldi iteration. Development of an R interface to ARPACK would be a nice project (but unfortunately one I don't have time for for a while). Maybe one of the maintainers of a package for sparse matrices would be interested. My `mgcv' package (see cran) uses Lanczos iteration for setting up low rank bases for smoothing. The source code is in mgcv/src/ matrix.c:lanczos_spd, but I'm afraid that there is no direct R interface, although it would not be too hard to write a suitable wrapper. It requires the matrix to be symmetric. Looking at the R source code for eigen and some posts on this list, it seems that the function uses a LAPACK routine, but obviously all the options are not available through the R wrapper. I'm not experienced enough to try and make my own interface with Fortran code, so here are two questions: - is this option (choosing a desired number of eigenvectors) already implemented in some function / package that I missed? --- In the symmetric case you can use `svd' which lets you select (although you'd need to fix up the signs of the singular values to get eigen- values if the matrix is not +ve definite). But this answer is pretty useless as it will be slower than using `eigen' and getting the full decomposition. Of course if you know that your matrix is low rank because it's a product of non-square matrices then there's usually some way of getting at the eigen-decomposition efficiently. E.g. if A=B'B where B is 3 by 1000, then the cost can easily be kept down to O(1000^2) in R... best, Simon - is the range of indices option in DSYEVR.f http:// www.netlib.org/lapack/double/dsyevr.f what I think, the indices of the desired eigenvalues ordered from the highest to lowest? dsyevr.f will work with symmetric real matrices only. When the range argument of dysevr is set to 'I', arguments il and iu seem to specify the range of eigenvalue indices you want in ascending order (lowest to highest, not highest to lowest). If you look at https://svn.r-project.org/R/trunk/src/modules/lapack/Lapack.c you see that range is always set to 'A'. Many thanks in advance for any piece of advice, Sincerely, Baptiste dummy example if needed: test - matrix(c(1, 2, 0, 4, 5, 6, 1.1, 2, 0), ncol=3) eigen(test) _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. -- Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK +44 1225 386603 www.maths.bath.ac.uk/~sw283 __ R-help@r-project.org 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@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392
[R] highest eigenvalues of a matrix
DeaR list, I happily use eigen() to compute the eigenvalues and eigenvectors of a fairly large matrix (200x200, say), but it seems over-killed as its rank is limited to typically 2 or 3. I sort of remember being taught that numerical techniques can find iteratively decreasing eigenvalues and corresponding orthogonal eigenvectors, which would provide a nice alternative (once I have the first 3, say, I stop the search). Looking at the R source code for eigen and some posts on this list, it seems that the function uses a LAPACK routine, but obviously all the options are not available through the R wrapper. I'm not experienced enough to try and make my own interface with Fortran code, so here are two questions: - is this option (choosing a desired number of eigenvectors) already implemented in some function / package that I missed? - is the range of indices option in DSYEVR.f http:// www.netlib.org/lapack/double/dsyevr.f what I think, the indices of the desired eigenvalues ordered from the highest to lowest? Many thanks in advance for any piece of advice, Sincerely, Baptiste dummy example if needed: test - matrix(c(1, 2, 0, 4, 5, 6, 1.1, 2, 0), ncol=3) eigen(test) _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] apply in apply
Dear list, I need to apply a function on each column of each matrix contained in a list. Consider the following code, x - 1:3 my.data - list(matrix(c(1,2,3,4,5,6),ncol=2), matrix(c(4,5,6,7,8,9),ncol=2)) par(mfrow=c(2,2)) results - sapply(1:length(my.data), function(ii) apply(my.data[[ii]], 2, function(y) plot(x,y) )) # plot is for demonstration purposes It works, but I think this is quite dirty code. Is there a simpler way of achieving this? I was considering recasting the list into a matrix, apply the function to all its columns, and then reshape the result into an array of adequate dimensions, as in: results - sapply(matrix(unlist(my.data),ncol= 2 * length (my.data)), function(y) plot(x,y)) #results - matrix(results, ncol = length(my.data)) # when the actual function is used instead of plot but again it feels a bit twisted. Best regards, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] apply in apply
Thank you for the suggestions (off-list as well). I think the best option may eventually be an explicit for loop to make things clearer. To clarify a bit, I've used the plot function in the example where in fact it is a numerical integration (which is why I need to pass an additional variable in the second apply call), intg - function (y, x) { n - length(x) index - order(x) dx - diff(sort(x)) z - y[index] ys - (z[1:(n - 1)] + z[2:n])/2 sum(ys * dx) } environment: namespace:PROcess Thanks again for the suggestions, baptiste On 30 May 2008, at 10:02, [EMAIL PROTECTED] wrote: I need to apply a function on each column of each matrix contained in a list. Consider the following code, x - 1:3 my.data - list(matrix(c(1,2,3,4,5,6),ncol=2), matrix(c(4,5,6,7,8,9),ncol=2)) par(mfrow=c(2,2)) results - sapply(1:length(my.data), function(ii) apply(my.data[[ii]], 2, function(y) plot (x,y) )) # plot is for demonstration purposes It works, but I think this is quite dirty code. Is there a simpler way of achieving this? The last line can be simplified results - sapply(my.data, function(x) apply(x,2,sum)) (It is perhaps a little clearer what is going on when you use sum rather than plot as the example function.) Regards, Richie. Mathematical Sciences Unit HSL -- -- ATTENTION: This message contains privileged and confidential info...{{dropped:30}} __ R-help@r-project.org 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] apply in apply
Very nice, i really like this one! May confuse non-R users, but that's not a concern here. Thanks a lot, baptiste On 30 May 2008, at 13:19, Charilaos Skiadas wrote: I think this is where the beauty of ... comes in, the following should be doing just what you want: sapply(my.data, apply, 2, intg, x) __ R-help@r-project.org 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] [slightly off topic] Sweave with markdown
Thank you all, I think I have a good list of options now. The best suited to my personal taste seems to be the Brew package, which I completely overlooked until I looked at the source examples. If I can persuade my text editor (textmate) to recognize the markup and also execute the R chunks, I'll have my dream workflow: a single text file with both text (in a lightweight, email friendly markup), and the R commands together. The MultiMarkdown extension allows me to convert the brewed file into whatever output one could wish for: LaTeX, Html, pdf, rtf,... I'll probably be using the html conversion for routine work, and the LaTeX one when a clean printed version is needed. The use of dev.copy or some similar command should make easy the creation of both bitmap and vector versions of the graphs if needed. Thanks again everyone! baptiste On 23 May 2008, at 13:30, Gabor Grothendieck wrote: You could check out the brew package: https://stat.ethz.ch/pipermail/r-packages/2007/000327.html On Fri, May 23, 2008 at 4:37 AM, baptiste Auguié [EMAIL PROTECTED] wrote: DeaR list, Has anyone tried to mix the Sweave paradigm with the Markdown[*] (and co.) syntax? Would this be hard to implement? My tiny understanding of Sweave is that one can define new drivers for the text part, while some functions that deal with the R code would not require any modification. Here's the reason I'm interested in Mardown for a driver. I've been orbiting around Sweave for several weeks, and while I understand the great value of this literate programming, I'm a bit put off by the technical aspect. I'm a LateX user, and a R user (you'd have guessed, albeit fairly novice), so the problem is not to get it working (i got some writings done with Sweave), but more of getting an efficient workflow. I find the LaTeX commands overly intruding in the middle of my R source code, plus the relatively slow compilation of latex makes it quite impractical for quick studies ( I once spent 20 minutes trying to get the graph to be at a sensible scale on the page). As an alternative, I'm resorting to having a folder per study (I'm physicist, doing data analysis for optical spectroscopy), with one R file per analysis and a text file to keep track of the experimental conditions. When the data is really important / interesting, I've also tried to write a package: great for storing the data, functions and commands in a consistent manner (this works fine, but it cannot be a solution for quick trial experiments. Also, the figures cannot be included in the html doc as far as I know.) I was considering some alternatives to Sweave, namely R2HTML and odfWeave, but in the former the source syntax is no less disturbing than LaTeX (although the zero compilation time is a plus), while for the latter I do not have a decent compatible editor (on a Mac, I tried Openoffice and Abiword but the fonts look like my handwriting for some obscure reason). Maybe I'll give it another shot at some stage, I just usually prefer plain text files. Any input welcome, Baptiste [*]: http://en.wikipedia.org/wiki/Markdown _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] [slightly off topic] Sweave with markdown
DeaR list, Has anyone tried to mix the Sweave paradigm with the Markdown[*] (and co.) syntax? Would this be hard to implement? My tiny understanding of Sweave is that one can define new drivers for the text part, while some functions that deal with the R code would not require any modification. Here's the reason I'm interested in Mardown for a driver. I've been orbiting around Sweave for several weeks, and while I understand the great value of this literate programming, I'm a bit put off by the technical aspect. I'm a LateX user, and a R user (you'd have guessed, albeit fairly novice), so the problem is not to get it working (i got some writings done with Sweave), but more of getting an efficient workflow. I find the LaTeX commands overly intruding in the middle of my R source code, plus the relatively slow compilation of latex makes it quite impractical for quick studies ( I once spent 20 minutes trying to get the graph to be at a sensible scale on the page). As an alternative, I'm resorting to having a folder per study (I'm physicist, doing data analysis for optical spectroscopy), with one R file per analysis and a text file to keep track of the experimental conditions. When the data is really important / interesting, I've also tried to write a package: great for storing the data, functions and commands in a consistent manner (this works fine, but it cannot be a solution for quick trial experiments. Also, the figures cannot be included in the html doc as far as I know.) I was considering some alternatives to Sweave, namely R2HTML and odfWeave, but in the former the source syntax is no less disturbing than LaTeX (although the zero compilation time is a plus), while for the latter I do not have a decent compatible editor (on a Mac, I tried Openoffice and Abiword but the fonts look like my handwriting for some obscure reason). Maybe I'll give it another shot at some stage, I just usually prefer plain text files. Any input welcome, Baptiste [*]: http://en.wikipedia.org/wiki/Markdown _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] generating help files
Hi, try prompt(yourFunction) hope thus helps, baptiste On 21 May 2008, at 21:40, Erin Hodgess wrote: Hi R People! Is there a quick way to generate help files for functions, please? I know that if you use package.skeleton, that will work. However, I'm not using that for a new package. (Hope this make sense) this is for SUSE10.1, please. thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: [EMAIL PROTECTED] __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] adaptive optimization of mesh size
my post may have slipped through the bank holiday and be forgotten by now, I'm still hoping for some pointers. Please let me know if I need to clarify the problem. baptiste On 4 May 2008, at 16:39, baptiste Auguié wrote: DeaR list, I'm running an external program that computes some electromagnetic response of a scattering body. The numerical scheme is based on a discretization with a characteristic mesh size y. The smaller y is, the better the result (but obviously the computation will take longer). A convergence study showed the error between the computed values and the exact solution of the problem to be a quadratic in y, with standard error increasing as y^3. I wrote the interface to the program in R, as it is much more user friendly and allows for post- processing analysis. Currently, it only runs with user-defined discretization parameter. I would like to implement an adaptive scheme [1] and provide the following improvements, 1) obtain an estimate of the error by fitting the result against a series of mesh sizes with the quadratic model, and extrapolate at y = 0. (quite straight forward) 2) adapt dynamically the set of mesh sizes to fulfill a final accuracy condition, between a starting value (a rule-of thumb estimate is given by the problem values). The lower limit of y should also be constrained by the resources (again, an empirical rule dictates the computation time and memory usage). I'm looking for advice on this second point (both on the technical aspect, and whether this is sound statistically): - I can foresee that I should always start with a few y values before I can do any extrapolation, but how many of them? 3, 10? How could I know? - once I have enough points (say, 10) to use the fitting procedure and get an estimate of the error, how should I decide the best location of the next y if the error is too important? - in a practical implementation, I would use a while loop and append the successive values to a data.frame(y, value). However, this procedure will be run for different parameters (wavelengths, actually), so the set and number of y values may vary between one run and another. I think I'd be better off using a list with each new run having its own data.frame. Does this make sense? Below are a few lines of code to illustrate the problem, program.result - function(x, p){ # made up function that mimicks the results of the real program y - p[3]*x^2 + p[2]*x + p[1] y * (1 + rnorm(1, mean=0, sd = 0.1 * y^3)) } p0 - c(0.1, 0.1, 2) # set of parameters ## user defined limits of the y parameter (log scale) limits - c(0.1, 0.8) limits.log - (10^limits) y.log - seq(limits.log[1], limits.log[2], l=10) y - log10(y.log) result - sapply(y, function(x) program.result(x, p0)) # results of the program fitting and extrapolation procedure library(gplots) # plot with CI plotCI(y, result, y^3, xlim=c(0, 1), ylim=c(0, 2)) # the data with y^3 errors my.data - data.frame(y = y, value = result) fm - lm(value ~ poly(y, degree=2, raw=TRUE), data = my.data , weights = 1/y^3) lines(y, predict(fm, data.frame(y=y)), col = 2) extrap - summary(fm)$coefficients[1,] # intercept and error on it plotCI(0,extrap[1], 2 * extrap[2], col = 2, add=T) ### my naive take on adaptive runs... ## objective - 1e-3 # stop when the standard error of the extrapolated value is smaller than this err - extrap[2] my.color - 3 while (err objective){ new.value - min(y)/2 # i don't know how to choose this optimally y - c(new.value, y) new.result - program.result(new.value, p0) result - c(new.result, result) points(new.value, new.result, col= my.color) my.data - data.frame(y = y, value = result) fm - lm(value ~ poly(y, degree=2, raw=TRUE), data = my.data , weights = 1/y^3) lines(y, predict(fm, data.frame(y=y)), col = my.color) extrap - summary(fm)$coefficients[1,] # intercept and error on it err - extrap[2] print(err) plotCI(0,extrap[1], 2 * err, col = 2, add=T) my.color - my.color + 1 } err Many thanks in advance for your comments, baptiste [1]: Yurkin et al., Convergence of the discrete dipole approximation. II. An extrapolation technique to increase the accuracy. J. Opt. Soc. Am. A / Vol. 23, No. 10 / October 2006 _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org
[R] adaptive optimization of mesh size
DeaR list, I'm running an external program that computes some electromagnetic response of a scattering body. The numerical scheme is based on a discretization with a characteristic mesh size y. The smaller y is, the better the result (but obviously the computation will take longer). A convergence study showed the error between the computed values and the exact solution of the problem to be a quadratic in y, with standard error increasing as y^3. I wrote the interface to the program in R, as it is much more user friendly and allows for post- processing analysis. Currently, it only runs with user-defined discretization parameter. I would like to implement an adaptive scheme [1] and provide the following improvements, 1) obtain an estimate of the error by fitting the result against a series of mesh sizes with the quadratic model, and extrapolate at y = 0. (quite straight forward) 2) adapt dynamically the set of mesh sizes to fulfill a final accuracy condition, between a starting value (a rule-of thumb estimate is given by the problem values). The lower limit of y should also be constrained by the resources (again, an empirical rule dictates the computation time and memory usage). I'm looking for advice on this second point (both on the technical aspect, and whether this is sound statistically): - I can foresee that I should always start with a few y values before I can do any extrapolation, but how many of them? 3, 10? How could I know? - once I have enough points (say, 10) to use the fitting procedure and get an estimate of the error, how should I decide the best location of the next y if the error is too important? - in a practical implementation, I would use a while loop and append the successive values to a data.frame(y, value). However, this procedure will be run for different parameters (wavelengths, actually), so the set and number of y values may vary between one run and another. I think I'd be better off using a list with each new run having its own data.frame. Does this make sense? Below are a few lines of code to illustrate the problem, program.result - function(x, p){ # made up function that mimicks the results of the real program y - p[3]*x^2 + p[2]*x + p[1] y * (1 + rnorm(1, mean=0, sd = 0.1 * y^3)) } p0 - c(0.1, 0.1, 2) # set of parameters ## user defined limits of the y parameter (log scale) limits - c(0.1, 0.8) limits.log - (10^limits) y.log - seq(limits.log[1], limits.log[2], l=10) y - log10(y.log) result - sapply(y, function(x) program.result(x, p0)) # results of the program fitting and extrapolation procedure library(gplots) # plot with CI plotCI(y, result, y^3, xlim=c(0, 1), ylim=c(0, 2)) # the data with y^3 errors my.data - data.frame(y = y, value = result) fm - lm(value ~ poly(y, degree=2, raw=TRUE), data = my.data , weights = 1/y^3) lines(y, predict(fm, data.frame(y=y)), col = 2) extrap - summary(fm)$coefficients[1,] # intercept and error on it plotCI(0,extrap[1], 2 * extrap[2], col = 2, add=T) ### my naive take on adaptive runs... ## objective - 1e-3 # stop when the standard error of the extrapolated value is smaller than this err - extrap[2] my.color - 3 while (err objective){ new.value - min(y)/2 # i don't know how to choose this optimally y - c(new.value, y) new.result - program.result(new.value, p0) result - c(new.result, result) points(new.value, new.result, col= my.color) my.data - data.frame(y = y, value = result) fm - lm(value ~ poly(y, degree=2, raw=TRUE), data = my.data , weights = 1/y^3) lines(y, predict(fm, data.frame(y=y)), col = my.color) extrap - summary(fm)$coefficients[1,] # intercept and error on it err - extrap[2] print(err) plotCI(0,extrap[1], 2 * err, col = 2, add=T) my.color - my.color + 1 } err Many thanks in advance for your comments, baptiste [1]: Yurkin et al., Convergence of the discrete dipole approximation. II. An extrapolation technique to increase the accuracy. J. Opt. Soc. Am. A / Vol. 23, No. 10 / October 2006 _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] quasi-random sequences
)) # cost } optim(p0,obj) # fails miserably, but this does not sound right anyway _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] Documentation General Comments
I believe in the great value of a Wiki for documentation purposes. Perhaps one could imagine more discussions on this mailing list being turned into a wiki entry? I would rather like that personally: the information, references, and possible solutions to a problem could all be summarized in one page, with working examples, links and graphics. This format would have some benefits over the mailing list archives (no attachments, several pages, off-topic remarks,...), and would still allow for multiple users contributions. This may provide the right trade-off between structure and content for an additional help resource. Best regards, baptiste On 25 Apr 2008, at 15:53, Tubin wrote: I realize the R developers are probably overwhelmed and have little time for this, but the documentation really needs some serious reorganizaton. My reply: I'm quite new to R and so have spent a lot of time in the last few days reading documentation both online and text. I'm incredibly impressed by the language and, overall, by the documentation - the documentation tools are so well integrated into the program, and in most cases the documentation does a good job of balancing brevity and completeness. The original poster noted a need for reorganization (not revision) and I think that's key. Several people have posted about potential projects in several threads - so I'll try combining those thoughts in a new response thread. Summary: 1) New users have questions based on common tasks, which don't necessarily reflect the structure of the language 2) There's a lot of good documentation out there already, both official and unofficial 3) It doesn't break intellectual property laws to create an annotated bibliography of existing references, with a task-based topic structure. 4) I suspect new and old users would contribute to such a project if it allowed external contributions - as in, suggest a resource (under an existing topic) along with a brief explanation about why that resource was helpful. Detailed comments: One thing I notice in the official documentation is that it's organized around the R language structure. The help table of contents does include a listing of natural-language titles, but they're listed alphabetically rather than organized by task-based concept. But when you look at the forums, the new users are searching for information by task or by concept - what are the classes of object? How do I manipulate a data frame? What are my tools for regular expressions? Many of my texts (and some independent web pages) seem to try to organize by task, but all are incomplete - perhaps because of space limitations, perhaps because the language is so dynamic that useful functions hadn't yet been developed when a text was published. Also, most of my books actually only introduce the R language then move on to discussing the use of R for specific statistical functions. Yet, most of the forum questions seem to be about things like loading and manipulating the data to get it into the appropriate format for the desired analysis. So I really like the suggestion to try creating some cross-indexing for the materials that are out there already. Perhaps a wiki-based annotated bibliography with a task-based structure. I'm tempted to suggest that we just expand on the wikipedia content for R! The structure of such a thing should have categories like managing regular expressions or manipulating data in dataframes - and often a particular topic might be cross-referenced to more than one category, I'd think. By annotated bibliography I mean that most of the entries under a topic would be here's a link or reference to a source that seems to explain this topic well - and here's why I like it. And - I have to run to a meeting. So I will stop brainstorming now. -- View this message in context: http://www.nabble.com/Documentation- General-Comments-tp16821085p16895859.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] quasi-random sequences
Dear list useRs, I have to generate a random set of coordinates (x,y) in [-1 ; 1]^2 for say, N points. At each of these points is drawn a circle (later on, an ellipse) of random size, as in: N - 100 positions - matrix(rnorm(2 * N, mean = 0 , sd= 0.5), nrow=N) sizes-rnorm(N, mean = 0 , sd= 1) plot(positions,type=p,cex=sizes) My problem is to avoid collisions (overlap, really) between the points. I would like some random pattern, but with a minimum exclusion distance. In looking up Numerical recipes in C, I found out about some Sobol quasi-random sequences, which one can call from the gsl package, library(gsl) g - qrng_alloc(type=sobol,dim=2) qrng_get(g,n= N) -xy plot((xy),t=p,cex=0.5) but this does not look very random: I clearly see some pattern (diagonals, etc...), and even the non-overlapping condition is not impressive. One (painful) way I can foresee is to check the distance between each symbol and the others, and move the overlapping ones in a recursive manner. Before delving into this, I wanted to check I'm not overlooking something in the rgl quasi-random sequences, or missing a more obvious way to generate such patterns. Perhaps solving an electrostatic problem with a potential both attractive at long distances and repulsive at short distances is a better way? I have a vague recollection of hearing that somewhere to position points evenly on a sphere. Thanks for any comment / suggestion, Baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] quasi-random sequences
Thank you all for the great suggestions and comments. As two of you pointed out, the problem was not well defined (who said a well-posed problem is a problem solved?), and also it seems to be a very wide topic. I've had an interesting reading discussing the similarities between half-toning in black and white printing, and the quasi Monte Carlo integration technique [1]. I'm trying the suggested ways plus a few other things, I'll put together a summary of my discoveries when I've clarified it all. Thanks again, baptiste [1] Halftoning and Quasi-Monte Carlo, K. M. Hanson, Sensitivity Analysis of Model Output, K. M. Hanson and F. M. Hemez, eds., pp. 430-442 (Los Alamos Research Library, 2005) _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] Documentation General Comments
AM Internal Virus Database is out-of-date. Checked by AVG Free Edition. 11:27 AM __ R-help@r-project.org 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. ## Attention:\ This e-mail message is privileged and confid... {{dropped:9}} __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] Suggestions: Terminology Pkgs for following spectra over time
Hello, I've found the function isPeak from the Bioconductor package PROCess very useful for peak finding. My data was similar to yours (spectroscopy, i'm a physicist) but not time dependent. It may be a starting point to work on one spectrum, retrieve a few relevant informations (peak positions, widths, intensities,...), and process similarly at regular time intervals. www.bioconductor.org/packages/2.0/ bioc/vignettes/PROcess/inst/doc/ howtoprocess.pdf hope this helps, baptiste On 17 Apr 2008, at 02:26, Bryan Hanson wrote: Hi Folks... No code to troubleshoot here. I need some suggestions about the right terminology to use in further searching, and any suggestions about R pkgs that might be appropriate. I am in the planning stages of a project in which IR, NMR and other spectra (I'm a chemist) would be collected on various samples, and individual samples would be followed over time. The spectra will be feature rich/complex, so one can't see the changes by visual inspection. The spectra are basically 2D matrices: peaks as a function of frequencies. So the data set is in the form of spectra of a single sample over time, for multiple samples. I am wondering about methods R pkgs that can be used to analyze changes in the spectra over time. For instance, I would like to find specific peaks that are changing over time, sets of peaks that are changing in a correlated way over time etc. I'd like to do this in an efficient and statistically valid way. What I am thinking of is somewhat like a time series, somewhat like image analysis (but only 2D), but it's not quite either of those and I need to know what it's really called to investigate further. Any suggestions as to R pkgs and key words/phrases will be appreciated. TIA, Bryan * Bryan Hanson Professor of Chemistry Biochemistry DePauw University, Greencastle Indiana USA __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] lm() of one matrix against another
Thanks for both replies, here is the cleaner version, n-5 N-10 data.x-matrix(1:(n*N),ncol=n) data.y-matrix(1:(n*N) + rnorm(n*N,sd=1),ncol=n) matplot(data.x,data.y,t=p,pch=1:n,bty=n) mapply(function(x,y){res - lm(y~x) ; lines(x,predict(res)) ; res} , as.data.frame(data.x),as.data.frame(data.y),SIMPLIFY=F) Out of curiosity, I'm not sure if one could get the lines colored corresponding to their index within mapply (or any function of the apply family for that matter). Is there a special trick to refer to the index under evaluation? Thanks again, baptiste On 12 Apr 2008, at 20:08, Henrique Dallazuanna wrote: Try mapply(function(x,y)lm(y~x),as.data .frame(data.x),as.data.frame(data.y),SIMPLIFY=F) 2008/4/12, baptiste Auguié [EMAIL PROTECTED]: Hello R list, I have two matrices of identical dimensions, and I want to fit a straight line for each pair of columns and plot the resulting lines. I got it to work with a for loop, but there must be a better way, n-5 N-10 data.x-matrix(1:(n*N),ncol=n) data.y-matrix(1:(n*N) + rnorm(n*N,sd=1),ncol=n) matplot(data.x,data.y,t=p,pch=1:n,bty=n) for (ii in 1:n) { test - lm(y~x,data=list(x=data.x[,ii],y=data.y[,ii]) ) lines(data.x[,ii],test$coefficients[1] + test$coefficients[2] * data.x[,ii],lty=2,col=ii) } Thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] lm() of one matrix against another
Hello R list, I have two matrices of identical dimensions, and I want to fit a straight line for each pair of columns and plot the resulting lines. I got it to work with a for loop, but there must be a better way, n-5 N-10 data.x-matrix(1:(n*N),ncol=n) data.y-matrix(1:(n*N) + rnorm(n*N,sd=1),ncol=n) matplot(data.x,data.y,t=p,pch=1:n,bty=n) for (ii in 1:n) { test - lm(y~x,data=list(x=data.x[,ii],y=data.y[,ii]) ) lines(data.x[,ii],test$coefficients[1] + test$coefficients[2] * data.x[,ii],lty=2,col=ii) } Thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] data.frame or list
Hi again, Is this approach correct, or should I rather use only one type of factors (combined)? I must admit I am a bit confused by the different options to store and manipulate data in R. As I understand, data.frames are the preferred format, but I can't see how to frame this example data in it (hence my resort to a list). Any advice welcome (or reference). baptiste PS: off topic, why would it be that i receive messages from this list but cannot anymore post without some approval being required? This is probably a weird technical glitch of mine, I can't imagine my few questions to this list having caused any inconvenience. On 3 Apr 2008, at 11:27, baptiste Auguié wrote: Dear R list, I'm having difficulties in choosing between a list or a data.frame, or an array for the storage and manipulation of my data (example follows). I've been using the three for different purposes but I would rather like to know which is more adapted to what task. Here is the data I'm currently working on: 200 observations, each observation being a vector of length 1000 depending on several factors (polarisation, pitch, dose, size) x - seq(1,100,length = 1000) observations - matrix( rnorm(200*1000), ncol = 200) # factors polarisation - rep(c(0,90), each = 100, length = 200) pitch - rep(1:5, length = 200) dose - rep(1:2, each =100, length = 200) size - rep(letters[1:6], each =100, length = 200) my.data - list(x = x, observations = as.data.frame(observations), polarisation = factor(polarisation), pitch = factor(pitch), dose = factor(dose), size = factor(size)) I would like to be able to manipulate the data in observations using the factors as opposed to column indices. For instance, I could plot all the observations corresponding to polarisation == 90 and pitch == 1, like in, with(my.data , matplot(x, subset((pitch == 1) (polarisation == 90), observations, type=l ))) which doesn't work, so I've had to use, with(my.data , matplot(x, observations[,(pitch == 1) (polarisation == 90)], type=l )) Is this a sensible way to store and manipulate this sort of data? Is there anything more appropriate I've overlooked using data.frames only? Many thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] data.frame or list
Dear R list, I'm having difficulties in choosing between a list or a data.frame, or an array for the storage and manipulation of my data (example follows). I've been using the three for different purposes but I would rather like to know which is more adapted to what task. Here is the data I'm currently working on: 200 observations, each observation being a vector of length 1000 depending on several factors (polarisation, pitch, dose, size) x - seq(1,100,length = 1000) observations - matrix( rnorm(200*1000), ncol = 200) # factors polarisation - rep(c(0,90), each = 100, length = 200) pitch - rep(1:5, length = 200) dose - rep(1:2, each =100, length = 200) size - rep(letters[1:6], each =100, length = 200) my.data - list(x = x, observations = as.data.frame(observations), polarisation = factor(polarisation), pitch = factor(pitch), dose = factor(dose), size = factor(size)) I would like to be able to manipulate the data in observations using the factors as opposed to column indices. For instance, I could plot all the observations corresponding to polarisation == 90 and pitch == 1, like in, with(my.data , matplot(x, subset((pitch == 1) (polarisation == 90), observations, type=l ))) which doesn't work, so I've had to use, with(my.data , matplot(x, observations[,(pitch == 1) (polarisation == 90)], type=l )) Is this a sensible way to store and manipulate this sort of data? Is there anything more appropriate I've overlooked using data.frames only? Many thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] Bessel functions of complex argument
Many thanks for this information! I'm pretty sure Matlab can handle complex argument in besselj, although i haven't checked myself, http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/ access/helpdesk/help/techdoc/ref/besselj.html After some more thinking, it turns out my code doesn't need a complex argument at the moment, i can live with the simplest case where it is real. Good to know where to go if this is to change in the future, though. Thanks again, baptiste On 10 Mar 2008, at 08:49, Robin Hankin wrote: Hello Baptiste Bessel functions with complex arguments are not supported in R. Neither matlab nor the Gnu Scientific Library support them either. . . . but . . . the pari/gp system (released on the GPL) does: ? besselj(1+I,3) %3 = 0.6919067491368555819808728680 + 0.4484268613977010268818252591*I ? You can access some pari/gp functionality from within R by using the elliptic package, although unfortunately its wrapper function, P.pari(), is not quite flexible enough to deal with besselj(). I'd be happy to discuss this offline; P.pari() will need only minor changes to accommodate besselj(). HTH Robin On 9 Mar 2008, at 13:44, baptiste Auguié wrote: Dear R users, I'm porting a piece of Matlab code to R, but I'm now stuck with the following: I need an equivalent of besselJ(x, nu) that can handle a complex argument x. I couldn't find any R implementation. I did find a possible fortran solution in SLATEC ( http://www.netlib.org/ slatec/ , CBESJ-C), however I've never tried to use external C or Fortran code together with my R code, so I'm not sure where to go for a simple solution. Any advice welcome, Best regards baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. -- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] Bessel functions of complex argument
Dear R users, I'm porting a piece of Matlab code to R, but I'm now stuck with the following: I need an equivalent of besselJ(x, nu) that can handle a complex argument x. I couldn't find any R implementation. I did find a possible fortran solution in SLATEC ( http://www.netlib.org/slatec/ , CBESJ-C), however I've never tried to use external C or Fortran code together with my R code, so I'm not sure where to go for a simple solution. Any advice welcome, Best regards baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] question on function arguments
Many thanks, this clarifies both points perfectly. baptiste On 19 Feb 2008, at 11:48, Duncan Murdoch wrote: On 18/02/2008 4:53 AM, baptiste Auguié wrote: Hi, I have two small issues with my R code, no big deal but curiosity really. Here is a sample code, x - rnorm(1:10) foo - function(a = 1, b = list(x = c(1:10), y = c(1:10))){ for (ii in seq(along=b$y)){ print(x[ii] + b$x[ii]) } } foo() # default OK foo(b=list(x=1, y=c(1:10))) # only the first term works foo(b$y = 1:5) # error In the last call, i wish to use all default arguments but b$y (that is, using the default a and b$x). Is this possible? No, arguments are a simple list where you use the defaults or you don't. You can't treat them as a hierarchical structure. The second call is more related to indices: i would like my argument b $x to be either a vector (default), or a scalar. In the latter, the loop b$x[ii] breaks when i would like it to recycle the single value. I can check the length of b$x with a if statement, but it becomes intricate when several arguments have this option of being vector or scalar. Is there a way to use b$x that would work in both cases? The normal way to do this is to standardize the length of the object early in the function, e.g. len - max(length(b$y), length(b$x)) b$y - rep(b$y, length=len) b$x - rep(b$x, length=len) Another way (which is the way used internally in the C implementation) is to compute an index using the recycling rules, but that's probably not efficient in R code, e.g. you'd need b$x[ (ii - 1) %% length(b$x) + 1 ] in place of b$x[ii] (and this solution is not general; it doesn't handle zero or negative indices properly). Duncan Murdoch Many thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] question on function arguments
Hi, I have two small issues with my R code, no big deal but curiosity really. Here is a sample code, x - rnorm(1:10) foo - function(a = 1, b = list(x = c(1:10), y = c(1:10))){ for (ii in seq(along=b$y)){ print(x[ii] + b$x[ii]) } } foo() # default OK foo(b=list(x=1, y=c(1:10))) # only the first term works foo(b$y = 1:5) # error In the last call, i wish to use all default arguments but b$y (that is, using the default a and b$x). Is this possible? The second call is more related to indices: i would like my argument b $x to be either a vector (default), or a scalar. In the latter, the loop b$x[ii] breaks when i would like it to recycle the single value. I can check the length of b$x with a if statement, but it becomes intricate when several arguments have this option of being vector or scalar. Is there a way to use b$x that would work in both cases? Many thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] question on function arguments
any input? (and also, why would all my posts get bounced off while I clearly subscribed to this list?) Best regards, On 18 Feb 2008, at 09:53, baptiste Auguié wrote: Hi, I have two small issues with my R code, no big deal but curiosity really. Here is a sample code, x - rnorm(1:10) foo - function(a = 1, b = list(x = c(1:10), y = c(1:10))){ for (ii in seq(along=b$y)){ print(x[ii] + b$x[ii]) } } foo() # default OK foo(b=list(x=1, y=c(1:10))) # only the first term works foo(b$y = 1:5) # error In the last call, i wish to use all default arguments but b$y (that is, using the default a and b$x). Is this possible? The second call is more related to indices: i would like my argument b $x to be either a vector (default), or a scalar. In the latter, the loop b$x[ii] breaks when i would like it to recycle the single value. I can check the length of b$x with a if statement, but it becomes intricate when several arguments have this option of being vector or scalar. Is there a way to use b$x that would work in both cases? Many thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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@r-project.org 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] write output in a custom format
Hi, I need to create a text file in the following format, 1 100.0 0 0 0 1 1 0 0 1 1 # 1 100.0 0 0 0 0 1 1 0 1 1 ... where # is part of the format and not a R comment. Each block (delimited by #) consists of a first line with three values, call it dose, and a list of (x,y) coordinates which are a matrix or data.frame, particle - list(dose=c(1,100.0,0),pos=data.frame(x=c(0,1,0,1),y=c (0,1,0,1))) print(particle) I'd like to establish a connection to a file and append to it a particle block in the format above, or even write the whole file at once. Because different lines have a different number of elements, I couldn't get write.table to work in this case, and my attempts at sink (), dump(), writeLines(), writeChar() all turn into really dirty solutions. I have this feeling I'm overlooking a simple solution. Any help welcome, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] write output in a custom format
Thanks for the input! It does work fine, however I'll have to do another loop to repeat this whole process quite a few times (10^3, 10^4 particles maybe), so I was hoping for a solution without loop. Maybe I could reshape all the values into a big array, dump it to a file and replace some values using system(awk...). I just don't really know how to format the data, having different number of values for some lines. Would that be a sensible thing to do? thanks, baptiste On 14 Feb 2008, at 16:49, jim holtman wrote: Here is a start. You basically have to interate through your data and use 'cat' to write it out: particle - list(dose=c(1,100.0,0),pos=data.frame(x=c(0,1,0,1),y=c (0,1,0,1))) output - file(/tempxx.txt, w) cat(particle$dose, \n, file=output, sep= ) for (i in 1:nrow(particle$pos)){ cat(particle$pos$x[i], particle$pos$y[i], \n, file=output, sep= ) } cat(#\n, file=output, sep= ) close(output) Here is what the file looks like: 1 100 0 0 0 1 1 0 0 1 1 # On 2/14/08, baptiste Auguié [EMAIL PROTECTED] wrote: Hi, I need to create a text file in the following format, 1 100.0 0 0 0 1 1 0 0 1 1 # 1 100.0 0 0 0 0 1 1 0 1 1 ... where # is part of the format and not a R comment. Each block (delimited by #) consists of a first line with three values, call it dose, and a list of (x,y) coordinates which are a matrix or data.frame, particle - list(dose=c(1,100.0,0),pos=data.frame(x=c(0,1,0,1),y=c (0,1,0,1))) print(particle) I'd like to establish a connection to a file and append to it a particle block in the format above, or even write the whole file at once. Because different lines have a different number of elements, I couldn't get write.table to work in this case, and my attempts at sink (), dump(), writeLines(), writeChar() all turn into really dirty solutions. I have this feeling I'm overlooking a simple solution. Any help welcome, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] distances between points in R^3
OK, I've come across a partial answer to the for loop problem in the use of dist(). However, my problem not only require to find the distances between points, but also to store the vector differences, and the normal to each face when i get the delaunay triangulation sorted. As i understand, the dist() function calls a C routine, is there a straight-forward way to find / edit / modify it to serve this specific purpose? Thanks, baptiste On 3 Feb 2008, at 18:55, baptiste Auguié wrote: Dear R helpers, I'm trying to write a numerical scheme for a boundary integral method to solve an electromagnetic problem. This requires the computation of the distance between points at the surface of an object (a sphere, in my example). Here is my code, require(rgl) r-1 size-10 theta-seq(0,2*pi,length=size*2) phi-seq(0,pi,length=size) pc = as.matrix(rbind(expand.grid(theta,phi))) x- r* sin(pc[,2]) * cos(pc[,1]) y- r* sin(pc[,2]) * sin(pc[,1]) z- r* cos(pc[,2]) plot3d(x, y, z, col=rainbow(1000), size=2,zlim=c(-1,1)) # scatterplot of points on a sphere df- unique(rbind(x,y,z), MARGIN = 2 ) # removes duplicates in cartesian coordinates dimension - dim(df)[1] matDistances - array(data=0,dim=c(dimension,dimension)) norm - function(a) sqrt(a %*% a) for (ii in 1:dimension){ for (jj in ii:dimension){ matDistances[ii,jj]- norm( df[,ii] - df[,jj]) } } This is both inefficient and ugly, I'll welcome any suggestion. In particular: - the location of the points on the sphere is not ideal (understand: not uniformly distributed over the area): i looked into delaunayn from the geometry package and qhull.com but at best I obtained a random set of points on the sphere. It must be a most classical problem –– generating a uniform distribution of points on a sphere––, but i've set that problem aside for the moment. - the double for loop over all the points cries out for a vectorized solution, but i can't really think of any. Many thanks in advance, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] ggplot / reshape: basic usage
Sorry i realize my example was silly. I've played a bit more and i now have a working example using base graphics and the plotCI function from the plotrix package (reproduced for self-consistency). I start with some scatterplot, and I want to group the data in say 4 arbitrary intervals along x. For each of these subgroups I now compute the mean x and y values, and the associated deviations. Finally, I plot these 4 points with errorbars and shaded rectangles displaying where the cuts occurred. It looked to me this whole process would be straight forward with reshape and ggplot2, but my previous attempts at understanding cast failed. Thanks for any tips, baptiste Here is the code, #require(plotrix) plotCI-function (x, y = NULL, uiw, liw = uiw, ui = NULL, li = NULL, err = y, sfrac = 0.01, gap = 0, slty = par(lty), add = FALSE, scol = NULL, pt.bg = par(bg), ...) { arglist - list(...) if (is.list(x)) { y - x$y x - x$x } if (is.null(y)) { if (is.null(x)) stop(both x and y NULL) y - as.numeric(x) x - seq(along = x) } if (missing(uiw) (is.null(ui) || is.null(li))) stop(must specify either relative limits or both lower and upper limits) if (!missing(uiw)) { if (err == y) z - y else z - x ui - z + uiw li - z - liw } if (is.null(arglist$xlab)) arglist$xlab - deparse(substitute(x)) if (is.null(arglist$ylab)) arglist$ylab - deparse(substitute(y)) if (err == y is.null(arglist$ylim)) arglist$ylim - range(c(y, ui, li), na.rm = TRUE) if (err == x is.null(arglist$xlim)) arglist$xlim - range(c(x, ui, li), na.rm = TRUE) if (missing(scol)) { if (!is.null(arglist$col)) scol - arglist$col else scol - par(col) } plotpoints - TRUE if (!is.null(arglist$pch) is.na(arglist$pch)) { arglist$pch - 1 plotpoints - FALSE } if (!add) do.call(plot, c(list(x, y, type = n), clean.args(arglist, plot))) if (gap == TRUE) gap - 0.01 ul - c(li, ui) if (err == y) { gap - rep(gap, length(x)) * diff(par(usr)[3:4]) smidge - par(fin)[1] * sfrac arrow.args - c(list(lty = slty, angle = 90, length = smidge, code = 1, col = scol), clean.args(arglist, arrows, exclude.other = c(col, lty))) do.call(arrows, c(list(x, li, x, pmax(y - gap, li)), arrow.args)) do.call(arrows, c(list(x, ui, x, pmin(y + gap, ui)), arrow.args)) } else if (err == x) { gap - rep(gap, length(x)) * diff(par(usr)[1:2]) smidge - par(fin)[2] * sfrac arrow.args - c(list(lty = slty, angle = 90, length = smidge, code = 1), clean.args(arglist, arrows, exclude.other = c (col, lty))) do.call(arrows, c(list(li, y, pmax(x - gap, li), y), arrow.args)) do.call(arrows, c(list(ui, y, pmin(x + gap, ui), y), arrow.args)) } if (plotpoints) do.call(points, c(list(x, y, bg = pt.bg), clean.args (arglist, points, exclude.other = c(xlab, ylab, xlim, ylim, axes invisible(list(x = x, y = y)) } xx-seq(0,10,length=500) yy-jitter(cos(10*xx)+cos(10*(xx*1.1)),amount=0.5) # some beating + random noise data-as.data.frame(list(xx=xx,yy=yy)) lev-seq(range(data$xx)[1],range(data$xx)[2],length=5) # levels for binning g - cut(data$xx,lev ) # factors for binning se - function(x) sd(x)/sqrt(length(x)) # standard error test-sapply(split(data,g),function(d) list(meanx=mean(d $xx),meany=mean(d$yy),stdx=se(d$xx),stdy=se(d$yy))) as.data.frame(t(test))-test plot(xx,yy) plotCI(as.numeric(test$meanx),as.numeric(test$meany),2*as.numeric (test$stdy),lwd=2,col=1,scol=2,add=TRUE) # y errorbars plotCI(as.numeric(test$meanx),as.numeric(test$meany),2*as.numeric (test$stdx),lwd=2,col=3,err=x,scol=5,add=TRUE) # x errorbars rect(lev[seq(1,length(lev)-1,by=1)], min(yy),lev[seq(2,length (lev),by=1)], max(yy),col =c(rgb(.8,.8,.8,.1),rgb(.6,.6,.6,.1)), border = NA) # show bins On 24 Jan 2008, at 01:34, hadley wickham wrote: On Jan 23, 2008 10:44 AM, baptiste Auguié [EMAIL PROTECTED] wrote: Hi, I've been trying to do the following simple thing: given a data.frame, library(reshape) library(ggplot2) df - data.frame(x=c(1:10),y=sin(1:10),z=cos(1:10)) dfm-melt(df, id=c(x), measured=c(y,z)) i want to plot y and z against x, and add vertical errorbars to the points corresponding to the standard deviation of y and z respectively. I tried the following, inspired by some previous post in the list, se - function(x) sd(x)/sqrt(length(x)) means - cast(dfm, variable~., function(x) c(se = se(x))) qplot(value,x, data=means, colour=variable, min = value - se, max
[R] ggplot / reshape: basic usage
Hi, I've been trying to do the following simple thing: given a data.frame, library(reshape) library(ggplot2) df - data.frame(x=c(1:10),y=sin(1:10),z=cos(1:10)) dfm-melt(df, id=c(x), measured=c(y,z)) i want to plot y and z against x, and add vertical errorbars to the points corresponding to the standard deviation of y and z respectively. I tried the following, inspired by some previous post in the list, se - function(x) sd(x)/sqrt(length(x)) means - cast(dfm, variable~., function(x) c(se = se(x))) qplot(value,x, data=means, colour=variable, min = value - se, max = value + se, geom=c(point,errorbar)) but this fails, as I obviously don't get the philosophy behind the cast function. Any advice welcome! Thanks, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] access data inside package
Dear all, Happy new year! I posted a very similar question a few days ago, but probably too cluttered. Here is a tidy, minimal version: I want to make a package, with a data.frame d and a function f given below. Now, the function f needs to use the data.frame d. I could (and that's what I've done temporarily) make a local duplicate of this data.frame in the function definition, but it seems silly. I've tried attach(), data(), but both fail when running R CMD check with the error * checking R code for possible problems ... NOTE f: no visible binding for global variable 'd' Here is the minimal package skeleton: d - data.frame(a=1) f - function(x) { data(d) x+d$a } package.skeleton(list=c(f,d), name=mypkg) What would be a sensible way to refer to this data inside the package? Best regards, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] adding a function after package.skeleton()
Dear R helpers, I've successfully created a package 'constants' using package.skeleton () with one dataframe and a few functions. However, now that I want to add some functions and data to the package, I run into a problem. I ran prompt(...) and moved + edited the resulting .Rd files as appropriate (I believe). The log file from RCMD check constants does indicate a few problems (full log below). As far as I understand, the real problem would be on the lines: * checking R code for possible problems ... NOTE L2eV: no visible binding for global variable 'Constants' eV2L: no visible binding for global variable 'Constants' These two functions use the dataframe Constants, part of this package: `L2eV` - function(lambda) { data(Constants) Constants$h*Constants$cel/Constants$ee/lambda -eV eV } and `eV2L` - function(eV) { data(Constants) Constants$h*Constants$cel/Constants$ee/eV -Lambda Lambda } After searching the R archives about no visible binding for global variable , I added the quotes around Constants but it doesn't seem to help. What would be the correct way to use this data inside the package? Best regards, baptiste Log of R CMD check constants : baptiste-auguies-ibook-g4:~ baptiste$ R CMD check constants * checking for working latex ... OK * using log directory '/Users/baptiste/constants.Rcheck' * using R version 2.6.1 (2007-11-26) * checking for file 'constants/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'constants' version '1.0' * checking package dependencies ... OK * checking if this is a source package ... OK * checking whether package 'constants' can be installed ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking top-level files ... OK * checking index information ... OK * checking package subdirectories ... OK * checking R files for non-ASCII characters ... OK * checking R files for syntax errors ... OK * checking whether the package can be loaded ... OK * checking whether the package can be loaded with stated dependencies ... OK * checking for unstated dependencies in R code ... OK * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK * checking R code for possible problems ... NOTE L2eV: no visible binding for global variable 'Constants' eV2L: no visible binding for global variable 'Constants' * checking Rd files ... WARNING Rd files with non-standard keywords: L2eV.Rd: kwd1 kwd2 constants-package.Rd: physical constants optical delete.all.Rd: kwd1 kwd2 eV2L.Rd: kwd1 kwd2 epsilon2nk.Rd: kwd1 kwd2 fano.Rd: kwd1 kwd2 lorentz.Rd: kwd1 kwd2 nk2epsilon.Rd: kwd1 kwd2 Each '\keyword' entry should specify one of the standard keywords (as listed in file 'KEYWORDS' in the R documentation directory). See the chapter 'Writing R documentation files' in manual 'Writing R Extensions'. * checking Rd cross-references ... OK * checking for missing documentation entries ... OK * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking data for non-ASCII characters ... OK * creating constants-Ex.R ... OK * checking examples ... OK * creating constants-manual.tex ... OK * checking constants-manual.tex ... OK WARNING: There was 1 warning, see /Users/baptiste/constants.Rcheck/00check.log for details _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] adding a function after package.skeleton()
Hi, Thanks for this tip, I'm always amazed at the number of clever functions built-in in R –– just wish i could think of their name rather than reinventing the wheel. However, I'm still stupidly stuck with this basic question: how should a function access data in its own package? On 30 Dec 2007, at 18:27, Gabor Grothendieck wrote: On Dec 30, 2007 1:19 PM, baptiste Auguié [EMAIL PROTECTED] wrote: These two functions use the dataframe Constants, part of this package: `L2eV` - function(lambda) { data(Constants) Constants$h*Constants$cel/Constants$ee/lambda -eV eV } This does not answer your question but note that you can do this: with(Constants, h * cel / ee / lambda) Initial post: From: [EMAIL PROTECTED] Subject:adding a function after package.skeleton() Date: 30 December 2007 18:19:40 GMT To: r-help@r-project.org Dear R helpers, I've successfully created a package 'constants' using package.skeleton() with one dataframe and a few functions. However, now that I want to add some functions and data to the package, I run into a problem. I ran prompt(...) and moved + edited the resulting .Rd files as appropriate (I believe). The log file from RCMD check constants does indicate a few problems (full log below). As far as I understand, the real problem would be on the lines: * checking R code for possible problems ... NOTE L2eV: no visible binding for global variable 'Constants' eV2L: no visible binding for global variable 'Constants' These two functions use the dataframe Constants, part of this package: `L2eV` - function(lambda) { data(Constants) Constants$h*Constants$cel/Constants$ee/lambda -eV eV } and `eV2L` - function(eV) { data(Constants) Constants$h*Constants$cel/Constants$ee/eV -Lambda Lambda } After searching the R archives about no visible binding for global variable , I added the quotes around Constants but it doesn't seem to help. What would be the correct way to use this data inside the package? Best regards, baptiste Log of R CMD check constants : baptiste-auguies-ibook-g4:~ baptiste$ R CMD check constants * checking for working latex ... OK * using log directory '/Users/baptiste/constants.Rcheck' * using R version 2.6.1 (2007-11-26) * checking for file 'constants/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'constants' version '1.0' * checking package dependencies ... OK * checking if this is a source package ... OK * checking whether package 'constants' can be installed ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking top-level files ... OK * checking index information ... OK * checking package subdirectories ... OK * checking R files for non-ASCII characters ... OK * checking R files for syntax errors ... OK * checking whether the package can be loaded ... OK * checking whether the package can be loaded with stated dependencies ... OK * checking for unstated dependencies in R code ... OK * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK * checking R code for possible problems ... NOTE L2eV: no visible binding for global variable 'Constants' eV2L: no visible binding for global variable 'Constants' * checking Rd files ... WARNING Rd files with non-standard keywords: L2eV.Rd: kwd1 kwd2 constants-package.Rd: physical constants optical delete.all.Rd: kwd1 kwd2 eV2L.Rd: kwd1 kwd2 epsilon2nk.Rd: kwd1 kwd2 fano.Rd: kwd1 kwd2 lorentz.Rd: kwd1 kwd2 nk2epsilon.Rd: kwd1 kwd2 Each '\keyword' entry should specify one of the standard keywords (as listed in file 'KEYWORDS' in the R documentation directory). See the chapter 'Writing R documentation files' in manual 'Writing R Extensions'. * checking Rd cross-references ... OK * checking for missing documentation entries ... OK * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking data for non-ASCII characters ... OK * creating constants-Ex.R ... OK * checking examples ... OK * creating constants-manual.tex ... OK * checking constants-manual.tex ... OK WARNING: There was 1 warning, see /Users/baptiste/constants.Rcheck/00check.log for details _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting
[R] unit attribute to list elements
Hi, I've started my own (first) package, part of which consists in listing common physical constants (Planck's constant, the speed of light in vacuum, etc). I'm wondering what would be a good way of dealing with pairs of value/unit. constants - list( cel = 2.99792458e8 , #m/s Z0 = 376.730313461, #ohm eps0 = 8.854187817e-12,#F/m mu0 = 4*pi*1e-7,#N/A^2 G = 6.67428e-11 # m^3 kg-1 s-2 ) I thought I could include the unit in the names attribute of each element, as in : names(constants$cel)- speed of light in vacuum [m.s^-1] Writing this for every element is very redundant... Is there any way to access and set the name of each first level element of the list? namesFirstLevelElements(constants)- c( speed of light in vacuum [m.s^-1], impedance of vacuum [some unit], ...) Quite possibly, I'm completely on the wring track; - maybe such a package already exists - a custom class or structure would be more appropriate? I don't really know how to deal with classes, though, and I'd like to keep the access to the constants' values as direct and generic as possible. Many thanks in advance, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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] unit attribute to list elements
On 28 Dec 2007, at 20:06, Gabor Grothendieck wrote: Is this what you want? Perfect! Thanks a lot! Lines - cel 3.0 m/s + Z0 367 ohm + eps0 8.9e-12 F/m + Constants.DF - read.table(textConnection(Lines), as.is = TRUE) Constants - as.list(Constants.DF[[2]]) names(Constants) - Constants.DF[[1]] for(i in seq_along(Constants)) comment(Constants[[i]]) - Constants.DF[i, 3] # get value Constants$Z0 [1] 367 # get comment comment(Constants$Z0) [1] ohm # add another Constant Constants$e - 2.7 comment(Constants$e) - exp On Dec 28, 2007 2:33 PM, baptiste Auguié [EMAIL PROTECTED] wrote: Hi, I've started my own (first) package, part of which consists in listing common physical constants (Planck's constant, the speed of light in vacuum, etc). I'm wondering what would be a good way of dealing with pairs of value/unit. constants - list( cel = 2.99792458e8 , #m/s Z0 = 376.730313461, #ohm eps0 = 8.854187817e-12,#F/m mu0 = 4*pi*1e-7,#N/A^2 G = 6.67428e-11 # m^3 kg-1 s-2 ) I thought I could include the unit in the names attribute of each element, as in : names(constants$cel)- speed of light in vacuum [m.s^-1] Writing this for every element is very redundant... Is there any way to access and set the name of each first level element of the list? namesFirstLevelElements(constants)- c( speed of light in vacuum [m.s^-1], impedance of vacuum [some unit], ...) Quite possibly, I'm completely on the wring track; - maybe such a package already exists - a custom class or structure would be more appropriate? I don't really know how to deal with classes, though, and I'd like to keep the access to the constants' values as direct and generic as possible. Many thanks in advance, baptiste _ Baptiste Auguié Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto __ R-help@r-project.org 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@r-project.org 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] data.frame, ggplot vs matplot
Hello, I've been trying to use ggplot, but I cannot seem to get my data in the correct form. I usually have a vector x, and a matrix y and I would usually use matplot(x,y) to plot the columns of y against x. Now, if i understand correctly, I should cast x and y into a data.frame and do something like ggplot(x,y,data=data). I think I'm missing something here: do I really need to reshape y into a single vector, replicate x accordingly, and put both vectors in a data frame? I've tried it, but the reshape function is still very obscure to me. Any help appreciated! Small example: x-seq(from=1,by=1,length=10) y-sin(x)%*%t(x) columns-c(1:10) matplot(x,y) legend(topleft, (x,y), columns) # the version below is not working require(ggplot2) data.frame(cbind(x,y))-dataS wide - reshape(dataS, direction=wide,timevar=c(1:length(columns))) dataL-reshape(wide,direction=long) # this clearly doesn't work as I would have thought qplot(x, y, data = dataL) Best regards, baptiste __ R-help@r-project.org 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] image 256 x 920 data points too slow
Dear R gurus, I'm trying to visualize a matrix 256 x 920 using image(), but i find the display too slow (~ 1 minute –– admittedly, my iBook G4 isn't very fast). The aim is not to get a good resolution image, but rather have a quick look at the matrix. I couldn't find a way to plot a smaller set of points from my data in a sensible manner (basically, i want to decrease the resolution). Is there an easy option for this purpose in image(), or possibly the lattice equivalent? Minimal example: x-c(1:256) y-c(1:920) z-x%*%t(y) image(x,y,z) Best regards, baptiste __ R-help@r-project.org 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] image 256 x 920 data points too slow
Thanks, it works nicely. In the meantime, I came across a similar approach taken in the documentation of ggplot2, using a small sample of the diamonds data. Thanks all again, baptiste On 21 Oct 2007, at 12:25, Katharine Mullen wrote: I'm not aware of any existing function that does what you want, but you could easily write a function to sample from x, y, z, and then pass the sampled values to image. e.g., sam - function(sx,sy,x,y,z){ xind-seq(1,length(x),by=sx) yind-seq(1,length(y),by=sy) samplex-x[xind] sampley-y[yind] samplez-z[xind,yind] list(x=samplex,y=sampley,z=samplez) } newval-sam(sx=13.3,sy=15,x=x,y=y,z=z) image(newval$x,newval$y,newval$z) On Sun, 21 Oct 2007, [ISO-8859-1] baptiste Auguié wrote: Dear R gurus, I'm trying to visualize a matrix 256 x 920 using image(), but i find the display too slow (~ 1 minute –– admittedly, my iBook G4 isn't very fast). The aim is not to get a good resolution image, but rather have a quick look at the matrix. I couldn't find a way to plot a smaller set of points from my data in a sensible manner (basically, i want to decrease the resolution). Is there an easy option for this purpose in image(), or possibly the lattice equivalent? Minimal example: x-c(1:256) y-c(1:920) z-x%*%t(y) image(x,y,z) Best regards, baptiste __ R-help@r-project.org 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@r-project.org 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.