Re: [R] dataframe (matrix) multiplication
where is the problem? input: A-c(2,3,2); B-c(1,2,1); C-c(4,4,5); D-c(3,5,4) df-data.frame(A,B,C,D) c1-1:2; c2-3:4 df[,c1]*df[,c2] output: A B 1 8 3 2 12 10 3 10 4 Peter Wolf Christoph Scherber wrote: Dear R users, Suppose I have 2 parts of a dataframe, say ABCD 2143 3245 2154 (the real dataframe is 160 columns with each 120 rows) and I want to multiply every element in [,A:B] with every element in [,C:D]; What is the most elegant way to do this? I´ve been thinking of converting [,A:B] to a matrix, and then multiplying it with the inverse of [,C:D]; would that be correct? The result should look like E;F 8;3 12;10 10;4 Thanks very much for any suggestions Christoph __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] hist(x, ...) with normal distribution curve
Knut Krueger wrote: . I am looking for a histogram or box plot with the adding normal distribution curve I think that must be possible, but I am not able to find out how to do. Regards Knut __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html *There are a lot of answers to add a histogram. Here is a simple way to add a tiny boxplot to a plot / histogram x-rexp(100) hist(x) boxplot(x,axes=F,add=T,horizontal=T, at=par()$usr[3]+diff(par()$usr[3:4])*.017, boxwex=0.02*diff(par()$usr[3:4]),pch=8) Peter Wolf * __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] questions about boxplots
concerning question 1: -- labeling of outliers -- you can get the outliers by boxplot(...)$out try: set.seed(17) x-rexp(99) names(x)-paste(x,1:99) out-boxplot(x)$out text(rep(1.1,length(out)), out, names(out)) Peter Wolf Stephen D. Weigand wrote: Dear Yulei, On Sep 26, 2005, at 6:56 PM, Yulei He wrote: Hi, there. I have two questions about using R to create boxplots. 1. The function boxplot() plots the outliers. How can I label the exact values arount these outlier points? Does R have an option allow me to do that? You can use identify(). Here's a toy example set.seed(1) y - rt(30, 3) boxplot(y) identify(x = rep(1,30), y = y, label = format(y, digits = 2)) ### now click on the outlier in the plot and you should see -7.398 ### beside the outlier 2. How can I put two boxplots in one x-y axis? x - rnorm(10) y - rnorm(20, 3, 5) z - runif(30) boxplot(x, y, z, labels = c(x, y, z)) ### - or - boxplot(list(x = x, y = y, z = z)) Thanks. Yulei Stephen Weigand Rochester, Minnesota, USA __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R: extracting elements in a matrix
... you can, for example set.seed(13) x-matrix(rnorm(100),20,5) lapply(split(x,col(x)), function(x){ limit-diff(quantile(x,c(.25,.75))) x[xlimit] }) output: $1 [1] 1.775163 1.142526 1.229507 1.105144 1.396432 $2 [1] 1.836163 $3 [1] 1.590034 1.614479 1.408354 1.483691 $4 [1] 1.745427 $5 [1] 1.602120 Peter Wolf Frank Schmid wrote: Dear R-users For a given matrix of dimension, say (n,p), I'd like to extract for every column those elements that are bigger than twice the interquartile range of the corresponding column. Can I get these elements without using a loop? Thank you for your help Frank [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] bagplot - a rough R function
Martin Maechler wrote: Peter == Peter Wolf [EMAIL PROTECTED] on Wed, 21 Sep 2005 16:49:10 +0200 writes: Peter In the moment I am writing an R function for drawing Peter bagplots (two dimensional boxplots). Peter For some elements of the plot are found numerically Peter the resulting plots differs a little bit from the correct bagplots. Peter Here is the link to the actual version: Peter http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/bagplot/bagplot.R Peter ( R code ) Peter http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/bagplot/bagplot.pdf Peter (examples and description) Peter Now I am looking for examples to test / to improve my function. Peter Where can I get data sets and the corresponding correct bagplots? I'll send you my (unpublished) version of my package 'bagplot' {{the one I've never released because of segmentation fault bugs somewhere in Fortran}} in a private E-mail. In my version I had started make a function bag() which *computes* all the information needed and then returns an (S3) object bag. Then, the plotting happens via the plot.bag - function(.) {..} method. So you can say plot(bag(.), ) if you want both (computing and plotting) at once, but can also separate the two --- following very much the modern S way whole object ... Martin Hello Martin, you are right. Separating computing and plotting is a better way for a public version and I think that I will split my function. But during testing algorithmic problems it is nice to have only one function. Peter __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Add function to histogram?
Robert Lundqvist wrote: Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet... Robert __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ??? dat-rnorm(100) hist(dat,prob=TRUE) x-seq(-3.5,3.5,length=100) y-dnorm(x) lines(x,y) have a look at: http://cran.at.r-project.org/doc/manuals/R-intro.pdf Peter Wolf __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] bagplot - a rough R function
In the moment I am writing an R function for drawing bagplots (two dimensional boxplots). For some elements of the plot are found numerically the resulting plots differs a little bit from the correct bagplots. Here is the link to the actual version: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/bagplot/bagplot.R ( R code ) http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/bagplot/bagplot.pdf (examples and description) Now I am looking for examples to test / to improve my function. Where can I get data sets and the corresponding correct bagplots? Thanks Peter Wolf ([EMAIL PROTECTED]) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] 'splice' two data frames
what about: @ *= z-data.frame(matrix(rbind(x,y),nrow(x),2*ncol(x))) rownames(z)-rownames(x) z @ output-start Thu Aug 25 11:24:29 2005 X1 X2 X3 X4 X5 X6 X7 X8 a 2 0.02 1 0.01 2 0.02 2 0.02 b 3 0.03 0 0.00 4 0.04 3 0.03 c 3 0.03 2 0.02 1 0.01 6 0.06 d 3 0.03 4 0.04 2 0.02 3 0.03 e 4 0.04 1 0.01 4 0.04 2 0.02 f 2 0.02 5 0.05 2 0.02 2 0.02 g 4 0.04 5 0.05 3 0.03 3 0.03 h 3 0.03 3 0.03 5 0.05 4 0.04 i 1 0.01 0 0.00 3 0.03 3 0.03 output-end Peter Wolf David Whiting wrote: Hi, I often need to take columns from two data.frames and 'splice' them together (alternately taking a column from the first data frame, then from the second). For example: x - table(sample(letters[1:9], 100, replace=TRUE), sample(letters[1:4], 100, replace=TRUE)) y - prop.table(x) splice - function (x, y) { z - matrix(rep(NA, (ncol(x) * 2) * nrow(x)), nrow = nrow(x)) j - 1 for (i in seq(1, ncol(z), by = 2)) { z[, i] - x[, j] z[, (i + 1)] - y[, j] j - j + 1 } z - data.frame(z) rownames(z) - rownames(x) z } splice(x, y) Manually using indexing I can do this: zz - data.frame(x[, 1], y[, 1], x[, 2], y[, 2], x[, 3], y[, 3], x[, 4], y[, 4]) I *feel* that it should be possible in R to generate the sequence of column indexes automatically. I can get close with this: i - paste(x[,, 1:ncol(x), ], , y[,, 1:ncol(y), ], collapse=, ) which creates a string version of what I want, but I am not sure how to use that with data.frame. FAQ 7.21 (How can I turn a string into a variable?) looked promising but I have not been able to apply any of the suggestions to this problem. I also tried using do.call: i - paste(x[,, 1:4, ],, y[,, 1:4, ], collapse=,) i - gsub(],, ]@, i) # Create a marker for i - strsplit(i, @) # strsplit to create a list do.call(data.frame, i) and with lapply: lappy(i, data.frame) These did not work (i.e. they worked as they were designed to and did not give me the results I am after). I think I need a nudge or two in the right direction. Thanks. Dave __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Converting characters to numbers in data frames
Try: NAMES=c(NRes.1.2., NRes.1.3., NRes.1.4., NRes.1.5., NRes.1.6.) pattern-NRes\.([0-9]*)\.([0-9]*)\. data.frame(x=sub(pattern,\\1,NAMES),y=sub(pattern,\\2,NAMES)) @ output-start Thu Aug 25 14:08:50 2005 x y 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 output-end Peter Wolf Anon. wrote: I'm sure I'm missing something obvious here, but I can't find the solution (including in the FAQ etc.). I have a vector of names of variables like this: NRes.x.y. where x and y are numbers. I want to extract these numbers as numbers to use elsewhere. I can extract the numbers as a list of characters using strsplit(), and convert that to a data frame, e.g.: NAMES=c(NRes.1.2., NRes.1.3., NRes.1.4., NRes.1.5., NRes.1.6.) NUMBERS=strsplit(gsub(NRes.,, NAMES, perl =T), '.', fixed = TRUE) NUMBERS.df=t(data.frame(NUMBERS)) But I now want to convert the characters to be numeric. Using as.numeric(NUMBERS.df) converts them, but to a vector. How can I convert and keep as a data frame? I could use this: matrix(as.numeric(NUMBERS.df), ncol=dim(NUMBERS.df)[2]) but I seem to be jumping through far too many hoops: there must be an easier way. An suggestions? Bob __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Plotting a simple subset
Michael Hopkins wrote: Hi all Just converting from Stata to R and struggling a little to come to terms with the new philosophy/command line. E.g. I want to plot x against y if x 5 In Stata: graph x y, if( x 5 ) How do I do this in R? Have tried most of the obvious options without success. Can I have multiple subsets? I.e. In Stata: if( x 5 y 3 ) Try: x-rnorm(20,0,5) y-2*x+rnorm(20) # plot without condition plot(x,y) # some conditions on x and y index- x5 y3 plot(x[index],y[index]) Peter Wolf TIA Michael _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ _/_/ _/_/_/ Hopkins Research Ltd _/_/ _/_/ _/_/_/_/ _/_/_/ http://www.hopkins-research.com/ _/_/ _/ _/ _/_/ _/ _/ 'touch the future' _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] [R-pkgs] New CRAN package relax: R Editor for Literate Analysis and lateX
Now package relax is on CRAN. The name relax is short for R Editor for Literate Analysis and lateX The main element of package relax is the function relax() which starts an all-in-one editor for data analysis and easy creation of LaTeX based documents with R. After calling relax() it creates a tcl/tk widget with a report field. The report field enables you to enter R expressions as well as pieces of text to document your ideas. Computations and plots can be included quickly. After finishing your work the sequence of text chunks, code chunks and integrated graphics and/or R-output will constitute the basis of your work. To achieve a higher quality relax integrates LaTeX compilation for professional formatting and pretty printing. Dependencies: * R (= 2.1.0), tcltk * relax runs on windows systems, LaTeX / ghostscript has to be installed * on Linux systems you have to install the img-package for tcltk For further info see: http://www.wiwi.uni-bielefeld.de/~wolf/software/relax/relax.html maintainer: Hans Peter Wolf Department of Economics University of Bielefeld [EMAIL PROTECTED] R Editor for Literate Analysis and lateX --- the all-in-one editor for data analysis and easy creation of LaTeX based documents with R __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R demos
Federico Calboli wrote: Hi All, I am currently preparing some form of slideshow introducing R and its capabilities for some colleagues. The thing will be about 30 mins, and I'd like to have some pretty pictures and some amazing facts (I'm trying to sell, obviously :)). Can I ask if it's possible to easily retrieve a gross figure of the number of functions in R considering the base install and all the libraries available? Apart from graphics and lattice, are there any more packages producing eye catching graphics (possibly with a survival analysis/epidemiological bend)? Cheers, Federico Calboli In the package relax you find the function slider(). The help page of slider shows a nice application: R.veil.in.the.wind() Here are the definitions of slider and R.veil.in.the.wind: # definition of slider slider-function (sl.functions, sl.names, sl.mins, sl.maxs, sl.deltas, sl.defaults, but.functions, but.names, no, set.no.value, obj.name, obj.value, reset.function, title) { if (!missing(no)) return(as.numeric(tclvalue(get(paste(slider, no, sep = ), env = slider.env if (!missing(set.no.value)) { try(eval(parse(text = paste(tclvalue(slider, set.no.value[1], )-, set.no.value[2], sep = )), env = slider.env)) return(set.no.value[2]) } if (!exists(slider.env)) slider.env - new.env() if (!missing(obj.name)) { if (!missing(obj.value)) assign(obj.name, obj.value, env = slider.env) else obj.value - get(obj.name, env = slider.env) return(obj.value) } if (missing(title)) title - slider control widget require(tcltk) nt - tktoplevel() tkwm.title(nt, title) tkwm.geometry(nt, +0+0) if (missing(sl.names)) sl.names - NULL if (missing(sl.functions)) sl.functions - function(...) { } for (i in seq(sl.names)) { eval(parse(text = paste(assign('slider, i, ',tclVar(sl.defaults[i]),env=slider.env), sep = ))) tkpack(fr - tkframe(nt)) lab - tklabel(fr, text = sl.names[i], width = 25) sc - tkscale(fr, from = sl.mins[i], to = sl.maxs[i], showvalue = T, resolution = sl.deltas[i], orient = horiz) tkpack(lab, sc, side = right) assign(sc, sc, env = slider.env) eval(parse(text = paste(tkconfigure(sc,variable=slider, i, ), sep = )), env = slider.env) sl.fun - if (length(sl.functions) 1) sl.functions[[i]] else sl.functions if (!is.function(sl.fun)) sl.fun - eval(parse(text = paste(function(...){, sl.fun, }))) tkconfigure(sc, command = sl.fun) } assign(slider.values.old, sl.defaults, env = slider.env) tkpack(f.but - tkframe(nt), fill = x) tkpack(tkbutton(f.but, text = Exit, command = function() tkdestroy(nt)), side = right) if (missing(reset.function)) reset.function - function(...) print(relax) if (!is.function(reset.function)) reset.function - eval(parse(text = paste(function(...){, reset.function, }))) tkpack(tkbutton(f.but, text = Reset, command = function() { for (i in seq(sl.names)) eval(parse(text = paste(tclvalue(slider, i, )-, sl.defaults[i], sep = )), env = slider.env) reset.function() }), side = right) if (missing(but.names)) but.names - NULL for (i in seq(but.names)) { but.fun - if (length(but.functions) 1) but.functions[[i]] else but.functions if (!is.function(but.fun)) but.fun - eval(parse(text = paste(function(...){, but.fun, }))) tkpack(tkbutton(f.but, text = but.names[i], command = but.fun), side = left) } invisible(nt) } # definition of R.veil.in.the.wind R.veil.in.the.wind-function(){ # Mark Hempelmann / Peter Wolf par(bg=blue4, col=white, col.main=white, col.sub=white, font.sub=2, fg=white) # set colors and fonts samp - function(N,D) N*(1/4+D)/(1/4+D*N) z-outer(seq(1, 800, by=10), seq(.0025, 0.2, .0025)^2/1.96^2, samp) # create 3d matrix h-100 z[10:70,20:25]-z[10:70,20:25]+h; z[65:70,26:45]-z[65:70,26:45]+h z[64:45,43:48]-z[64:45,43:48]+h; z[44:39,26:45]-z[44:39,26:45]+h x-26:59; y-11:38; zz-outer(x,y,+); zz-zz*(65zz)*(zz73) cz-10+col(zz)[zz0];rz-25+row(zz)[zz0]; z[cbind(cz,rz)]-z[cbind(cz,rz)]+h refresh.code-function(...){ theta-slider(no=1); phi-slider(no=2) persp(x=seq(1,800,by=10),y=seq(.0025,0.2,.0025),z=z,theta=theta,phi=phi, scale=T, shade=.9, box=F, ltheta = 45, lphi = 45, col=aquamarine, border=NA,ticktype=detailed) } slider(refresh.code, c(theta, phi), c(0, 0),c(360, 360),c(.2, .2),c(85, 270) ) } # now let's test it! R.veil.in.the.wind() __ R-help
Re: [R] r programming help
Try: DRY-c(2,5,3,7,11) WET-(1:5)*10 print(filter(c(rep(0,length(WET)),DRY),WET)) Time Series: Start = 1 End = 10 Frequency = 1 [1] NA NA 0 20 90 190 360 640 NA NA CYCLE.n-c(NA, WET[1]*DRY[1], WET[1]*DRY[2]+WET[2]*DRY[1], WET[1]*DRY[3]+WET[2]*DRY[2]+WET[3]*DRY[1], WET[1]*DRY[4]+WET[2]*DRY[3]+WET[3]*DRY[2]+WET[4]*DRY[1], WET[1]*DRY[5]+WET[2]*DRY[4]+WET[3]*DRY[3]+WET[4]*DRY[2]+WET[5]*DRY[1]) print(CYCLE.n) [1] NA 20 90 190 360 640 Peter Wolf Mohammad Ehsanul Karim wrote: Dear list, Is there anyway i can make the following formula short by r-programming? CYCLE.n-c(NA, WET[1]*DRY[1], WET[1]*DRY[2]+WET[2]*DRY[1], WET[1]*DRY[3]+WET[2]*DRY[2]+WET[3]*DRY[1], WET[1]*DRY[4]+WET[2]*DRY[3]+WET[3]*DRY[2]+WET[4]*DRY[1], WET[1]*DRY[5]+WET[2]*DRY[4]+WET[3]*DRY[3]+WET[4]*DRY[2]+WET[5]*DRY[1], WET[1]*DRY[6]+WET[2]*DRY[5]+WET[3]*DRY[4]+WET[4]*DRY[3]+WET[5]*DRY[2]+WET[6]*DRY[1], WET[1]*DRY[7]+WET[2]*DRY[6]+WET[3]*DRY[5]+WET[4]*DRY[4]+WET[5]*DRY[3]+WET[6]*DRY[2]+WET[7]*DRY[1], WET[1]*DRY[8]+WET[2]*DRY[7]+WET[3]*DRY[6]+WET[4]*DRY[5]+WET[5]*DRY[4]+WET[6]*DRY[3]+WET[7]*DRY[2]+WET[8]*DRY[1], WET[1]*DRY[9]+WET[2]*DRY[8]+WET[3]*DRY[7]+WET[4]*DRY[6]+WET[5]*DRY[5]+WET[6]*DRY[4]+WET[7]*DRY[3]+WET[8]*DRY[2]+WET[9]*DRY[1], WET[1]*DRY[10]+WET[2]*DRY[9]+WET[3]*DRY[8]+WET[4]*DRY[7]+WET[5]*DRY[6]+WET[6]*DRY[5]+WET[7]*DRY[4]+WET[8]*DRY[3]+WET[9]*DRY[2]+WET[10]*DRY[1], WET[1]*DRY[11]+WET[2]*DRY[10]+WET[3]*DRY[9]+WET[4]*DRY[8]+WET[5]*DRY[7]+WET[6]*DRY[6]+WET[7]*DRY[5]+WET[8]*DRY[4]+WET[9]*DRY[3]+WET[10]*DRY[2]+WET[11]*DRY[1], WET[1]*DRY[12]+WET[2]*DRY[11]+WET[3]*DRY[10]+WET[4]*DRY[9]+WET[5]*DRY[8]+WET[6]*DRY[7]+WET[7]*DRY[6]+WET[8]*DRY[5]+WET[9]*DRY[4]+WET[10]*DRY[3]+WET[11]*DRY[2]+WET[12]*DRY[1], WET[1]*DRY[13]+WET[2]*DRY[12]+WET[3]*DRY[11]+WET[4]*DRY[10]+WET[5]*DRY[9]+WET[6]*DRY[8]+WET[7]*DRY[7]+WET[8]*DRY[6]+WET[9]*DRY[5]+WET[10]*DRY[4]+WET[11]*DRY[3]+WET[12]*DRY[2]+WET[13]*DRY[1], WET[1]*DRY[14]+WET[2]*DRY[13]+WET[3]*DRY[12]+WET[4]*DRY[11]+WET[5]*DRY[10]+WET[6]*DRY[9]+WET[7]*DRY[8]+WET[8]*DRY[7]+WET[9]*DRY[6]+WET[10]*DRY[5]+WET[11]*DRY[4]+WET[12]*DRY[3]+WET[13]*DRY[2]+WET[14]*DRY[1], WET[1]*DRY[15]+WET[2]*DRY[14]+WET[3]*DRY[13]+WET[4]*DRY[12]+WET[5]*DRY[11]+WET[6]*DRY[10]+WET[7]*DRY[9]+WET[8]*DRY[8]+WET[9]*DRY[7]+WET[10]*DRY[6]+WET[11]*DRY[5]+WET[12]*DRY[4]+WET[13]*DRY[3]+WET[14]*DRY[2]+WET[15]*DRY[1], WET[1]*DRY[16]+WET[2]*DRY[15]+WET[3]*DRY[14]+WET[4]*DRY[13]+WET[5]*DRY[12]+WET[6]*DRY[11]+WET[7]*DRY[10]+WET[8]*DRY[9]+WET[9]*DRY[8]+WET[10]*DRY[7]+WET[11]*DRY[6]+WET[12]*DRY[5]+WET[13]*DRY[4]+WET[14]*DRY[3]+WET[15]*DRY[2]+WET[16]*DRY[1], WET[1]*DRY[17]+WET[2]*DRY[16]+WET[3]*DRY[15]+WET[4]*DRY[14]+WET[5]*DRY[13]+WET[6]*DRY[12]+WET[7]*DRY[11]+WET[8]*DRY[10]+WET[9]*DRY[9]+WET[10]*DRY[8]+WET[11]*DRY[7]+WET[12]*DRY[6]+WET[13]*DRY[5]+WET[14]*DRY[4]+WET[15]*DRY[3]+WET[16]*DRY[2]+WET[17]*DRY[1], WET[1]*DRY[18]+WET[2]*DRY[17]+WET[3]*DRY[16]+WET[4]*DRY[15]+WET[5]*DRY[14]+WET[6]*DRY[13]+WET[7]*DRY[12]+WET[8]*DRY[11]+WET[9]*DRY[10]+WET[10]*DRY[9]+WET[11]*DRY[8]+WET[12]*DRY[7]+WET[13]*DRY[6]+WET[14]*DRY[5]+WET[15]*DRY[4]+WET[16]*DRY[3]+WET[17]*DRY[2]+WET[18]*DRY[1], WET[1]*DRY[19]+WET[2]*DRY[18]+WET[3]*DRY[17]+WET[4]*DRY[16]+WET[5]*DRY[15]+WET[6]*DRY[15]+WET[7]*DRY[13]+WET[8]*DRY[12]+WET[9]*DRY[11]+WET[10]*DRY[10]+WET[11]*DRY[9]+WET[12]*DRY[8]+WET[13]*DRY[7]+WET[14]*DRY[6]+WET[15]*DRY[5]+WET[16]*DRY[4]+WET[17]*DRY[3]+WET[18]*DRY[2]+WET[19]*DRY[1], WET[1]*DRY[20]+WET[2]*DRY[19]+WET[3]*DRY[18]+WET[4]*DRY[17]+WET[5]*DRY[16]+WET[6]*DRY[15]+WET[7]*DRY[14]+WET[8]*DRY[13]+WET[9]*DRY[12]+WET[10]*DRY[11]+WET[11]*DRY[10]+WET[12]*DRY[9]+WET[13]*DRY[8]+WET[14]*DRY[7]+WET[15]*DRY[6]+WET[16]*DRY[5]+WET[17]*DRY[4]+WET[18]*DRY[3]+WET[19]*DRY[2]+WET[20]*DRY[1], WET[1]*DRY[21]+WET[2]*DRY[20]+WET[3]*DRY[19]+WET[4]*DRY[18]+WET[5]*DRY[17]+WET[6]*DRY[16]+WET[7]*DRY[15]+WET[8]*DRY[14]+WET[9]*DRY[13]+WET[10]*DRY[12]+WET[11]*DRY[11]+WET[12]*DRY[10]+WET[13]*DRY[9]+WET[14]*DRY[8]+WET[15]*DRY[7]+WET[16]*DRY[6]+WET[17]*DRY[5]+WET[18]*DRY[4]+WET[19]*DRY[3]+WET[20]*DRY[2]+WET[21]*DRY[1], WET[1]*DRY[22]+WET[2]*DRY[21]+WET[3]*DRY[20]+WET[4]*DRY[19]+WET[5]*DRY[18]+WET[6]*DRY[17]+WET[7]*DRY[16]+WET[8]*DRY[15]+WET[9]*DRY[14]+WET[10]*DRY[13]+WET[11]*DRY[12]+WET[12]*DRY[11]+WET[13]*DRY[10]+WET[14]*DRY[9]+WET[15]*DRY[8]+WET[16]*DRY[7]+WET[17]*DRY[6]+WET[18]*DRY[5]+WET[19]*DRY[4]+WET[20]*DRY[3]+WET[21]*DRY[2]+WET[22]*DRY[1], WET[1]*DRY[23]+WET[2]*DRY[22]+WET[3]*DRY[21]+WET[4]*DRY[20]+WET[5]*DRY[19]+WET[6]*DRY[18]+WET[7]*DRY[17]+WET[8]*DRY[16]+WET[9]*DRY[15]+WET[10]*DRY[14]+WET[11]*DRY[13]+WET[12]*DRY[12]+WET[13]*DRY[11]+WET[14]*DRY[10]+WET[15]*DRY[9]+WET[16]*DRY[8]+WET[17]*DRY[7]+WET[18]*DRY[6]+WET[19]*DRY[5]+WET[20]*DRY[4]+WET[21]*DRY[3]+WET[22]*DRY[2]+WET[23]*DRY[1], WET[1]*DRY[24]+WET[2]*DRY[23]+WET[3]*DRY[22]+WET[4]*DRY[21]+WET[5]*DRY[20]+WET[6]*DRY[19]+WET[7]*DRY[18]+WET[8]*DRY[17]+WET[9]*DRY[16]+WET[10]*DRY[15]+WET[11]*DRY[14]+WET[12]*DRY[13]+WET[13]*DRY[12]+WET[14]*DRY[11]+WET[15]*DRY[10]+WET[16]*DRY[9]+WET[17]*DRY[8]+WET[18]*DRY[7]+WET[19]*DRY[6
[R] Testing for warning inside functions
I am looking for a way to get a warning message immediately after an evaluation within a function. To get error messages you can use geterrmessage(). But I found no function that allows me to check for warnings. Five years ago this questions has been posted but I haven't found any answer. Thanks for any help. Peter Wolf For illustration purpose a simple example follows: you can get warnings after an error occured or after exiting from the function but not at once after x-matrix(1:3,2,2): options(warn=0) f-function(x){ x-matrix(1:3,2,2) print(0) warnings() # no warning is shown print(1) try({xxx}) print(2) print(geterrmessage()) print(3) warnings() print(4) } f() [1] 0 [1] 1 Fehler in try({ : Objekt xxx nicht gefunden Zusätzlich: Warnmeldung: Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen [2] in matrix [1] 2 [1] Fehler in try({ : Objekt \xxx\ nicht gefunden\n [1] 3 Warning message: Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen [2] in matrix [1] 4 version platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major2 minor1.0 year 2005 month04 day 18 language R __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Testing for warning inside functions
Prof Brian Ripley wrote: On Wed, 18 May 2005, Peter Wolf wrote: I am looking for a way to get a warning message immediately after an evaluation within a function. To get error messages you can use geterrmessage(). Well, only in an error handler, as an error normally throws you out of the function. (That is the point of geterrmessage(), to enable error handlers to be written.) To print the message immediately, look up options(warn). options(warn=1) prints the message immediately, but I cannot store it in a variable to analyse it or to show the message it in a message box, for example. We don't have a general warnings handler mechanism, but you can make use of condition objects. Thank you, I will have a look at condition objects. But I found no function that allows me to check for warnings. Five years ago this questions has been posted but I haven't found any answer. What exactly is `this question'? I see several assertions but no question. (It would have been helpful to give an exact reference to the archives.) link to the old mail: R-help archive Jan - June 2000: * [R] Testing for warning inside functions -- V/idhyanath Rao (Sun 07 May 2000 - 12:46:38 EST)/ Message-ID: [EMAIL PROTECTED] This is probably a newbie question: How do I test, inside a function, is a call I made from inside that function produced a warning? ... Thanks for any help. Peter Wolf For illustration purpose a simple example follows: you can get warnings after an error occured or after exiting from the function but not at once after x-matrix(1:3,2,2): options(warn=0) f-function(x){ x-matrix(1:3,2,2) print(0) warnings() # no warning is shown print(1) try({xxx}) print(2) print(geterrmessage()) print(3) warnings() print(4) } f() [1] 0 [1] 1 Fehler in try({ : Objekt xxx nicht gefunden Zusätzlich: Warnmeldung: Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen [2] in matrix [1] 2 [1] Fehler in try({ : Objekt \xxx\ nicht gefunden\n [1] 3 Warning message: Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen [2] in matrix [1] 4 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] hex format
Steve Vejcik wrote: Thanks for your advice. Unfortunately, your answers are inconsistent: as.numeric(0x1AF0) returns a decimal value for a hex string. I'd like to do the opposite-use hex notation to represent a decimal. e.g. x-0x000A y-0x0001 x+y=0x00B Cheers. you can use chcode() to define hex.to.dec(), dec.to.hex() and sum.hex() to operate with hex numbers. Peter Wolf -- define chcode= chcode - function(b, base.in=2, base.out=10, digits=0123456789ABCDEF){ # change of number systems, pwolf 10/02 # e.g.: from 2 2 2 2 ... - 16 16 16 ... digits-substring(digits,1:nchar(digits),1:nchar(digits)) if(length(base.in)==1) base.in - rep(base.in, max(nchar(b)-1)) if(is.numeric(b)) b - as.character(as.integer(b)) b.num - lapply(strsplit(b,), function(x) match(x,digits)-1 ) result - lapply(b.num, function(x){ cumprod(rev(c(base.in,1))[ 1:length(x) ] ) %*% rev(x) } ) number-unlist(result) cat(decimal representation:,number,\n) if(length(base.out)==1){ base.out-rep(base.out,1+ceiling(log( max(number), base=base.out ) ) ) } n.base - length(base.out); result - NULL for(i in n.base:1){ result - rbind(number %% base.out[i], result) number - floor(number/base.out[i]) } result[]-digits[result+1] apply(result, 2, paste, collapse=) } @ define hex.to.dec, dec.to.hex and sum.hex= hex.to.dec-function(x) as.numeric(chcode(x, base.in=16, base.out=10)) dec.to.hex-function(x) chcode(x, base.in=10, base.out=16) sum.hex-function(x,y) dec.to.hex(hex.to.dec(x) + hex.to.dec(y)) @ quick test: define hex numbers= a-dec.to.hex(10); print(a) b-dec.to.hex(3);print(b) @ output-start decimal representation: 10 [1] 0A decimal representation: 3 [1] 03 output-end @ sum of a and b= sum.hex(a,b) @ output-start decimal representation: 10 decimal representation: 3 decimal representation: 13 Thu Apr 7 17:31:42 2005 [1] 0D output-end __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Hexidecimal conversion
Some years ago I wrote a function to convert a number from number system base.in to number system base.out. Here is the code: *= chcode - function(b, base.in=2, base.out=10, digits=0123456789ABCDEF){ # change of number systems, pw 10/02 # e.g.: from 2 2 2 2 ... - 16 16 16 ... digits-substring(digits,1:nchar(digits),1:nchar(digits)) if(length(base.in)==1) base.in - rep(base.in, max(nchar(b)-1)) if(is.numeric(b)) b - as.character(as.integer(b)) b.num - lapply(strsplit(b,), function(x) match(x,digits)-1 ) result - lapply(b.num, function(x){ cumprod(rev(c(base.in,1))[ 1:length(x) ] ) %*% rev(x) } ) number-unlist(result) cat(decimal representation:,number,\n) if(length(base.out)==1){ base.out-rep(base.out,1+ceiling(log( max(number), base=base.out ) ) ) } n.base - length(base.out); result - NULL for(i in n.base:1){ result - rbind(number %% base.out[i], result) number - floor(number/base.out[i]) } result[]-digits[result+1] apply(result, 2, paste, collapse=) } @ ... a short check: *= chcode(9F,16,10) @ output-start decimal representation: 159 [1] 0159 output-end @ ... conversion backwards: *= chcode(159,10,16) @ output-start decimal representation: 159 [1] 09F output-end Is chcode the function you are looking for? Peter Wolf Hanke, Alex wrote: Help I can produce the hexidecimal equivalent of a decimal number but I am having a hard time reversing the operation. I'm good for hex representations to 159 and am close to extending to 2559. The archives are not clear on the existence of a function for this task. Is there one? Here is what I have got so far: #Good for hex values to 9F as.decmode-function(as.character(x)){ hexDigit-c(0:9,LETTERS[1:6]) z-matrix(c(strsplit(x, NULL),recursive=T), length(x),2,byrow=T) z.1-as.numeric(z[,1]) z.2- match(z[,2],hexDigit)-1 dec-16*z.1+z.2 return(dec) } Alex Hanke Department of Fisheries and Oceans St. Andrews Biological Station 531 Brandy Cove Road St. Andrews, NB Canada E5B 2L9 [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] how to get to interesting part of pattern match
Vadim Ogranovich wrote: Hi, I am looking for a way to extract an interesting part of the match to a regular expression. For example the pattern [./](*.) matches a substring that begins with either . or / followed by anything. I am interested in this anything w/o the . or / prefix. If say I match the pattern against abc/foo I want to get foo, not /foo. In Perl one can simply wrap the interesting part in () and get it out of the match. Is it possible to do a similar thing in R? There seems to be a way to refer to the match, see below, but I couldn't figure out how to make gsub return it. gsub([./](*.), \\1, abc/foo) [1] abcfoo Thanks, Vadim __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html what about: gsub(.*[./](*.), \\1, abc/foo) output-start [1] foo output-end or try: strsplit(abc/foo,/)[[1]][2] output-start [1] foo output-end Peter Wolf __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] crossprod vs %*% timing
You can study that the order of the operation has an effect on the times of the computations. *= f1 - function(a,X){ ignore - t(a) %*% X %*% a } f2 - function(a,X){ ignore - crossprod(t(crossprod(a,X)),a) } f3 - function(a,X){ ignore - crossprod(a,X) %*% a } f4 - function(a,X){ ignore - (t(a) %*% X) %*% a } f5 - function(a,X){ ignore - t(a) %*% (X %*% a) } f6 - function(a,X){ ignore - crossprod(a,crossprod(X,a)) } a - rnorm(100); X - matrix(rnorm(1),100,100) print(system.time( for(i in 1:1){ a1-f1(a,X)})) print(system.time( for(i in 1:1){ a2-f2(a,X)})) print(system.time( for(i in 1:1){ a3-f3(a,X)})) print(system.time( for(i in 1:1){ a4-f4(a,X)})) print(system.time( for(i in 1:1){ a5-f5(a,X)})) print(system.time( for(i in 1:1){ a6-f6(a,X)})) c(a1,a2,a3,a4,a5,a6) @ output-start [1] 4.06 0.04 4.11 0.00 0.00 [1] 1.48 0.00 1.53 0.00 0.00 [1] 1.17 0.00 1.22 0.00 0.00 [1] 4.10 0.01 4.39 0.00 0.00 [1] 2.58 0.01 3.24 0.00 0.00 [1] 1.10 0.00 1.29 0.00 0.00 Wed Oct 6 11:26:38 2004 [1] -79.34809 -79.34809 -79.34809 -79.34809 -79.34809 -79.34809 output-end Peter Wolf Robin Hankin wrote: Hi the manpage says that crossprod(x,y) is formally equivalent to, but faster than, the call 't(x) %*% y'. I have a vector 'a' and a matrix 'A', and need to evaluate 't(a) %*% A %*% a' many many times, and performance is becoming crucial. With f1 - function(a,X){ ignore - t(a) %*% X %*% a } f2 - function(a,X){ ignore - crossprod(t(crossprod(a,X)),a) } f3 - function(a,X){ ignore - crossprod(a,X) %*% a } a - rnorm(100) X - matrix(rnorm(1),100,100) print(system.time( for(i in 1:1){ f1(a,X)})) print(system.time( for(i in 1:1){ f2(a,X)})) print(system.time( for(i in 1:1){ f3(a,X)})) I get something like: [1] 2.68 0.05 2.66 0.00 0.00 [1] 0.48 0.00 0.49 0.00 0.00 [1] 0.29 0.00 0.31 0.00 0.00 with quite low variability from run to run. What surprises me is the third figure: about 40% faster than the second one, the extra time possibly related to the call to t() (and Rprof shows about 35% of total time in t() for my application). So it looks like f3() is the winner hands down, at least for this task. What is a good way of thinking about such issues? Anyone got any performance tips? I quite often need things like 'a %*% X %*% t(Y) %*% Z %*% t(b)' which would be something like crossprod(t(crossprod(t(crossprod(t(crossprod(a,X)),t(Y))),Z)),t(b)) (I think). (R-1.9.1, 2GHz G5 PowerPC, MacOSX10.3.5) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] multiple dimensional diag()
Robin Hankin wrote: hi again Peter thanks for this. Now we've got the legal stuff sorted, on to business! One of the reasons I wanted the function was to to multidimensional moving- window averaging, and for this I needed to be able to call a.block.diag(a,b) when a and b might have one or more dimensions of zero extent. I also figure that a scalar argument should be interpreted as having dimensions rep(1,d) where d=length(dim(other matrix)), and that a.block.diag(n,m) should return plain old diag(n,m) if both n and m are scalars. I've modified your function do to this, and added a padding value argument (function and a couple of calls pasted below) what do you think? perfect! ... and if you want to combine more than two arrays you can add a function like mbd mbd-function(...,pad=0){ result-(args-list(...))[[1]] for(li in args[-1]) result-a.block.diag(result,li,pad=pad) return(result) } a - matrix(1:4,2,2) mbd(a,a,a,a,pad=10) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]13 10 10 10 10 10 10 [2,]24 10 10 10 10 10 10 [3,] 10 1013 10 10 10 10 [4,] 10 1024 10 10 10 10 [5,] 10 10 10 1013 10 10 [6,] 10 10 10 1024 10 10 [7,] 10 10 10 10 10 1013 [8,] 10 10 10 10 10 1024 ... or integrate the loop into a.block.diag Peter Wolf best wishes Robin a.block.diag - function(a,b,pad=0) { ## a.block.daig combines arrays a and b and builds a new array which has ## a and b as blocks on its diagonal. pw 10/2004 if( (length(a)==1) (length(b)==1) ){return(diag(c(a,b)))} if(length(a)==1){dim(a) - rep(1,length(dim(b)))} if(length(b)==1){dim(b) - rep(1,length(dim(a)))} if(length(dim.a - dim(a)) != length(dim.b - dim(b))){ stop(a and b must have identical number of dimensions) } seq.new - function(i){if(i==0){return(NULL)} else {return(1:i)}} s - array(pad, dim.a+dim.b) s - do.call([-,c(list(s),lapply(dim.a,seq.new),list(a))) ind - lapply(seq(dim.b),function(i)seq.new(dim.b[[i]])+dim.a[[i]]) do.call([-,c(list(s),ind,list(b))) } a - matrix(1:4,2,2) a [,1] [,2] [1,]13 [2,]24 b - array(1e44,c(0,3)) b [,1] [,2] [,3] a.block.diag(a,b) [,1] [,2] [,3] [,4] [,5] [1,]13000 [2,]24000 Before I go any further, I need to check that it's OK with you for me to put this function (or modifications of it) into my package, which is GPL. Full authorship credit given. Is this OK? Hallo Robin, you are welcome to put a.block.diag() in your package. The function is free software; you can redistribute it and/or modify it under the terms of the GNU... Peter Wolf __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] multiple dimensional diag()
Here is a function that does what you want (perhaps): *= a.block.diag - function(a,b) { # a.block.daig combines arrays a and b and builds a new array which has # a and b as blocks on its diagonal. pw 10/2004 if(length(dim.a-dim(a))!= length(dim.b-dim(b))){ stop(a and b must have identical number of dimensions)} s-array(0,dim.a+dim.b) s-do.call([-,c(list(s),lapply(dim.a,seq),list(a))) ind-lapply(seq(dim.b),function(i)seq(dim.b[[i]])+dim.a[[i]]) do.call([-,c(list(s),ind,list(b))) } @ Try: *= a=matrix(1,3,4); b=matrix(2,2,2) a.block.diag(a,b) @ output-start Fri Oct 1 17:20:26 2004 [,1] [,2] [,3] [,4] [,5] [,6] [1,]111100 [2,]111100 [3,]111100 [4,]000022 [5,]000022 output-end and an another example: *= xx-array(1:8,c(2,rep(2,2))); yy-array(-1*(1:9),c(2,rep(3,2))) a.block.diag(xx,yy) @ output-start Fri Oct 1 17:21:38 2004 , , 1 [,1] [,2] [,3] [,4] [,5] [1,]13000 [2,]24000 [3,]00000 [4,]00000 , , 2 [,1] [,2] [,3] [,4] [,5] [1,]57000 [2,]68000 [3,]00000 [4,]00000 , , 3 [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]00000 [3,]00 -1 -3 -5 [4,]00 -2 -4 -6 , , 4 [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]00000 [3,]00 -7 -9 -2 [4,]00 -8 -1 -3 , , 5 [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]00000 [3,]00 -4 -6 -8 [4,]00 -5 -7 -9 output-end a.block.diag is not always the best solution. In case of x-array(1:8,rep(2,3)); y-array(-1,rep(2,3)) the function adiag will be a little bit faster. Peter Wolf Robin Hankin wrote: Hi I have two arbitrarily dimensioned arrays, a and b, with length(dim(a))==length(dim(b)). I want to form a sort of corner-to-corner version of abind(), or a multidimensional version of blockdiag(). In the case of matrices, the function is easy to write and if a=matrix(1,3,4) and b=matrix(2,2,2), then adiag(a,b) would return: [,1] [,2] [,3] [,4] [,5] [,6] [1,]111100 [2,]111100 [3,]111100 [4,]000022 [5,]000022 I am trying to generalize this to two higher dimensional arrays. If x - adiag(a,b) then I want all(dim(x)==dim(a)+dim(b)); and if dim(a)=c(a_1, a_2,...a_d) then x[1:a_1,1:a_2,...,1:a_d]=a, and x[(a_1+1):(a_1+b_1),...,(a_d+1):(a_d+b_d)]=b. Other elements of x are zero. The fact that I'm having difficulty expressing this succinctly makes me think I'm missing something basic. If a and b have identical dimensions [ie all(dim(a)==dim(b)) ], the following ghastly kludge (which is one of many) works: adiag - function(a,b) { if(any(dim(a) != dim(b))){stop(a and b must have identical dimensions)} jj - array(0,rep(2,length(dim(a jj[1] - 1 jj[length(jj)] - 1 jj - kronecker(jj,b) f - function(i){1:i} do.call([-,c(list(jj),sapply(dim(a),f,simplify=FALSE),list(a))) } Then adiag(array(1:8,rep(2,3)),array(-1,rep(2,3))) is OK. What is the best way to bind arbitrarily dimensioned arrays together corner-to-corner? __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] loops: pasting indexes in variables names
Umberto Maggiore wrote: I cannot figure out how, using R, I can paste indexes or characters to the variable names which are used within loops. I will explain this with a simple example: Immagine I have a huge series of variables, each one taken two times, say x1 x2 y1 y2 z1 z2. Now, immagine that I want to compute a variable from the difference of each couple, say dx=x1-x2, dy=y1-y2, dz=z1-z2... In Stata, for example, this wold be straightforward: foreach i in x y z { gen d`i'= `i'1-`i'2 } With R I tried to use paste( ) but I found that it applies to objects, not to variable names. best regards, Umberto __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Try: x1-10 x2-20 y1-5 y2-22 z1-4 z2-7 for(i in c(x,y,z)){ eval(parse(text=paste(d,i,-,i,1 - ,i,2,sep=))) } ls(pattern=d) output-start Wed Sep 22 14:38:28 2004 [1] dx dy dz output-end but why don't you store x1,y1,z1 and x2,y2,z2 in a list: a-list(x=1:4, y=1:7, z=1:5) b-list(x=(1:4)*10, y=1:7, z=(1:5)-20) d-sapply(1:3, function(i) a[[i]]-b[[i]] ) @ output-start Wed Sep 22 14:43:09 2004 [[1]] [1] -9 -18 -27 -36 [[2]] [1] 0 0 0 0 0 0 0 [[3]] [1] 20 20 20 20 20 output-end Peter Wolf __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] persiting complex R objects
Richard Mott wrote: Is there a method to save a large and complex R object (either as a binary or text file) so that it can be loaded and reused at a later time? Specifically, I am creating large lists (several thousand elements), each element of which is either a vector or a matrix (with ~ 2000 rows). The dimensions of the matrices are not all the same. My ideal would be a set of functions of the form obj - create() # computes the object save(obj,filename) obj - load(filename) Have a look at ? dump ? source Peter Wolf __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] efficient submatrix extraction
there are two main ideas to improve the efficiency: 1. the comparison with the limit can be done at first 2. a matrix with boxsize*boxsize rows can be defined so that you can apply function apply without using inner loops *= # parameters size-1024; limit-0.7 # some data set.seed(17); data-runif(size^2) m-matrix(data,size,size) bcount.vec-NULL m-mlimit for (boxsize in 2^(1:8)) { # inner loop: m-array(m,c(boxsize,size/boxsize,size)) m-aperm(m,c(1,3,2)) m-matrix(m,nrow=boxsize*boxsize) bcount.vec-c(bcount.vec,sum(apply(m,2,max))) } bcount-sum(bcount.vec) print(bcount.vec) print(bcount) @ output-start [1] 199099 65306 16384 4096 1024256 64 16 [1] 286245 output-end Some remarks on your code: The first expression in your outer loop is setting bcount to 0. In the inner loops bcount is incremented. But the outer loop sets bcount to the value 0 again!? What do you want to count? Peter Rajarshi Guha wrote: Hi, I have a matrix of say 1024x1024 and I want to look at it in chunks. That is I'd like to divide into a series of submatrices of order 2x2. | 1 2 3 4 5 6 7 8 ... | | 1 2 3 4 5 6 7 8 ... | | 1 2 3 4 5 6 7 8 ... | | 1 2 3 4 5 6 7 8 ... | ... So the first submatrix would be | 1 2 | | 1 2 | the second one would be | 3 4 | | 3 4 | and so on. That is I want the matrix to be evenly divided into 2x2 submatrices. Now I'm also doing this subdivision into 4x4, 8x8 ... 256x256 submatrices. Currently I'm using loops and I'm sure there is a mroe efficient way to do it: m - matrix(runif(1024*1024), nrow=1024) boxsize - 2^(1:8) for (b in boxsize) { bcount - 0 bstart - seq(1,1024, by=b) for (x in bstart) { for (y in bstart) { xend - x + b - 1 yend - y + b - 1 if (length(which( m[ x:xend, y:yend ] 0.7)) 0) { bcount - bcount + 1 } } } } Is there any way to vectorize the two inner loops? Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- The way to love anything is to realize that it might be lost. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] reshaping some data
Try: x - data.frame(x1 = 1: 5, y11 = 1: 5, x2 = 6:10, y21 = 6:10, y22 = 11:15, x3 = 11:15, y31 = 16:20, x4 = 16:20, y41 = 21:25, y42 = 26:30, y43 = 31:35) df.names-names(x) ynames-df.names[grep(y,df.names)] xnames-substring(sub(y,x,ynames),1,2) cbind(unlist(x[,xnames]),unlist(x[,ynames])) Peter Sundar Dorai-Raj wrote: Hi all, I have a data.frame with the following colnames pattern: x1 y11 x2 y21 y22 y23 x3 y31 y32 ... I.e. I have an x followed by a few y's. What I would like to do is turn this wide format into a tall format with two columns: x, y. The structure is that xi needs to be associated with yij (e.g. x1 should next to y11 and y12, x2 should be next to y21, y22, and y23, etc.). x y x1 y11 x2 y21 x2 y22 x2 y23 x3 y31 x3 y32 ... I have looked at ?reshape but I didn't see how it could work with this structure. I have a solution using nested for loops (see below), but it's slow and not very efficient. I would like to find a vectorised solution that would achieve the same thing. Now, for an example: x - data.frame(x1 = 1: 5, y11 = 1: 5, x2 = 6:10, y21 = 6:10, y22 = 11:15, x3 = 11:15, y31 = 16:20, x4 = 16:20, y41 = 21:25, y42 = 26:30, y43 = 31:35) # which are the x columns nmx - grep(^x, names(x)) # which are the y columns nmy - grep(^y, names(x)) # grab y values y - unlist(x[nmy]) # reserve some space for the x's z - vector(numeric, length(y)) # a loop counter k - 0 n - nrow(x) seq.n - seq(n) # determine how many times to repeat the x's repy - diff(c(nmx, length(names(x)) + 1)) - 1 for(i in seq(along = nmx)) { for(j in seq(repy[i])) { # store the x values in the appropriate z indices z[seq.n + k * n] - x[, nmx[i]] # move to next block in z k - k + 1 } } data.frame(x = z, y = y, row.names = NULL) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] advanced dist
Martin Klaffenboeck wrote: Hi there. I have now found that dist() does what I want in a specific case. But I have (out of many fields) in my Fragebogen.data file: VpnCodeFamilyTest 1X1m45 2X1t58 3X2m44 4X2t43 ... When I now do: Fbg - read.table(Fragebogen.data, header=TRUE) dist(Fbg['Test']) I have the distances between everyone to everyone. Now I want to have the mothers (m in Family) at the y axis and the daugthers (t) on the x axis, so I have half the size of my distance matrix. Is that possible somehow? And if possible, I would have the 'Code' at the top to know which mother and wich daugther are compared. Thanks, Martin __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html what about: mtdist- as.matrix(dist(Fbg['Test'])) dimnames(mtdist) - list( Fbg[ 'Family' ], Fbg[ 'Family' ] ) mtdist [ Fbg['Code']==m ,Fbg['Code']==t ] Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Making a ranking algorithm more efficient
let's start by defining x and y matching the properties of the points in the picture *= x-c(1,2,4,5,9,3,7,6); y-c(10,8,5,3,2,9,4,7) plot(x,y,pch=letters[1:8]) @ along each dimension we have to compare the coordinates of the points: *= outer(x,x,) @ we get a logical matrix: output-start Wed Jun 2 10:36:52 2004 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE [3,] FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE [4,] FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE [7,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE [8,] FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE output-end results of dimension x and of dimension y have to be aggregated by *= outer(x,x,)outer(y,y,) @ and we get: output-start Wed Jun 2 10:44:56 2004 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE [4,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE output-end TRUE in position (i,j) indicates that x- and y-coordinates of point i are smaller than those of point j. The sum of row i shows the number of points that dominate point i. The sum of col j shows the number of points that are dominated by point j. @ so we can compute the the number of dominated points by applying function sum on the cols: *= apply(outer(x,x,)outer(y,y,),2,sum) @ at last some polishing *= result-apply(outer(x,x,)outer(y,y,),2,sum)+1 names(result)-letters[1:8] result @ output-start Wed Jun 2 10:59:03 2004 a b c d e f g h 1 1 1 1 1 2 2 3 output-end in the case of more than 2 dimensions you have to add further outer-operations Peter Wolf Waichler, Scott R wrote: I would like to make a ranking operation more efficient if possible. The goal is to rank a set of points representing objective function values such that points which are dominated by no others have rank 1, those which are dominated by one other point have rank 2, etc. In the example with two dimensions below, objective functions 1 and 2 are to be minimized. Points a-e are non-dominated, rank 1 points. Point f is rank 2 because point b is superior, and point g is rank 2 because point d is superior. Point h is rank 3 because points c and d are both superior. | a |f | b | h (figure requires monospaced, plain-text display) Obj 1 | c | g | d |e | Obj 2 I need to compute the ranks of the rows of a matrix, where each row represents a point in objective space and the columns contain the objective function values to be minimized. Rank is defined as the number of points with objective function values less than or equal to the current point (including the current point). I've written the following function with two loops: PARETO.RANK - function(obj.array) { obj.array - unique(obj.array) ind.row - 1:nrow(obj.array) ind.col - 1:ncol(obj.array) rank.vec - rep(NA, length(ind.row)) # initialize # Loop thru rows (points in objective function space) for(i in ind.row) { set - ind.row for (j in ind.col) { # Loop thru objective functions set - set[ obj.array[set,j] = obj.array[i,j] ] } rank.vec[i] - length(set) } return(rank.vec) } # end PARETO.RANK3() Can anyone think of a way to do this more efficiently, for example by vectorizing further? Thanks, Scott Waichler [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] syntax error in function 'for'
Ulrich Leopold wrote: Dear list, I get a syntax error for the following function: for(na.omit(sqrt(D))2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C} Any idea what is wrong? I am using R 1.8.1 on Linux, Kernel 2.4.21-i686. Regards, Ulrich have a look at help(for): Control package:base R Documentation Control Flow Description: ... Usage: if(cond) expr if(cond) cons.expr else alt.expr for(var in seq) expr ... Examples: for(i in 1:5) print(1:i) for(n in c(2,5,10,20,50)) { x - rnorm(n) cat(n,:, sum(x^2),\n) } Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] syntax error in function 'for'
Ulrich Leopold wrote: On Wed, 2004-05-05 at 17:11, Peter Dalgaard wrote: Ulrich Leopold [EMAIL PROTECTED] writes: Dear list, I get a syntax error for the following function: for(na.omit(sqrt(D))2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C} Any idea what is wrong? Yes, that's not the syntax for a for loop. I have no idea what your intention might have been, though. Ok thanks. I think I misunderstood the example in the help. I think, I rather need an if function. I would like to calculate the right hand side if the condition on the left hand side is met. I am afraid I do not quite understand the syntax. if(na.omit(sqrt(D))2) {na.omit(A)-(na.omit(B)+(na.omit(sqrt(C} Now I get the following warning: the condition has length 1 and only the first element will be used in: if(na.omit(sqrt(D))2) { What is the result of the condition (1:10) 2 ? What should be done? Read the text of the warning again. Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Superposing data on boxplot
What about: y-rnorm(100);z-sample(1:7,100,T);boxplot(y~z);points(y~z) Peter Wolf Ted Harding wrote: Hi folks, I have a vaiable Y and an associated factor Z at several (13) levels. boxplot(Y~Z) produces a nice array of boxplots, one for each level of Z, and each duly labaelled with its level of Z. I would like to superpose on each boxplot the actual data points which it represents, i.e. do something conceptually (though not in real R) expressed as points(Y~Z) or points(Z,Y) It can be done with bare hands along the lines of B-boxplot(Y~Z) x-bxp(B) X-rep(x,ngroups) points(X,Y) where ngroups is the number of data at each level of Z, *provided* the data are pre-sorted by level of Z and in the same order as these levels occurr in the boxplot. But of course they're not! OK, I could do this by hand as well, but now it's getting a bit tedious, and I'm wondering if there's a better way. Thanks for any suggestions, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 04-May-04 Time: 15:48:23 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] rbind with missing columns
here is another solution: A - data.frame(a = c(1,2,3), b = c(4,5,6)) B - data.frame(a = c(1,2,3)) C - data.frame(b=4:6) rbind.data.frame.NA-function(...){ N-unique(unlist(lapply(list(...),names))) result-NULL for(DF in list(...)){ x-as.data.frame(lapply(N,function(x)if(x %in% names(DF)) DF[,x] else NA)) names(x)-N result-rbind(result,x) } result } rbind.data.frame.NA(B,A,C) @ output-start Fri Apr 30 15:21:21 2004 a b 1 1 NA 2 2 NA 3 3 NA 11 1 4 21 2 5 31 3 6 12 NA 4 22 NA 5 32 NA 6 output-end Peter Wolf Duncan Murdoch wrote: On Fri, 30 Apr 2004 13:47:35 +0200, [EMAIL PROTECTED] wrote : Hello, I have several data sets, which I want to combine column-by-column using rbind (the data sets have ~100 columns). The problem is, that in some data sets some of the columns are missing. Simply using rbind gives me: Error in match.names(clabs, names(xi)) : names don't match previous names Is there a simple way to make R do the rbind despite the missing columns and to fill out the missing data with NA's? (I could program this somehow, but probably there is one very simple solution available) It's not there already as far as I know, but it's not too hard to write a new rbind that does this: newrbind - function(A,B) { anames - names(A) bnames - names(B) notinb - anames[!(anames %in% bnames)] if (length(notinb)) B[[notinb]] - rep(NA, nrow(B)) notina - bnames[!(bnames %in% anames)] if (length(notina)) A[[notina]] - rep(NA, nrow(A)) rbind(A, B) } This only works on data.frames; if you want it to work as generally as the real rbind, you'll need to add some extra conversions at the start. Duncan Murdoch __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] accessing information in lists
Try: *= rev.matrix-matrix(rnorm(40),10,4) rev.acf-apply(rev.matrix, 2, acf, na.action=na.contiguous, lag.max=12, plot=FALSE) x.acf-lapply(rev.acf,function(x) x[[acf]][,,1]) matrix(unlist(x.acf),ncol=4) output-start Thu Apr 29 09:56:22 2004 [,1] [,2][,3][,4] [1,] 1. 1.0 1. 1. [2,] -0.18009242 -0.066131687 -0.19268167 -0.25340270 [3,] -0.15442666 -0.342732543 -0.30959136 -0.11987720 [4,] 0.14785268 0.074751553 0.16748753 -0.16495692 [5,] -0.34024505 0.210414077 -0.03338385 0.25205651 [6,] -0.05562243 0.044047730 0.02971677 -0.05640866 [7,] 0.09955468 -0.358754179 -0.02301302 -0.11177397 [8,] 0.01609530 0.072731988 -0.16732203 -0.25562259 [9,] -0.04621386 -0.005510738 -0.13049985 0.23890998 [10,] 0.01309776 -0.128816203 0.15928749 -0.02892446 output-end BTW -- do not use the name t for a variable, parameter, etc. because there is a function t(). Peter Wolf Murray Keir wrote: I've created a dataframe containing multiple ACF lists through the command rev.acf-apply(rev.matrix, 2, acf, na.action=na.contiguous, lag.max=12, plot=FALSE) where rev.matrix is an n by t matrix containing n time series in columns. I'd now like to pull out only the ACF information and store it in a seperate n by 12 matrix. So far the only way I can work out to access this is rev.acf[[series1]][[acf]] rev.acf[[series2]][[acf]] etc Is there some way I can do this automatically? Thanks Murray [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] GUI checkbox linked to entry
Julien Glenat wrote: Hi , i am using R 1.8 and tcltk library in order to make a GUI and i would like to know if it is possible to link a check box to one or more entry dynamically (and eventually other widgets) . ( in concrete terms : if the check box is ticked the entry is avaible for input and unavaible when not ticked) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Are you looking for something like this? require(tcltk) cbValue - tclVar(0); info-tclVar(initial text) top - tktoplevel() cb - tkcheckbutton(top,variable=cbValue,text=write or not) en - tkentry (top,textvariable=info,state=disabled) exit- tkbutton (top, text=exit, command=function() tkdestroy(top)) tkpack(cb,en,exit) set.entry.state-function(){ on.off-tclvalue(cbValue) if(on.off==1) tkconfigure(en, state=normal) if(on.off==0) tkconfigure(en, state=disabled) print(on.off) } tkbind(top,1,set.entry.state) Do you know http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples? Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Tcl Tk table
[EMAIL PROTECTED] wrote: Hi I've a problem with the following example: library(tcltk) .Tcl(array unset tclArray) myRarray - matrix(1:1000, ncol=20) for (i in (0:49)) for (j in (0:19)) .Tcl(paste(set tclArray(,i,,,j,) ,myRarray[i+1,j+1],sep=)) tt-tktoplevel() table1 - tkwidget(tt,table,variable=tclArray, rows=50, cols=50) tkpack(table1) #Old version which worked in R 1.6 but it doesn't work with R 1.9 (and also not with 1.8), why?? . Under R version 1.6 I had no problem. Now I installed R 1.9 (with ActiveTcl) and my program doesn't work. ... Thomas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Are you sure? library(tcltk) .Tcl(array unset tclArray) Tcl myRarray - matrix(1:1000, ncol=20) for (i in (0:49)) + for (j in (0:19)) + .Tcl(paste(set tclArray(,i,,,j,) ,myRarray[i+1,j+1],sep=)) tt-tktoplevel() table1 - tkwidget(tt,table,variable=tclArray, rows=50, cols=50) Error in structure(.External(dotTcl, ..., PACKAGE = tcltk), class = tclObj) : [tcl] invalid command name table. version _ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major1 minor6.2 year 2003 month01 day 10 language R the error occurs with version 1.6.2, too. Some debugging shows: the error message comes from tcl/tk and you need some additional features to be allowed to use the table-widget! you have to include: tclRequire(Tktable) ... and all is ok for 1.8.1 and I also hope for 1.9.0. see: http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/tktable.html Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] x-only zoom and pan?
Hallo here is a simple proposal using tcltk-sliders. Peter Wolf # step 1: define general slider function slider-function(refresh.code,names,minima,maxima,resolutions,starts,title=control,no=0, set.no.value=0){ # pw 03/2004 if(no!=0) return(as.numeric(tclvalue(get(paste(slider,no,sep=),env=slider.env if(set.no.value[1]!=0){ try(eval(parse(text=paste(tclvalue(slider,set.no.value[1],)-, set.no.value[2],sep=)),env=slider.env)); return(set.no.value[2]) } if(!exists(slider.env)) slider.env-new.env() library(tcltk); nt-tktoplevel(); tkwm.title(nt,title); tkwm.geometry(nt,+0+0) for(i in seq(names)) eval(parse(text=paste(assign(\slider,i,\,tclVar(starts[i]),env=slider.env),sep=))) for(i in seq(names)){ tkpack(fr-tkframe(nt)); lab-tklabel(fr, text=names[i], width=25) sc-tkscale(fr, command=refresh.code, from=minima[i], to=maxima[i], showvalue=T, resolution=resolutions[i], orient=horiz) assign(sc,sc,env=slider.env); tkpack(lab,sc,side=right) eval(parse(text=paste(tkconfigure(sc,variable=slider,i,),sep=)),env=slider.env) } tkpack(fr-tkframe(nt),fill=x) tkpack(tkbutton(fr, text=Exit, command=function()tkdestroy(nt)),side=right) tkpack(tkbutton(fr, text=Reset, command=function(){ for(i in seq(starts)) eval(parse(text=paste(tclvalue(slider,i,)-,starts[i],sep=)),env=slider.env) refresh.code()} ),side=left) } # step 2: define function for zooming ts.zoom-function(x,y,z){ # pw 05042004 library(tcltk) n.ts-1; if(!missing(y)) n.ts-2; if(!missing(z)) n.ts-3 refresh.code-function(...){ # initialization start-slider(no=1)*100; delta-slider(no=2)*100 if(start+deltalength(x)) start-slider(set.no.value=c(1,(length(x)-delta)/100))*100 # plot par(mfrow=c(n.ts,1)) plot(x,type=l,xlim=c(start,start+delta)) if(n.ts=2) plot(y,type=l,xlim=c(start,start+delta)) if(n.ts=3) plot(z,type=l,xlim=c(start,start+delta)) par(mfrow=c(1,1)) } slider(refresh.code, # names of sliders c(begin index (unit=100), window width (unit=100)), # min of sliders c(0,1), # max of sliders c(floor(length(x)/100),length(x)/100), # step of sliders c(1,1), # initial values c(1,1)) } # step 3: test it. ts.zoom(runif(1), rexp(1), rnorm(1)) Randy Zelick wrote: Hello list, Could the following be done without too much grief...? Lets say I have two or three time series objects that I want to inspect visually. Each I would plot with a y-offset so they stack up. They share the same X scaling. The problem is that each is perhaps 100K values. Due to the large number of values, features of the data sets cannot be seen when all values are plotted. What would be nice is to plot a fraction of the X range (say 10%). This would be equivalent to zooming in the X direction. Then using a key (ideally an arrow key), shift the viewing frame right or left to effectively scroll through the data. So first you view 0-10%, then 10-20% and so forth. If necessary I can fabricate a vector with X values in it and plot(x,y) instead of as time series, if this makes it any easier. I am using a Windows version of R. Thanks, =Randy= R. Zelick email: [EMAIL PROTECTED] Department of Biology voice: 503-725-3086 Portland State University fax: 503-725-3888 mailing: P.O. Box 751 Portland, OR 97207 shipping: 1719 SW 10th Ave, Room 246 Portland, OR 97201 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Zero Index Origin?
Sorting is a wonderful topic! Especially because you can discuss different fundamental ideas like brute force, divide and conquer, and questions of efficiency, tradeoffs of space and time, etc. In this way sort.6 is one of a sequence of sorting algorithms. Each of them demonstrates a specific point for teaching purposes. You are right: in sort.6 a[0] plays the role of a sentinel. So the main points are: 1) What is the new idea found in sort.6 (compared to sort.5 -- a solution without sentinel)? 2) Are there other ideas for improvements concerning space and time (this leads to sort.7, etc.)? 3) What about readability: what is the best notation for human readers? 4) What should be considered and changed in the light of a specific computer language? Your remarks show that often an example initiates a desire to start a discussion of one of the four questions. That's what we want to have in the classroom, too. In case of sort.6 my situation was that I have a lot of nice slides with algorithms in pseudo code and I wanted to get them running for demonstration. For APL is a little bit out of fashion I used my favorite computer language: R. Thank you for your comments -- which increase the set of R programming pearls and will enrich the discussion in my lectures Peter Wolf Richard A. O'Keefe wrote: I asked where index origin 0 would help. (I am familiar with Dijkstra's article on why counting should start at 0 and agree whole-heartedly. I am also convinced that kicking against the goads is not helpful.) Peter Wolf [EMAIL PROTECTED] has provided two examples. (1) Converting pseudo-code which assumes index origin 0. I've done this myself, although not yet in R. I've also done it the other way, converting index origin 1 code to C. A good method, I've found, is to to start by converting to index-free form. This applies whatever the source and target languages are. (2) A sorting algorithm. sort.6 - function (a) { n - length(a) adapt - function (i) {i+1} a - c(0,a) for (i in 2:n) { j - i-1 a[adapt(0)] - a[adapt(i)] while (a[adapt(j)] a[adapt(0)]) { a[adapt(j+1)] - a[adapt(j)] j - j-1 } a[adapt(j+1)] - a[adapt(0)] } a[-1] } The really interesting thing here is that this basically is an index origin 1 algorithm. The original array and the final result start at 1, not 0, and position 0 is used for a sentinel. Let's convert it to 1-origin. I'll skip the details of how I did it because that's not the point I want to make. sort.VI - function (a) { a - c(0, a) for (i in 3:length(a)) { j - i-1 a[1] - a[i] while (a[j] a[1]) { a[j+1] - a[j] j - j-1 } a[j+1] - a[1] } a[-1] } What do you get if you move up to index-free form? sort.six - function (a) { s - c() for (x in a) { f - s = x s - c(s[f], x, s[!f])# insert x stably into s } s } It's clear that sort.six is shorter, clearer, and easier to get right than sort.VI. But how much do we have to pay for this? How much efficiency do we lose? a - runif(400) system.time(sort.VI(a)) [1] 3.64 0.02 12.56 0.00 0.00 system.time(sort.six(a)) [1] 0.15 0.01 0.16 0.00 0.00 We don't lose any efficiency at all. We gain, considerably. (Not as much as we'd gain by using the built-in sort(), of course.) I expect that this will happen fairly often: rewriting the code to be index-free will *usually* make it shorter and clearer, and will *always* make it easier to adapt to a language with a different index origin. When the target language is R or S, the result is likely to be faster than a direct conversion. - Gabor Grothendieck wrote: .. and Prof Brian Ripley wrote: : since you can shift whole blocks at a time rather than use a while loop. In words the algorithm runs from 2 through length(a) inserting the current element into the subarray to its left, so to follow up Prof Riley's suggestion to replace the while loop with code that copies whole blocks at a time we have the following which does not seem to suffer from lack of 0-origin and is clearer than the double loop approach: sort.6a - function(a) { for(i in 2:length(a)){ # insert a[i] into subvector to left of it left - a[1:(i-1)] sel - left a[i] a[1:i] - c( left[sel], a[i], left[!sel] ) } a } - Prof Brian Ripley wrote: I think that is an excellent illustration of my point: If you are writing code that works with single elements, you are probably a lot better off writing C code to link into R (and C is 0-based ...). but even in R it is not following However, the R thinking
Re: [R] Zero Index Origin?
Bob Cain wrote: I'm very new to R and utterly blown away by not only the language but the unbelievable set of packages and the documentation and the documentation standards and... I was an early APL user and never lost my love for it and in R I find most of the essential things I loved about APL except for one thing. At this early stage of my learning I can't yet determine if there is a way to effect what in APL was zero index origin, the ordinality of indexes starts with 0 instead of 1. Is it possible to effect that in R without a lot of difficulty? I come here today from the world of DSP research and development where Matlab has a near hegemony. I see no reason whatsoever that R couldn't replace it with a _far_ better and _far_ less idiosyncratic framework. I'd be interested in working on a Matlab equivalent DSP package for R (if that isn't being done by someone) and one of the things most criticized about Matlab from the standpoint of the DSP programmer is its insistence on 1 origin indexing. Any feedback greatly appreciated. Thanks, Bob Hallo Bob, in APL we control index origin by QUAD.IO and QUAD.IO \in {0,1}. Suppose within a function index origin is unknown: a) If we want to work with origin 0 we write x[ i + QUAD.IO ] b) ... with origin 1 ... x[ i - !QUAD.IO ] . So set: QUAD.IO - 1 and use a) --- apl-like. Or define an index shift function: io.0-function(ind) ind+1 to be able to type x[io.0(0:5)] I am shure that your first experiments have been to implement APL functions like take, drop, rotate, ... and now you are looking for a more elegant way to manage origin 0 than +QUAD.IO. Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Zero Index Origin?
Richard A. O'Keefe wrote: It would be interesting to see some sample code where origin 0 is supposed to make life easier, ... An application is the implementation of algorithms which use origin 0 and are written in pseudo code. Write down the statements in R syntax, include some print or browser statements and you are able to demonstrate the working of different approaches. Here is an example for sorting -- I know sort(x) is a better solution ... sort.6-function(a){ n-length(a) adapt-function(i){i+1} # local function to perform the index correction a-c(0,a) for(i in 2:n){ j-i-1 a[adapt(0)]-a[adapt(i)] while(a[adapt(j)]a[adapt(0)]){ a[adapt(j+1)]-a[adapt(j)] j-j-1 } a[adapt(j+1)]-a[adapt(0)] } return(a[-1]) } Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] squashing some numbers
are you looking for s.th. like this? # some data m-c(3,1,2,4,4,4,4,0,5,4,4,5,6,1,1,1) m-as.data.frame(matrix(m,4,4)) print(m) # sort m -- if necessary m-m[order(m[,3]),] m-m[order(m[,2]),] # compute sums cum-cumsum(m[,4]) # find rows ind-c(T,0!=diff(m[,2] )| 0!=diff(m[,3])) ind-c(ind[-1],T) # combine result mneu-cbind(m[ind,-4],V4=diff(c(0,cum[ind]))) print(mneu) initial matrix: V1 V2 V3 V4 1 3 4 5 6 2 1 4 4 1 3 2 4 4 1 4 4 0 5 1 result: V1 V2 V3 V4 4 4 0 5 1 3 2 4 4 2 1 3 4 5 6 result without sorting V1 V2 V3 V4 1 3 4 5 6 3 2 4 4 2 4 4 0 5 1 Peter Wolf Fred J. wrote: Hello I have a data frame with many col.s and rows V4 V5 V6 V7 3 4 5 6 1 4 4 1 2 4 4 1 4 0 5 1 since the data has the middle 2 rows with V5 and V6 are equal, I need to produce V4 V5 V6 V7 3 4 5 6 this line removed and the value V7 = 1 is added to the next row V7 valuve making it 2 2 4 4 2 4 0 5 1 thanks a lot __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] expanding some values in logical vector
Try: x-rep(FALSE,20); x[c(4,10,15)]-TRUE x [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE [13] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE x[outer(which(x),-1:1,+)]-T x [1] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE [13] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE x-rep(FALSE,20); x[c(4,10,15)]-TRUE x[outer(which(x),-2:2,+)]-T x [1] FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [13] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE # now we definie expand.true-function(x,span=1){ m-floor(span/2) ind-outer(which(x),(-m):m,+) ind-ind[ind0 ind = length(x)] x[ind]-TRUE x } x-rep(FALSE,20); x[c(4,10,19)]-TRUE expand.true(x,span=5) [1] FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [13] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE Peter Wolf Petr Pikal wrote: Dear all In automatic dropout evaluation function I construct an index (pointer), which will be TRUE at unusual values. Then I need to expand these TRUE values a little bit forward and backward. Example: having span=5, from vector idx-rep(F,10) idx[4]-T idx [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE I need idx [1] FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE I was using embed() for this task (myfun1): idx - rep(F,20) idx[c(4,11,13)] - T idx [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE iii - myfun1(idx) iii [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE It is close to what I want, but it is slow and when idx vector is long enough it goes short of memory. Then I tried to accomplish it with rle() and some fiddling with $values and $lengths (myfun2), which works as long as the true values are completely separated. idx - rep(F,20) idx[c(4,12)] - T idx [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE iii - myfun2(idx) iii [1] FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE But when using previous idx idx - rep(F,20) idx[c(4,11,13)] - T iii - myfun2(idx) Error in rep.default(kody$values, opak) : invalid number of copies in rep Problem is, if TRUE values are closer than span allows. Then some values in opak for FALSE kody$values are negative, what is not allowed. Setting them to zero will expand the length of index vector. Instead number of repetitions of neighbour TRUE values should be decreased accordingly. I greatly appreciated, if somebody could give me a hint if there is some another built in function which can help me or what to do with myfun2 or how to get properly expanded index values by some another way. Sorry for the long post but I was not able to explain my problem and what I have done yet to solve it in shorter. Thank you and best regards. Petr Pikal # Here are functions used # myfun1 - function(idx,span=5) { n - length(idx) s - span%/%2 z - embed(idx,span) sumy - rowSums(z)0 index - c(rep(sumy[1],s),sumy,rep(sumy[n-span+1],s)) } myfun2 - function(idx,span=5) { n - length(idx) kody - rle(idx) test - letters[sum(cumsum(c(kody$values[1],idx[n])))+1] is some of true values at the end of vector idx? opak - kody$values*(span-1)*2-(span-1)+kody$lengths enlarge number of TRUE repetitions according to the span delka - length(opak) some ifs to ensure the end points will have correct number of repetitions opak[c(1,delka)] - opak[c(1,delka)]+span%/%2 if (sum(kody$values)==0) opak - n if (opak[1]0) {opak[2] - opak[2]+opak[1]; opak[1] - 0} if (opak[delka]0) {opak[delka-1] - opak[delka-1]+opak[delka]; opak[delka] - 0} switch(test, a = opak-opak, b = opak[delka]-opak[delka]-(span-1), c = opak[1]-opak[1]-(span-1), d = opak[c(1,delka)]-opak[c(1,delka)]-(span-1), ) here should opak contain correct number of repetitions but does not index-rep(kody$values,opak) } Petr Pikal [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Changingvalues in data frame
Frank Gerrit Zoellner wrote: Hi! I have a question concerning data frames and changing particular values in it. I have set up a data frame containing n rows of observations od dimension m, so in each row there is a vector of size m. Now I want to introduce a cut off to tha data. Therfore I caluclated the mean and variance within each column using apply: me-apply(daten[1:72],2,mean) st-apply(daten[1:72],2,var) Now I want use the mean and the var to form a cut off value, and apply it to each row of the data frame. I tried the following cutoff-function(x){ if(xme+2*st){ x-me+2*st } } cutd-apply(daten[1:72],1,cutoff) but I have to supply somehow the column index to me and st. Any Ideas ? Thanks, Frank what about: x-as.data.frame(matrix(rnorm(100),20,5)) apply(x,2,function(x){limit-mean(x)+1*sqrt(var(x)); ifelse(xlimit,1000*limit,x)}) or apply(x,2,function(x){limit-mean(x)+2*sqrt(var(x)); ifelse(xlimit,limit,x)}) Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] circular filter
filter.matrix.center implements Manhattan or L 1 distance. If you want to define neighbor points by Euklidean distances (L 2) use filter.matrix.center(p=2): filter.matrix.center.p - function(n=9,size=5,p=2){ x-matrix(1,n,n) center-(n+1)/2 (abs(row(x)-center)^p+abs(col(x)-center)^p)^(1/p) size } example: 0+filter.matrix.center.p(9,4.1) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,]000010000 [2,]001111100 [3,]011111110 [4,]011111110 [5,]111111111 [6,]011111110 [7,]011111110 [8,]001111100 [9,]000010000 Peter Wolf -- Peter Wolf wrote: try: filter.matrix.center-function(n=9,size=5){ x-matrix(1,n,n) center-(n+1)/2 (abs(row(x)-center)+abs(col(x)-center)) size } filter.matrix.center() some tests: 0+filter.matrix.center(5,2) [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]00100 [3,]01110 [4,]00100 [5,]00000 0+filter.matrix.center() [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,]000010000 [2,]000111000 [3,]001111100 [4,]011111110 [5,]111111111 [6,]011111110 [7,]001111100 [8,]000111000 [9,]000010000 Peter Wolf Christof Bigler wrote: I try to find a circular filter that I can export to be used in a spatial software. Assuming, we have a matrix, representing 9x9 regularly spaced points with the center point 'filter[5, 5]'. In this example, I want to find a function that weighs all neighbor points within a distance of d=4 units with 1: filter - matrix(0, 9, 9) filter - function() ... filter [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,]000010000 [2,]001111100 [3,]011111110 [4,]011111110 [5,]111111111 [6,]011111110 [7,]011111110 [8,]001111100 [9,]000010000 Finally, I want to use a larger matrix, e.g. with 61x61 points. Is there a simple function around that I could use to this end? Thanks! Christof __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] circular filter
try: filter.matrix.center-function(n=9,size=5){ x-matrix(1,n,n) center-(n+1)/2 (abs(row(x)-center)+abs(col(x)-center)) size } filter.matrix.center() some tests: 0+filter.matrix.center(5,2) [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]00100 [3,]01110 [4,]00100 [5,]00000 0+filter.matrix.center() [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,]000010000 [2,]000111000 [3,]001111100 [4,]011111110 [5,]111111111 [6,]011111110 [7,]001111100 [8,]000111000 [9,]000010000 Peter Wolf Christof Bigler wrote: I try to find a circular filter that I can export to be used in a spatial software. Assuming, we have a matrix, representing 9x9 regularly spaced points with the center point 'filter[5, 5]'. In this example, I want to find a function that weighs all neighbor points within a distance of d=4 units with 1: filter - matrix(0, 9, 9) filter - function() ... filter [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,]000010000 [2,]001111100 [3,]011111110 [4,]011111110 [5,]111111111 [6,]011111110 [7,]011111110 [8,]001111100 [9,]000010000 Finally, I want to use a larger matrix, e.g. with 61x61 points. Is there a simple function around that I could use to this end? Thanks! Christof __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to repeat a procedure
Haiyan Chen wrote: Hello, 1. After I generate a 100x50 matrix by x3-matrix(0,100,50);for (i in 1:100) {x1-rpois(50, mu[i]);x2-x1; x2[runif(50).01]-0; x3[i,]-x2}, 2. I want to calculate means and sample variances of each row and create a new matrix 100x2; Try: mean.var-function(n=10, m=5){ mu-(1:n)^2 x-matrix(rpois(n*m,mu),n,m) stat-t(apply(x,1,function(x)c(mean(x),var(x } to set some elements of x to zero you can add an additional line in mean.var 3. I then want to repeat above procedure 500 times so that eventually I will have 500 100x2 matrices. n-100; m-50 n.comp-500 for(i in 1:n.comp) eval(parse(text=paste(result,i,-mean.var(n,m),sep=))) Peter Would someone mind helping me to code 2 3? Thanks ahead of time. Heyen __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] (no subject) -- integer to binary
[EMAIL PROTECTED] wrote: Hello, Splus contains the function intbin(x,l). This function allows to make a conversion from an integer x to a binary of length l. for example intbin(3,2) returns 11 intbin(3,3) returns 011 Do you know how to do it in R ? Thank you meriema __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html you can use the following function encode - function(number, base) { # simple version of APL-encode / APL-representation T, pw 10/02 # encode converts the numbers number using the radix vector base n.base - length(base); result - matrix(0, length(base), length(number)) for(i in n.base:1){ result[i,] - if(base[i]0) number %% base[i] else number number - ifelse(rep(base[i]0,length(number)), floor(number/base[i]), 0) } return( if(length(number)==1) result[,1] else result ) } paste(encode(c(13), c(2,2, 2, 2, 2)),collapse=) [1] 01101 encode(c(13), c(2,2, 2, 2, 2)) [1] 0 1 1 0 1 See also: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/decodeencode/decodeencode.rev Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R: plotting multiple functions
allan clark wrote: Hi all Another simple question. I would like to plot three graphs one the same plot with different colours. Say red, blue and black. Here are the functions. r1-1+5*cos(2*pi*seq(1:100)/20)+rnorm(100) r2-1+7*sin(2*pi*seq(1:100)/20)+rnorm(100) r3-1+7*sin(2*pi*seq(1:100)/20)+5*cos(2*pi*seq(1:100)/20)+rnorm(100) Regards Allan Try: r1-1+5*cos(2*pi*seq(1:100)/20)+rnorm(100) r2-1+7*sin(2*pi*seq(1:100)/20)+rnorm(100) r3-1+7*sin(2*pi*seq(1:100)/20)+5*cos(2*pi*seq(1:100)/20)+rnorm(100) x - 1:100 y.min-min(r1,r2,r3) y.max-max(r1,r2,r3) plot(x,type=n,main=3 graphs, xlab=x,ylab=,ylim=c(y.min,y.max)) lines(x,r1,col=red) lines(x,r2,col=blue) lines(x,r3,col=black) vp-par()$usr legend(vp[1]+0.85*(vp[2]-vp[1]), vp[3]+0.9*(vp[4]-vp[3]), legend=c(graph1,graph2,graph3), col=c(red,blue,black), lty=rep(1,3), bty=n) Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to generate a report with graphics and tables?
Liaw, Andy wrote: If you don't mind pdf report generated from LaTeX, Sweave would probably work nicely for you. See the two articles on it in R News, which you can find on the R web site. One other possibility is to use the R2HTML package (and maybe the xtable package, too) to write the `report' in HTML. HTH, Andy From: Olaf Bürger Hello R-Users, I have some data sets which change on a daily bases. So far I have imported these sets into R, done all my evaluations resulting in a couple of plots, charts and tables of numbers which I copypasted via clipboard into Powerpoint. The procedure is always the same and I wonder, whether there is no easier way for doing so. Is there some type of report generator available or some HowTo on this. Can anybody give me a hint on where to look for? Regards, Olaf Bürger A third way is to use the R function ff: Step 1: define a raw latex-report with R expressions (e.g. report.tex) Step 2: modify some expressions in the raw report according the local situation (data sets, etc.) Step 3: start R and evaluate ff(report.tex) Step 4: latex report Step 5: repeat 2-4 for the different data sets Here is the link to the rd file: See: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/formfill/ff.html or http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/formfill/ff.rd Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] subset select within a function
juli g. pausas wrote: Dear all, I'd like to subset a df within a function, and use select for choosing the variable. Something like (simplified example): mydf - data.frame(a= 0:9, b= 10:19) ttt - function(vv) { tmpdf - subset(mydf, select= vv) mean(tmpdf$vv) } ttt(mydf$b) But this is not the correct way. Any help? Thanks in advance Juli __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html are you looking for something like: mydf - data.frame(a= 0:9, b= 10:19) ttt - function(vv) { tmpdf - subset(mydf, select= vv) lapply(tmpdf,mean) } ttt(b) $b [1] 14.5 ttt(c(a,b)) $a [1] 4.5 $b [1] 14.5 ?? Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] 3-dimensional looping Q.
You can compute cor by hand without loops but the code is not much faster (dim(slp)[1]==3): *= set.seed(13) n-3 y-1:n slp-array(rnorm(n*73*144),c(3,73,144)) n.1-length(y)-1 print(system.time({ rData - array(0,c(73,144)) # array to store results for (i in 1:73) { for (j in 1:144) { rData[i,j] - cor(slp[,i,j],y) } } })) print(system.time({ n.1-length(y)-1 mean.slp-apply(slp,c(2,3),mean) mean.y-mean(y); sy-var(y)^0.5 sqslp-apply(slp*slp,c(2,3),sum)/n.1-mean.slp^2*n/n.1 sslpy-apply(slp*y,c(2,3),sum)/n.1-mean.slp*mean.y*n/n.1 rslpy-sslpy/(sqslp^0.5*sy) })) print(all(round(100*rslpy)==round(100*rData))) @ output-start [1] 1.49 0.00 1.49 0.00 0.00 [1] 0.92 0.00 0.92 0.00 0.00 [1] TRUE output-end Peter Wolf Uwe Ligges wrote: Maurice McHugh wrote: Hello everyone- I have a 3-d array with the 1st dimension being monthly mean data that I would like to correlate with some time series index, for example, and save the coefficients in an array. The code I am currently running is rData - array(0,c(73,144)) # array to store results for (i in 1:73) { for (j in 1:144) { rData[i,j] - cor(slp[,i,j],y) } } Rather than running this analysis embedded with two outer loops, are = there any more efficient ways of doing this? Many thanks! Don't know whether it's more efficient: You can try to apply() the function rcorr() in package Hmisc to your problem... Uwe Ligges Maurice Maurice McHugh Department of Geography and Anthropology Louisiana State University Baton Rouge, LA [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] assign
Perez Martin, Agustin wrote: DeaR useRs: I would like to assign a values in an object using a loop 'for'. This is a reduce example of my problem, my real problem is a few complicated: for (j in 1:10) { x.j-rnorm(100) } I want to create 10 objects as x.1, x.2, ... , x.9, x.10 with values in it. I used the assign function but nothing happens. Thank you very much __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Use eval or assign: for(j in 1:10) eval(parse(text=paste(x,j,-,j,sep=))) x1 [1] 1 x8 [1] 8 for(j in 1:10) eval(parse(text=paste(x,j,-rnorm(10),sep=))) x1 [1] 0.49045324 0.44842510 -1.18015351 -0.04325187 -0.75345939 -0.99181309 [7] 0.62517301 -0.68466635 0.33383560 -0.62189500 x2 [1] -0.4344948 2.0276137 1.2783173 1.1170551 0.3546490 -0.0748969 [7] -0.8817247 -1.4649175 -1.5461995 -0.6575016 for(j in 1:10) assign(paste(y,j,sep=),rnorm(10)) y1 [1] 1.7255925 0.4993323 1.8846513 0.2058971 0.2284036 -3.1971553 [7] -0.2629365 0.2724788 -0.9911388 -0.6341857 y2 [1] 0.11301503 -1.28497666 0.40670853 0.08479014 1.05818411 -1.10843908 [7] 1.30441911 -0.49050833 -1.21438645 0.03923849 ls() [1] j x1 x10 x2 x3 x4 x5 x6 x7 x8 x9 y1 [13] y10 y2 y3 y4 y5 y6 y7 y8 y9 pw __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] for loop over dataframe without indices
Try: data(iris); df-as.data.frame(t(iris[1:3,])) for(i in df) print(i) [1] 5.13.51.40.2setosa Levels: 0.2 1.4 3.5 5.1 setosa [1] 4.93.01.40.2setosa Levels: 0.2 1.4 3.0 4.9 setosa [1] 4.73.21.30.2setosa Levels: 0.2 1.3 3.2 4.7 setosa ... however, not very nice Peter Wolf Gabor Grothendieck wrote: Based on an off list email conversation, I had I am concerned that my original email was not sufficiently clear. Recall that I wanted to use a for loop to iterate over the rows of a dataframe without using indices. Its easy to do this over the columns (for(v in df) ...) but not for rows. What I wanted to do is might be something like this. Define a function, rows, which takes a dataframe, df, as input and converts it to the structure: list(df[1,], df[2,], ..., df[n,]) where there are n rows: rows - function( df ) { ll - NULL for( i in 1:nrow(df) ) ll - append( ll, list(df[i,]) ) ll } This allows us to iterate over the rows of df without indices like this: data( iris ) df - iris[1:3,] # use 1st 3 rows of iris data set as df for( v in rows(df) ) print(v) Of course, this involves iterating over the rows of df twice -- once within rows() and once in the for loop. Perhaps this is the price one must pay for being able to eliminate index computations from a for loop or is it? Have I answered my own question or is there a better way to use a for loop over the rows of a dataframe without indices? --- Date: Thu, 18 Dec 2003 19:20:04 -0500 From: Gabor Grothendieck [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: for loop over dataframe without indices One can perform a for loop without indices over the columns of a dataframe like this: for( v in df ) ... some statements involving v ... Is there some way to do this for rows other than using indices: for( i in 1:nrow(df) ) ... some statements involving df[i,] ... If the dataframe had only numeric entries I could transpose it and then do it over columns but what about the general case? __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] `bivariate apply'
Vito Muggeo wrote: dear all, Given a matrix A, say, I would like to apply a bivariate function to each combination of its colums. That is if myfun-function(x,y)cor(x,y) #computes simple correlation of two vectors x and y then the results should be something similar to cor(A). I tried with mapply, outer,...but without success Can anybody help me? many thanks in advance, vito __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help what about myfun-function(x,fun=cor){ cl-matrix(1:ncol(x),ncol(x),ncol(x)) cl-cbind(as.vector(cl),as.vector(t(cl))) res-apply(cl,1,function(xx)fun(x[,xx[1]],x[,xx[2]])) matrix(res,ncol(x),ncol(x)) } Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Flury faces.
Eryk Wolski wrote: How to plot if possible flury faces in R? /Eryk __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help Some days ago I was looking for a function to represent multivariate data by faces, too. For no faces function could be found I constructed my own one. Here is the result: Rd-file: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/faces/faces.Rd Definition: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/faces/faces.sch Documentation: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/faces/faces.pdf Some random faces: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/faces/p20618291De3NA.jpg Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] convert data
y - c( OLDa, ALL, OLDc, OLDa, OLDb, NEW, OLDb, OLDa, ALL,...) el - c(OLDa, OLDb, OLDc, NEW, ALL) match(y,el) [1] 1 5 3 1 2 4 2 1 5 NA Peter Wolf --- Peter Dalgaard wrote: Muhammad Subianto [EMAIL PROTECTED] writes: Dear R-helper, I have a data set like: OLDa ALL OLDc OLDa OLDb NEW OLDb OLDa ALL . . . ALL OLDc NEW I want to convert that data as OLDa=1, OLDb=2, OLDc=3, NEW=4 and ALL=5 or the result like: 1 5 3 1 2 4 2 1 5 . . . 5 3 4 How can I do it. Thanks you for your help. I'd do it like this: x - scan(what=) 1: OLDa 2: ALL 3: OLDc 4: OLDa 5: OLDb 6: NEW 7: OLDb 8: OLDa 9: ALL 10: Read 9 items f - factor(x,levels=c(OLDa, OLDb, OLDc, NEW, ALL) ) as.integer(f) [1] 1 5 3 1 2 4 2 1 5 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] GUI's for R
[EMAIL PROTECTED] wrote: Hello, ... All I said so far has little to do with the title of this email. I am getting to it. While I am pretty happy with a CLI I was asked to evaluate a few statistical packages to be used by casual users in my group. One of the main requirements is ease of use, hence the GUI, furthermore, it will be a lot easier for me to support them in their analyses if we use the same software. One idea to archive easy use is to construct an editor from which you can start R-commands. If you have saved frequently used R-expressions in a text file you can load this file into such an editor, locate the correct formula and eval it. For this task I am developing an editor that uses the tcltk package of R: rwined(). Here is a screen shot: http://www.wiwi.uni-bielefeld.de/~wolf/software/revweb/rwinedwin.jpg For experimentation you can download the code (window and linux) from http://www.wiwi.uni-bielefeld.de/~wolf/software/revweb/rtrevive.exe after installation the package, you have to type: library(rtrevive) rwined() Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] BEGINNER: please help me to write my VERY simple function
Michele Grassi wrote: Hi. 1)I have two variables: call a-c(e.g.0,3,6,7...) b-c(e.g.6,8,3,4...) I want to create a third vector z wich contain the pairs values z-c(0,6,3,8,6,3,7,4and so on for each pairs (a,b)). There is a specific function? How can i write my own function? 2)When i try to write a function and then i save it like function.R file, i try to retrieve it with source comand. As result i obtain an error message error in parse: sintax error on line I apply deparse() and i see an incorrect parsing: how avoid unwanted parsing? Thanks. Michele. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help 1) a simple solution to the problem: define (for example): gen.pairs - function(x,y){ z-as.vector(rbind(x,y)) z } try: x-1:10 y-11:20 gen.pairs(x,y) [1] 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20 2) a) you can write the definition to a file and then use source this file. If the file name is myfun.R source(myfun.R) will work. But only if there are no errors in the definition b) you can type in the code of the definition after the R prompt c) you can type in : gen.pairs - function(x,y) { z } and complete the definition by using edit: edit(gen.pairs) However, syntax errors are not allowed! Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] report generator a la epiinfo
Lucas Gonzalez Santa Cruz wrote: Hi I'd like to use R in epidemiology and disease surveillance. In EpiInfo you can have a script (.pgm) which calls a predefined report (.rpt), where a table is calculated and values picked from that table and placed where the author of the report wants them, with text around those values. (Please see example below.) I've looked at manuals, faq, mail-search and google. The closest is an R Report Generator email that looked as if it wasn't followed after a couple of years. ##The script might have something like this: read.epiinfo(oswego.rec) report(oswego.rpt, output=oswego.txt) ##The predefined report might have this: #{ill} Exactly {YES} people fell ill, and {NO} people didn't. We don't know about the remaining [({}-{YES}-{NO})*100/{}] percent. #{icecream ill} We are specifically interested in the number of people who chose vanilla and didn't fall ill (all {VANILLA, YES} of them). Is there anyway to do this with R? Any direction I should look into? Thanks in advance. Lucas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help One way is to use Sweave, another one is given by my R function ff. ff allows you to substitute expressions by evaluated results in a raw report. See: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/formfill/ff.html or http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/formfill/ff.rd Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Save object R with tkgetSaveFile
[EMAIL PROTECTED] wrote: HI, my question is about the function tkgetSavefile not save any file, for example the next script run OK but not save the file who i like to save, how i cant to save and object R with tkgetSaveFile, how i use the function save(objet, file=foo.R) with tkgetSaveFile ¿What is the error?. I'm work with R 1.7.1 library(tcltk) x-1 filetypes - list({Texto {.txt}} {Word {.doc}} {Pdf {.pdf}} {Postscript {.ps}} {fuente C{.C}} {Eps {.eps}} {Latex {.tex}} {Todos *}) fileD - tkgetSaveFile (filetypes=filetypes,initialdir=c:\\temp,defaultextension=.txt) save(x,file=foo.R) #how i cant merge tkgetSaveFile with function save for to save the object x? thanks Ruben __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help Try: x-1:3 a-tkgetSaveFile() if(0length(a)) save(x,file=as.character(a)) Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] extracting columns with NA's
antonio rodriguez wrote: Hi All, How do I can delete from a matrix (or array) only those columns which have all their values set to NA? Cheers Antonio Rodriguez --- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help Try: x-matrix(1:16,4,4) x[col(x)=row(x)]-NA x[,! apply(x,2,function(x) all(is.na(x))) ] [,1] [,2] [,3] [1,] NA NA NA [2,]2 NA NA [3,]37 NA [4,]48 12 Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] subscripts in lists
What about ... unlist(lis)[which(unlist(lis)==next)+1] [1] want1 want2 ... to avoid the loop in sapply? Richard A. O'Keefe wrote: Chris Knight [EMAIL PROTECTED] has lis-list(c(a,b,next,want1,c),c(d, next, want2, a)) and wants c(want1,want2) Step 1: inx - sapply(lis, function(x) which(x == next)) + 1 == 4 3 Step 2: sapply(1:length(lis), function(i) lis[[i]][inx[i]]) == want1 want2 Think about this for a bit and restructure it: sapply(1:length(lis), function (i) {v - lis[[i]]; v[which(v==next)+1]}) Wrap it up: after - function(lis, what=next) { sapply(1:length(lis), function (i) { v - lis[[i]] v[which(v == what)+1] }) } Of course, from my point of view, a call to sapply() *is* a loop, just packaged slightly differently. I think this is reasonably clear. Peter Wolf Department of economics University of Bielefeld [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] reverse array indexing
Richard A. O'Keefe wrote: Jerome Asselin [EMAIL PROTECTED] suggests this: arr - array(rnorm(27),c(3,3,3)) dimarr - dim(arr) tmparr - array(1:prod(dimarr),dimarr) sapply(c(3),function(x,tmparr) which(tmparr==x,T),tmparr=tmparr) sapply(c(3,17,13,5),function(x,tmparr) which(tmparr==x,T),tmparr=tmparr) Of course, in R we can simplify the last two lines to sapply(argument goes here, function(x) which(tmparr==x,T)) However, wearing my computer scientist hat, I have to wonder about costs. This is basically the equivalent of the APL decode operator. Let's define index.decode - function (index, array) { dimarr - dim(arr) tmparr - array(1:prod(dimarr), dimarr) sapply(index, function(x) which(tmparr == x, T)) } The result is a matrix with C=length(index) columns and R=length(dim(array)) rows. ... I think you mean the APL encode operator? index-1:8 encode(index-1,c(2,2,2))+1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,]11112222 [2,]11221122 [3,]12121212 download code of encode from: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/decodeencode.rev Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Stem and leaf display?
Some weeks ago I wrote a function to construct a stem-and-leaf-display because we want to have some more parameters to control the result. D.Trenkler remind me of /Velleman/Hoaglin: ABC of EDA, page 15: It is easy to construct a Stem-and-Leaf-Display by hand... It is not nearly as easy to write a general computer program to produce Stem-and-Leaf-Displays./ so the programming was a great challange also. Here is the link to the code: http://www.wiwi.uni-bielefeld.de/~wolf/software/mystem/ms.sch German documentation: http://www.wiwi.uni-bielefeld.de/~wolf/software/mystem/ms.ps http://www.wiwi.uni-bielefeld.de/~wolf/software/mystem/ms.html Syntax of stem.leaf ## #Description:# # stem.leaf produces a stem-and-leaf-display of a data set# ## #Usage: # # stem.leaf(data) # # stem.leaf(data,unit=100,m=5,Min=50,Max=1000,rule.line=Dixon# ## #Arguments: # # data: vector of input data # # unit: unit of leafs in: { ...,100,10,1,.1,.01,... }# # m: 1, 2 or 5 -- 10/m=number of possible leaf digits # # Min: minimum of stem # # Max: maximum of stem # # rule.line: = Dixon= number of lines - 10*log(n,10) # # = Velleman = number of lines - 2*sqrt(n) # # = Sturges = number of lines - 1 + log(n,2) # # style: = UREDA= UREDA like stem ( m = 2, 5 ) # ## #Author: # # Peter Wolf 05/2003 # ## Hope it helps Peter Wolf Richard A. O'Keefe wrote: I would like to do some fairly basic stem-and-leaf displays in R. I am aware (I might even say painfully aware) of stem(base) and have tried it. That's why I'm hoping someone has a usable stem- and-leaf display for R so that I don't have to write my own. r-project.org Search R Site Search stem and leaf display finds nothing. I also tried the mail archive search, but couldn't figure out how to search the bodies of the messages. For the record, there are some less important things that I would like to have (like depths and outlier lines), and it is a pity that the atom argument of stem() is effectively undocumented (it's a tolerance, fine, but what _of_ and how used?), but I'd put up with stem() the way it is if only it didn't do things like displaying 5.67 and 5.78 as 56 | 78 which makes it effectively unusable. What good is a stem and leaf plot where you can't recover the leading digits of the numbers correctly? __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help --- Peter Wolf, Department of Economics and Business Administration, University of Bielefeld [EMAIL PROTECTED], http://www.wiwi.uni-bielefeld.de/~wolf/wolf.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] How can I do a spinning plot in R?
Some days ago I wrote a function for rotating a cloud of points. To control the rotation a simple Tcl/Tk-widget is used. Here you can find the R code: http://www.wiwi.uni-bielefeld.de/~wolf/software/spin3R/spin3R.sch Peter Wolf, [EMAIL PROTECTED] Richard A. O'Keefe wrote: I have found XLispStat's spinning plots illuminating. I'd like to do the same thing in R. A dozen or so probes with help, help.search, apropos haven't turned up anything, and I've even resorted to grepping through the entire R source distribution looking for 'spin.*plot', to no avail. Either the feature is called something else in R (what?), or it's in some other package in CRAN (which?), or it's not yet available (I hope not, because I am very far from being skilled enough in R programming to do it myself). Help? __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re:[R] Combining the components of a character vector
John Miyamoto wrote: Suppose I have a character vector. x - c(Bob, loves, Sally) I want to combine it into a single string: Bob loves Sally . paste(x) yields: paste(x) [1] Bob loves Sally Try: paste(x,collapse= ) and help(paste) --Peter --- Peter Wolf, Statistik/Informatik, Fak.f.Wiwi, Uni Bielefeld [EMAIL PROTECTED], http://www.wiwi.uni-bielefeld.de/~wolf/wolf.html --- [[alternate HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] dataframes (The message that once had a suspicious header)
Ernesto Jardim wrote: Hi I want to combine 2 dataframes (t1 and t2) in order to get a third one (t3). The dataframes are below. In SQL I would do something like: SELECT t1.f1,t1.f2,t1.f3,t2.f4 FROM t1, t2 WHERE t1.f1=t2.f1 AND t1.f2=t2.f2 How can I do it with R ? Thanks EJ t1 f1 f2 f3 1 A C E1 2 A D E2 3 B C E3 4 B D E4 t2 f1 f2 f4 1 A C F1 2 A C F2 3 B C F3 4 B C F4 5 A D F5 6 A D F6 t3 f1 f2 f3 f4 1 A C E1 F1 2 A C E1 F2 3 B C E3 F3 4 B C E3 F4 5 A D E2 F5 6 A D E2 F6 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help Try function merge. For details see: help(merge) t1 [,1] [,2] [,3] [1,] A C E1 [2,] A D E2 [3,] B C E3 [4,] B D E4 t2 [,1] [,2] [,3] [1,] A C F1 [2,] A C F2 [3,] B C F3 [4,] B C F4 [5,] A D F5 [6,] A D F6 merge(t1,t2,1:2) V1 V2 V3.x V3.y 1 A C E1 F1 2 A C E1 F2 3 A D E2 F5 4 A D E2 F6 5 B C E3 F3 6 B C E3 F4 --- Peter Wolf __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] inserting elements in a list
Inserting new values into a vector requires to describe where to insert the new values. The function insert.values allows you to define the new position(s) by fractional indices or by a logical vector. insert.values - function(x,pos.insert,x.insert){ # insert.values inserts x.insert into x at positions defined by pos.insert # Arguments: # x input vector # x.insertvector of new values to be stored # pos.insert defines the positions of the new values in one of two ways: # a) pos.insert is a vector of fractional numbers of the same length # as x.insert: x.insert[i] will be inserted between # x[ floor(pos.insert) ] and x[ ceiling(pos.insert) ] # b) pos.insert is of mode logical with length(x) values FALSE # and length(x.insert) values TRUE. Then TRUE values indicate # the position of the new elements and FALSE the values # of old ones # # Examples: #x-1:5 #insert.values(x, 2.5, 1000) # [1]12 1000345 #insert.values(x, (1:5)+.2, 1001:1005) # [1]1 10012 10023 10034 10045 1005 #insert.values(x, .9, ) # [1] 12345 #insert.values(x, c(T,rep(F,5)), 1000) # [1] 100012345 #insert.values(x, c(rep(F,5),T), 1000) # [1]12345 1000 #insert.values(x, rbind(rep(F,5),T), 1001:1005) # [1]1 10012 10023 10034 10045 1005 # pw 02/2003 if(is.logical(pos.insert)){ pos.insert - pos.insert[pos.insert]/length(pos.insert) + cumsum(!pos.insert)[pos.insert] } x-c(x,x.insert)[order(c(seq(x),pos.insert))] return(x) } Peter Wolf -- Dr. Agustin Lobo wrote: I've searched the doc for insert and could not find the way to do the following, hope someone can help: Let's say we have a vector: a [1] 1 2 3 5 6 3 and we want to insert a 7 after any given 3, i.e., we want vector a to become: [1] 1 2 3 7 5 6 3 7 --- Peter Wolf, Statistik/Informatik, Fak.f.Wiwi, Uni Bielefeld [EMAIL PROTECTED], http://www.wiwi.uni-bielefeld.de/~wolf/wolf.html --- [[alternate HTML version deleted]] __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help