[R] [Fwd: Re: How to apply functions over rows of multiple matrices]
[Apologies to Gabor, who I sent a personal copy of the reply erroneously instead of posting to List directly] [...] Perhaps what you really intend is to take the average over those elements in each row of the first matrix which correspond to 1's in the second in the corresponding row of the second. In that case its just: rowSums(newtest * goldstandard) / rowSums(goldstandard) Thank you for clearing my thoughts about the particular example. My question was a bit more general though, as I have different functions which are applied row-wise to multiple matrices. An example that sets all values of a row of matrix A to NA after the first occurrence of TRUE in matrix B. fillfrom - function(applvec, testvec=NULL) { if (is.null(testvec)) testvec - applvec if (length(testvec) != length(applvec)) { stop(applvec and testvec have to be of same length!) } else if(any(testvec, na.rm=TRUE)) { applvec[min(which(testvec)) : length(applvec)] - NA } applvec } fillafter - function(applvec, testvec=NULL) { if (is.null(testvec)) testvec - applvec fillfrom(applvec, c(FALSE, testvec[-length(testvec)])) } numtest - 6 numsubj - 20 newtest - array(rbinom(numtest*numsubj, 1, .5), dim=c(numsubj, numtest)) goldstandard - array(rbinom(numtest*numsubj, 1, .5), dim=c(numsubj, numtest)) newtest.NA - t(sapply(1:nrow(newtest), function(i) { fillafter(newtest[i,], goldstandard[i,]==1)})) My general question is if R provides some syntactic sugar for the awkward sapply(1:nrow(A)) expression. Maybe in this case there is also a way to bypass the apply mechanism and my way of thinking about the problem has to be adapted. But as the *apply calls are galore in R, I feel this is a standard way of dealing with vectors and matrices. -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [Fwd: Re: How to apply functions over rows of multiple matrices]
1. matrices are stored columnwise so R is better at column-wise operations than row-wise. I am seeing this by my code which contains more t() than what seems healthy. However, the summaries are patient-wise over repeated measurements. Out of convention, I am storing patients in rows and measurements in columns. 2. Here is one way to do it (although I am not sure its better than the index approach): row.apply - function(f, a, b) t(mapply(f, as.data.frame(t(a)), as.data.frame(t(b Ah, thank you so much. I'll take the generalization to N arguments à la mapply() as an exercise for the reader. 3. The code for the example in this post could be simplified to: first.1 - apply(cbind(goldstandard, 1), 1, which.max) ifelse(col(newtest) first.1, NA, newtest) Ouch! Consider this scholar slapped. 4. given that both examples did not inherently need row by row operations I wonder if that is the wrong generalization in the first place? Given that you managed to squeeze my 20 lines of code into 2 lines AND that row.apply() does not exist in base without many people missing it, I'll have to concede this point and eliminate the craving for row.apply() in favour of the whole-object approach. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to apply functions over rows of multiple matrices
Dear ExpRts, I would like to perform a function with two arguments over the rows of two matrices. There are a couple of *applys (including mApply in Hmisc) but I haven't found out how to do it straightforward. Applying to row indices works, but looks like a poor hack to me: sens - function(test, gold) { if (any(gold==1)) { sum(test[which(gold==1)]/sum(which(gold==1))) } else NA } numtest - 6 numsubj - 20 newtest - array(rbinom(numtest*numsubj, 1, .5), dim=c(numsubj, numtest)) goldstandard - array(rbinom(numtest*numsubj, 1, .5), dim=c(numsubj, numtest)) t(sapply(1:nrow(newtest), function(i) { sens(newtest[i,], goldstandard[i,])})) Is there any shortcut to sapply over the indices? Best wishes Johannes __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write to clipboard under Linux
Prof Brian Ripley: Linux does not have a clipboard but an X11 session has primary and secondary selections. ?file says [...] so RTFM applied. Indeed it did here. Many thanks for the externsive answer despite the lucid help entry already being there! __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] write to clipboard under Linux
Dear all, I am trying to use the clipboard when writing a table. Typing: write.table(object, file=clipboard) leads to the message: Fehler in file(file, ifelse(append, a, w)) : 'mode' für die Zwischenablage muss unter Unix 'r' sein My interpretation is that I am not allowed to write into the clipboard from a program called from R. Is there a way to change this behaviour? -- Johannes HüsingThere is something fascinating about science. [EMAIL PROTECTED] One gets such wholesale returns of conjecture from such a trifling investment of fact. (Mark Twain, Life on the Mississippi) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] latex() in Hmisc and n.group
This is a bug that has now been fixed. Until a new release of Hmisc appears see the following to get a corrected version of latex( ): http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/LatestRFunctions - you will need getLatestSource('latex') It worked. Many thanks, and apologies for posting without a subject! __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] (kein Betreff)
Frank Harrell rote in a message dating from Oct 8th: n.group is an argument to latex.default in the Hmisc package I must admit that I can't find it in the function head, which reads on my installation: function (object, title = first.word(deparse(substitute(object))), file = paste(title, .tex, sep = ), append = FALSE, label = title, rowlabel = title, rowlabel.just = l, cgroup = NULL, n.cgroup = NULL, rgroup = NULL, n.rgroup = NULL, cgroupTexCmd = bfseries, rgroupTexCmd = bfseries, rownamesTexCmd = NULL, colnamesTexCmd = NULL, cellTexCmds = NULL, rowname, cgroup.just = rep(c, length(n.cgroup)), colheads = dimnames(cx)[[2]], extracolheads = NULL, extracolsize = scriptsize, dcolumn = FALSE, numeric.dollar = !dcolumn, cdot = FALSE, longtable = FALSE, draft.longtable = TRUE, ctable = FALSE, booktabs = FALSE, table.env = TRUE, here = FALSE, lines.page = 40, caption = NULL, caption.lot = NULL, caption.loc = c(top, bottom), double.slash = FALSE, vbar = FALSE, collabel.just = rep(c, nc), na.blank = TRUE, insert.bottom = NULL, first.hline.double = !(booktabs | ctable), where = !tbp, size = NULL, center = c(center, centering, none), landscape = FALSE, multicol = TRUE, ...) I am using Hmisc 3.1.1 here. The first occurrence (and presumably the point where the execution stops) is the line which reads: if (length(n.group) j) which occurs around the 200th line of the function. Am I missing something here? __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] acos(0.5) == pi/3 FALSE
Peter Dalgaard: Ben Bolker [EMAIL PROTECTED] writes: 1. compose your response I've always wondered why step 1. - often the time-consuming bit - is not listed last. The advice applies to the situation when answering immediately would be your knee-jerk reaction. It is assumed that actually composing and sending the mail would take very little time and thought, whereas coming around to answering it after runif(1)*4 hours would take considerably more time, even when mulitiplied with the probability that you are still the first one. Looking at the submission times of questions and answers in this particular case, though, I would be upset if the helpful guys actually used this algorithm. Most of the answers were submitted after 3.5 to 4 h time, thus revealing a possible flaw of the random number generator underlying runif(). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Dividing objects in classes using function sample()
I want to divide n objects in k classes and need an output with all (n+1)(n+2)/2 possibilities. That's the set of compositions. You may use the partitions package and proceed from there (provided the brute-force method suggested by Gabor is not viable'). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Dividing objects in classes using function sample()
I want to divide n objects in k classes and need an output with all (n+1)(n+2)/2 possibilities. That's the set of compositions. You may use the partitions package and proceed from there (provided the brute-force method suggested by Gabor is not viable'). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] split a y-axis to show data on different scales
The pro's and con's of using scale breaks were discussed by Cleveland (1985) The Elements of Graphing Data (Wadsworth, pp. 85-91, 149). I don't know what Cleveland said about this is the second edition Spencer Graves: but I believe there are times when scale breaks are appropriate, but the display should make this nonstandard transition very clear; ... in which case you are close to having two graphs sharing an x-axis and therefore saving on ink (yay!). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] S curve via R
Hello sir: How can I get S curve function via R? For SPSS,the function is:y=exp(b0+b1/x) I am not sure if this is the answer you want, but Scurve - function(x, b0=0, b1=1) { exp(b0+b1/x) } should do what you request. Greetings Johannes __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] change the name of file
Of course file name Data_i.txt will be the same for changing i, unfortunately. ?paste, e.g. paste(Data_, i, .txt, sep=) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] dotchart with log scale?
Dear all, I would like to draw a dot chart on a log scale. What is the syntax for this? A barchart may use log=x, but trying this with dotchart() leads to an error message. ok, I extended dotchart() with a log option. The main reason I am sticking to dotchart() is adjusting of axes with respect to long text labels. This is the definition I am using now: mydotchart - function (x, labels = NULL, groups = NULL, gdata = NULL, cex = par(cex), pch = 21, gpch = 21, bg = par(bg), color = par(fg), gcolor = par(fg), lcolor = gray, xlim = range(x[is.finite(x)]), main = NULL, ### new option xlab = NULL, ylab = NULL, log=FALSE, ...) { opar - par(mai, cex, yaxs) on.exit(par(opar)) par(cex = cex, yaxs = i) n - length(x) if (is.matrix(x)) { if (is.null(labels)) labels - rownames(x) if (is.null(labels)) labels - as.character(1:nrow(x)) labels - rep(labels, length.out = n) if (is.null(groups)) groups - col(x, as.factor = TRUE) glabels - levels(groups) } else { if (is.null(labels)) labels - names(x) glabels - if (!is.null(groups)) levels(groups) } plot.new() linch - if (!is.null(labels)) max(strwidth(labels, inch), na.rm = TRUE) else 0 if (is.null(glabels)) { ginch - 0 goffset - 0 } else { ginch - max(strwidth(glabels, inch), na.rm = TRUE) goffset - 0.4 } if (!(is.null(labels) is.null(glabels))) { nmai - par(mai) nmai[2] - nmai[4] + max(linch + goffset, ginch) + 0.1 par(mai = nmai) } if (is.null(groups)) { o - 1:n y - o ylim - c(0, n + 1) } else { o - sort.list(as.numeric(groups), decreasing = TRUE) x - x[o] groups - groups[o] color - rep(color, length.out = length(groups))[o] lcolor - rep(lcolor, length.out = length(groups))[o] offset - cumsum(c(0, diff(as.numeric(groups)) != 0)) y - 1:n + 2 * offset ylim - range(0, y + 2) } ### instead of log= plot.window(xlim = xlim, ylim = ylim, log = ifelse(log, x, )) lheight - par(csi) if (!is.null(labels)) { linch - max(strwidth(labels, inch), na.rm = TRUE) loffset - (linch + 0.1)/lheight labs - labels[o] mtext(labs, side = 2, line = loffset, at = y, adj = 0, col = color, las = 2, cex = cex, ...) } abline(h = y, lty = dotted, col = lcolor) points(x, y, pch = pch, col = color, bg = bg) if (!is.null(groups)) { gpos - rev(cumsum(rev(tapply(groups, groups, length)) + 2) - 1) ginch - max(strwidth(glabels, inch), na.rm = TRUE) goffset - (max(linch + 0.2, ginch, na.rm = TRUE) + 0.1)/lheight mtext(glabels, side = 2, line = goffset, at = gpos, adj = 0, col = gcolor, las = 2, cex = cex, ...) if (!is.null(gdata)) { abline(h = gpos, lty = dotted) points(gdata, gpos, pch = gpch, col = gcolor, bg = bg, ...) } } axis(1) box() title(main = main, xlab = xlab, ylab = ylab, ...) invisible() } Many thanks for your attention. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] dotchart with log scale?
Dear all, I would like to draw a dot chart on a log scale. What is the syntax for this? A barchart may use log=x, but trying this with dotchart() leads to an error message. Greetings Johannes __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Manipulating code?
Dear expeRts, I am currently struggling with the problem of finding cut points for a set of stimulus variables. I would like to obtain cut points iteratively for each variable by re-applying a dichotomised variable in the model and then recalculate it. I planned to have fixed names for the dichotomised variables so I could use the same syntax for every recalculation of the whole model. I furthermore want to reiterate the process until no cut point changes any more. My problem is in accomplishing this syntactically. How can I pass a variable name to a function without getting lost in as.symbol and eval and parse mayhem? I am feeling I am thinking too much in macro expansion à la SAS when trying to tackle this. __ 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] Manipulating code?
Johannes Hüsing [EMAIL PROTECTED] writes: [...] I think a simple example of what you are trying to do might be needed. I don't think so, as ... But take a look at the help pages for assign() and get(). ... this seems to be what I was looking for. Many thanks! Greetings Johannes __ 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] fixed variance of lmer object
Martin Maechler [EMAIL PROTECTED] [Fri, Mar 03, 2006 at 05:38:52PM CET]: Johannes == Johannes Hüsing [EMAIL PROTECTED] on Wed, 1 Mar 2006 21:01:39 +0100 writes: Johannes Dear expRts, Johannes could anybody nudge me toward some documentation about how to Johannes extract the variance-covariance matrix of the fixed parameters Johannes from an lmer object? vcov( . ) thank you! I just found it out myself by looking at https://svn.r-project.org/R-packages/trunk/Matrix/R/lmer.R. as ``for all good objects '' :-) Which brings me back to the core of my question: Where was I supposed to find this piece of information myself. The logical place, in my opinion, would be Section 11.3 of An Introduction to R. Am I missing something here? If desired, I can write a patch for R-intro.texi. Greetings Johannes -- Johannes HüsingThere is something fascinating about science. [EMAIL PROTECTED] One gets such wholesale returns of conjecture from such a trifling investment of fact. (Mark Twain, Life on the Mississippi) __ 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] fixed variance of lmer object
Dear expRts, could anybody nudge me toward some documentation about how to extract the variance-covariance matrix of the fixed parameters from an lmer object? Best Johannes -- Johannes HüsingThere is something fascinating about science. [EMAIL PROTECTED] One gets such wholesale returns of conjecture from such a trifling investment of fact. (Mark Twain, Life on the Mississippi) __ 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] a series of 1's and -1's
You could try to zip your data file and see whether there is a change in file size that you feel is significant in which case the series is not random (-: ... after converting the -1s and 1s to bits, of course. __ 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] graphics: axis label
Hello, par(las=1) sets the orientation of the axis labels to horizontal. That is, the tick mark labels. How do I set the orientation of the axis label, which annotates the variable plotted along the axis, to horizontal? Sorry for asking such a basic question here, but I haven't found anything in the description of the pars. Greetings Johannes __ 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] reading contigency tables
Just a remark: I need some help using read.ftable to read a contingency table. My columns are organized as follows: order--family--species--location--number of individuals As the first three variables are nested, I would expect this table to contain a lot of structural zeroes. I understand you just get the data in table form and do not intend to work on them as a contingency table. I couldn't figure out how to change the data on my text file to be imported into R; and after you do that, is it possible to convert the table into a data frame? Any tips would be greatly appreciatted! data(HairEyeColor) condensed - as.data.frame(HairEyeColor) If this is not sufficient, then extended - condensed[-ncol(condensed), rep(1:nrow(condensed), condensed[,ncol(condensed)])] should do the trick. Greetings Johannes __ 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] normality test
Bert wrote: You probably have noticed that I'm quite new to statistics, but I'm working on that... And you want to use Bayesian methods?! I was always under the impression that it's mostly a matter of mindset if you go Bayesian or frequentist, not of your statistical skills. [...] The business of the statistician is to catalyze the scientific learning process. - George E. P. Box And I find this quote a bit disturbing because a catalyst leaves the process unchanged, yet as a statistician I might at least sometimes have learnt a bit of the subject matter problem. And no, I have nothing substantial to contribute any more tonight. Greetings Johannes __ 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] error messages on R CMD check
Hallo Uwe, Uwe Ligges schrieb: [...] In Johannes' case, the problem is different, because the error message is not that clear. Johannes, can you install and load the package? Is the DESCRIPTION file correct? If so, you might want to send the package in a private message... I have found the root of the error: I renamed some functions without renaming them in the export statement of the NAMESPACE file. Silly me. Thanks for offering help, and good thing I didn't send you the stuff. It would be good to add your suggestions to the R-exts document: If the error messages after R CMD check do not sound helpful, try building and loading the package and see what the system has to say about this. Greetings Johannes __ 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] error messages on R CMD check
Dear all, I am trying to wrap up a package. On entering R CMD check, I get the following error messages: [...] * checking S3 generic/method consistency ... WARNING Error in .try_quietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed for 'resper' Execution halted See section 'Generic functions and methods' of the 'Writing R Extensions' manual. * checking replacement functions ... WARNING Error in .try_quietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed for 'resper' Execution halted In R, the argument of a replacement function which corresponds to the right hand side must be named 'value'. * checking foreign function calls ... WARNING Error in .try_quietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : package/namespace load failed for 'resper' Execution halted See section 'System and foreign language interfaces' of the 'Writing R Extensions' manual. * checking Rd files ... OK * checking for missing documentation entries ... ERROR Error in .try_quietly({ : Error in library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE) : I am not getting these messages, nor their relation to section 'Generic functions and methods' of the 'Writing R Extensions' manual, as I did not write any generic methods in my package. There has been some discussion of the error message around Christmas 2003: http://maths.newcastle.edu.au/~rking/R/devel/03b/1438.html, but I can't see how the circumstances described there apply to my situation (export a class name). Could somebody give me a clue on how to give my search a direction? Greetings Johannes __ 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 equivalent to funcall?
On Thu, 17 Mar 2005 13:20:32 +0100 (CET), Johannes Hüsing [EMAIL PROTECTED] wrote : [...] Is there a way that the dot-dot-dot argument of a function accepts a list as single arguments, such as funcall in several Lisp dialects? do.call() comes close to what you want. [...] Indeed it does. Thank you very much. I have overlooked that function. Greetings Johannes __ 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