Re: [R] lines at margin?
On Mon, 2006-10-09 at 10:56 -0400, Mike Wolfgang wrote: Hi list, I want to add some lines at margin area of one figure. mtext could add text to these margins, can I add lines with different lty parameters? Thanks, mike You can do it, but it will take some fiddling to get the coordinates right: # Do a generic plot plot(1:10) # Get the current plot region axis ranges # x1, x2, y1, y2 par(usr) [1] 0.64 10.36 0.64 10.36 # Draw a vertical line in the right hand margin # Set 'xpd = TRUE' so that plotting is not # clipped at the plot region boundary segments(10.75, 4, 10.75, 6, xpd = TRUE) See ?par for more information. HTH, Marc Schwartz __ 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] How can I delete components in a column ?
On Mon, 2006-10-09 at 17:15 +0200, Yen Ngo wrote: Hi all R-helpers, i am a new R-user and have problem with deleting some components in a column. I have a dataset like Name Idx empty 2 empty 3 anone2 bnone3 d none 2 ad cfh 4 bf cdt 5 empty 2 empty 2 gf cdh 4 d none 5 and want to eliminate all components that have id=none and empty . The remaining data should be Name Id x ad cfh 4 bfcdt 5 gfcdh 4 How can I do this ? The components with id=empty have no name. Thanks in advance, Regards, Yen The easiest way is the use the subset() function. Presuming that your data frame is called 'DF': NewDF - subset(DF, !Id %in% c(empty, none)) The second argument, using a logical negation of the %in% function, tells subset to only select those rows where the Id column does not contain either empty or none. See ?subset and ?%in% HTH, Marc Schwartz __ 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] Transcript of Conservative ANOVA tables
On Mon, 2006-10-09 at 15:43 +, Gregor Gorjanc wrote: Dieter Menne dieter.menne at menne-biomed.de writes: Dear friends of lmer, http://wiki.r-project.org/rwiki/doku.php?id=guides:lmer-tests I have put a transcript of the long thread on lmer/lme4 statistical test into the Wiki. For all those who missed it life, and for those like me, who don't like the special style of the R-list to keep full length quotes. Creating the text there was not much fun, waiting times are simply unacceptable and the Wiki only give an empty page when syntax errors (for example from quotes) are detected. I agree that there is a problem about waiting times with large pages. This might be of interest for r-sig-wiki list, but I can not CC from Gmane - I will send separate mail. I think there was discussion about this and that wiki is optimized for many small pages. I do agree though that having several pages for this transcript is not acceptable. Gregor If the content of this particular transcript is likely to be static, consider an alternative of making it available in a PDF document that is linked on that page. Then others can perhaps contribute by providing other relevant content in that section as may be desired/required. HTH, Marc Schwartz __ 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] lmer output
On Fri, 2006-10-06 at 17:05 +0100, Mike Ford wrote: When I do lmer models I only get Estimate, Standard Error and t value in the output for the fixed effects. Is there a way I get degrees of freedom and p values as well? I'm a very new to R, so sorry if this a stupid question. Thank you - Mike See R FAQ 7.35 Why are p-values not displayed when using lmer()? http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-p_002dvalues-not-displayed-when-using-lmer_0028_0029_003f HTH, Marc Schwartz __ 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] Stopping ctrl-\ from qutting R
On Fri, 2006-10-06 at 14:46 -0400, Martin C. Martin wrote: Hi, In the Linux (FC3) version of R, ctrl-\ quits R. This wouldn't be so bad, but on my keyboard, it's right next to ctrl-p and I tend to hit it by accident. Is there any way to turn that off? Open your favorite terminal emulator (ie. gnome-terminal, xterm, konsole) and type: stty quit undef then type: R The first command will disable the QUIT signal within the tty session, which by default is set to CTRL-\. This will not change other console sessions. Of course this behavior may introduce other problems. :-) BTW, you might want to consider updating your FC distro, as FC3 is now EOL and only supported by the Fedora Legacy folks. That support will end on December 31. At this point of course, you might just want to wait until FC 6 is out sometime in the next week or so. HTH, Marc Schwartz __ 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] is it possible to fill with a color or transparency gradient?
On Fri, 2006-10-06 at 16:15 -0400, Eric Harley wrote: Hi all, Is there a way to fill a rectangle or polygon with a color and/or transparency gradient? This would be extremely useful for me in terms of adding some additional information to some plots I'm making, especially if I could define the gradient on my own by putting functions into rgb something like rgb( r=f(x,y), g=f(x,y), b=f(x,y), alpha=f(x,y) ). Not so important whether the coordinates are in terms of the plot axes or normalized to the polygon itself somehow. Ideally it would work not only for a fill color but also for shading lines. I haven't been using R very long, so it's possible that I'm just missing something, but I haven't found anything like this in the help files. I've tried to poke around in graphics, grid, and ggplot, without any luck so far. I really like some of the functionality in ggplot, and it does some nice things with continuous gradients for the color of scatter plot points, for example, but it each individual point (or grob) is always one solid color as far as I can tell. Thanks, Eric Take a look at the gradient.rect() function in Jim Lemon's 'plotrix' CRAN package. HTH, Marc Schwartz __ 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] how ot replace the diagonal of a matrix
On Tue, 2006-10-03 at 17:03 -0400, Duncan Murdoch wrote: On 10/3/2006 4:59 PM, roger bos wrote: Dear useRs, Trying to replace the diagonal of a matrix is not working for me. I want a matrix with .6 on the diag and .4 elsewhere. The following code looks like it should work--when I lookk at mps and idx they look how I want them too--but it only replaces the first element, not each element on the diagonal. mps - matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) idx - diag(3) mps idx mps[idx] - rep(.6,3) I also tried something along the lines of diag(mps=.6, ...) but it didn't know what mps was. Matrix indexing can use a two column matrix, giving row and column numbers. So you could get what you want by mps[cbind(1:n,1:n)] - 0.6 What's wrong with: mps - matrix(rep(.4, 3*3), nrow = 3, byrow=TRUE) mps [,1] [,2] [,3] [1,] 0.4 0.4 0.4 [2,] 0.4 0.4 0.4 [3,] 0.4 0.4 0.4 diag(mps) - 0.6 mps [,1] [,2] [,3] [1,] 0.6 0.4 0.4 [2,] 0.4 0.6 0.4 [3,] 0.4 0.4 0.6 HTH, Marc Schwartz __ 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] Barplot
Mohsen, I had not seen a reply to your follow up yet and I have been consumed in meetings and on phone calls. On your first question, add two additional lines of code: BL - c(36.35, 36.91, 25.70, 34.38, 5.32) LR - c(1.00, 4.00, 6.00, 3.00, 0.50) Q - c(1.92, 0.00, 0.00, 1.92, 0.00) # Get the bar midpoints in 'mp' mp - barplot(LR, main='LR Value', col='orange', border='black', space=0.05, width=(BL), xlab='Length', ylab='LR') # Write the LR and Q values below the bar midpoints mtext(1, at = mp, text = sprintf(%.1f, LR), line = 1) mtext(1, at = mp, text = sprintf(%.1f, Q), line = 0) # Write labels at minimum value of X axis mtext(1, at = par(usr)[1], text = LR, line = 1) mtext(1, at = par(usr)[1], text = Q, line = 0) See ?par for more information. With respect to adding some sort of curve fit/density plot to your data, it is not clear to me what the data represents, as the x axis does not appear to be monotonic in Q (other than the bar midpoints) and the y axis values do not appear to be counts. If you have the original vector of data, you may be better off with a histogram rather than a barplot, since the histogram will enable a common density area within the bars (ie. the area of the bars = 1.0) over which you can then draw a normal density curve. This general approach was recently covered here: https://stat.ethz.ch/pipermail/r-help/2006-September/113686.html and there are similar examples in the archives. See ?hist and ?truehist in the MASS package. HTH, Marc Schwartz On Mon, 2006-10-02 at 16:42 -0400, Mohsen Jafarikia wrote: Thanks for your response. I just have two more questions: 1) I don't know how to write the titles of the LR and Q behind their lines of values (at the bottom of the graph). I tried to write like text = sprintf(LR%.1f, LR)... but it writes 'LR' behind all values while I only want it once at the beginning of the line while all the LR and Q values are still in the mid points of bars. 2) I would like a line which connects the mid points of each bar to be like a density function (or regression) line which is not sharp like what I have now. I tried to write density in the code but it tells Error in xy.coords(x, y) : 'x' and 'y' lengths differ I appreciate any comment about these questions Thanks, Mohsen On 10/2/06, Marc Schwartz (via MN) [EMAIL PROTECTED] wrote: On Mon, 2006-10-02 at 11:14 -0400, Mohsen Jafarikia wrote: Hello, I have used the following data to draw my barplot: BL LRQ 36.351.00 1.92 36.914.00 0.00 25.706.00 0.00 34.383.00 1.92 05.320.50 0.00 BL-c(36.35, 36.91, 25.70, 34.38, 05.32) LR-c(1.00, 4.00, 6.00, 3.00, 0.50) Q-(1.92, 0.00, 0.00, 1.92, 0.00) barplot(dt$LR, main='LR Value', col='orange', border='black', space= 0.05, width=(dt$BL), xlab='Length', ylab='LR') axis(1) I would like to do the following things that I don't know how to do it: 1) Writing the value of each 'BL' on my X axis. 2) Writing the value of 'Q' on the bottom of X axis. 3) Draw a line on the bars which connects the 'LR' values. I appreciate your comments. Thanks, Mohsen I'm not sure if I am getting this completely correct, but is this what you want? BL - c(36.35, 36.91, 25.70, 34.38, 5.32) LR - c(1.00, 4.00, 6.00, 3.00, 0.50) Q - c(1.92, 0.00, 0.00, 1.92, 0.00) # Get the bar midpoints in 'mp' mp - barplot(LR, main='LR Value', col='orange', border='black', space=0.05, width=(BL), xlab='Length', ylab='LR') # Write the LR and Q values below the bar midpoints mtext(1, at = mp, text = sprintf(%.1f, LR), line = 1) mtext(1, at = mp, text = sprintf(%.1f, Q), line = 0) # Now connect the LR values across the bars lines(mp, LR) See ?barplot, ?mtext, ?sprintf and ?lines HTH, Marc Schwartz __ 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-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] Barplot
On Mon, 2006-10-02 at 11:14 -0400, Mohsen Jafarikia wrote: Hello, I have used the following data to draw my barplot: BL LRQ 36.351.00 1.92 36.914.00 0.00 25.706.00 0.00 34.383.00 1.92 05.320.50 0.00 BL-c(36.35, 36.91, 25.70, 34.38, 05.32) LR-c(1.00, 4.00, 6.00, 3.00, 0.50) Q-(1.92, 0.00, 0.00, 1.92, 0.00) barplot(dt$LR, main='LR Value', col='orange', border='black', space=0.05, width=(dt$BL), xlab='Length', ylab='LR') axis(1) I would like to do the following things that I don't know how to do it: 1) Writing the value of each 'BL' on my X axis. 2) Writing the value of 'Q' on the bottom of X axis. 3) Draw a line on the bars which connects the 'LR' values. I appreciate your comments. Thanks, Mohsen I'm not sure if I am getting this completely correct, but is this what you want? BL - c(36.35, 36.91, 25.70, 34.38, 5.32) LR - c(1.00, 4.00, 6.00, 3.00, 0.50) Q - c(1.92, 0.00, 0.00, 1.92, 0.00) # Get the bar midpoints in 'mp' mp - barplot(LR, main='LR Value', col='orange', border='black', space=0.05, width=(BL), xlab='Length', ylab='LR') # Write the LR and Q values below the bar midpoints mtext(1, at = mp, text = sprintf(%.1f, LR), line = 1) mtext(1, at = mp, text = sprintf(%.1f, Q), line = 0) # Now connect the LR values across the bars lines(mp, LR) See ?barplot, ?mtext, ?sprintf and ?lines HTH, Marc Schwartz __ 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] X-axis labels in histograms drawn by the truehist function
On Mon, 2006-10-02 at 14:58 -0400, Ravi Varadhan wrote: Hi, I had sent this email last week, but received no reply. So, I am resending it - please excuse me for the redundant email. I have a simple problem that I would appreciate getting some tips. I am using the truehist function within an apply call to plot multiple histograms. I can't figure out how to get truehist to use the column names of the matrix as the labels for the x-axis of the histograms. Here is a simple example: library(MASS) # this contains the truehist function X - matrix(runif(4000),ncol=4) colnames(X) - c(X1,X2,X3,X4) par(mfrow=c(2,2)) apply(X, 2, function(x)truehist(x)) In this example, I would like the x-labels of the histograms to be X1, X2, etc. Any help is appreciated. Best, Ravi Ravi, Gabor did reply: https://stat.ethz.ch/pipermail/r-help/2006-September/114019.html HTH, Marc Schwartz __ 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] index vector
On Fri, 2006-09-29 at 16:13 -0400, bertrand toupin wrote: Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe See ?is.na and note the use of: is.na(x) - value Example: mat - matrix(sample(50), 10, 5) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 19 [6,]7 36 31 49 37 [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 10 # Set some values in column 5 to -9 mat[sample(10, 3), 5] - -9 mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 30 5 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 11 6 [5,] 13 15 42 33 -9 [6,]7 36 31 49 -9 [7,] 29 419 27 4 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 -9 # Use which to get the indices within column 5 # of those values which are -9 # See ?which which(mat[, 5] == -9) [1] 5 6 10 # Now extend that and set those to NA is.na(mat[, 5]) - which(mat[, 5] == -9) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 NA [6,]7 36 31 49 NA [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 NA Note one other possibility, which is that if you used one of the read.table() family functions to read in a delimited ASCII file containing the data set, you can set the 'na.strings' argument to -9 and have it set these to NA upon importing. See ?read.table for more information. HTH, Marc Schwartz __ 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] recode problem - unexplained values
On Thu, 2006-09-28 at 12:27 +1000, [EMAIL PROTECTED] wrote: I am hoping for some advice regarding the difficulties I have been having recoding variables which are contained in a csv file. Table 1 (below) shows there are two types of blanks - as reported in the first two columns. I am using windows XP the latets version of R. When blanks cells are replaced with a value of n using syntax: affect [affect==] - n there are still 3 blank values (Table 2). When as.numeric is applied, this also causes problems because values of 2,3 4 are generated rather than just 1 2. TABLE 1 table(group,actions) actions group n y 1 100 2 0 3 2 30 1 1 0 3 24 0 0 0 TABLE 2 table(group,actions) actions group n y 1 0 2 100 3 2 0 1 31 0 3 0 0 24 0 Below is another example - for some reason there are 2 types of 'aobh' values. table(group, type) type group aobh aobh gbh m uw 1 104 1 0 0 0 20 0 15 0 17 30 0 0 24 0 Any assistance is much appreciated, Bob Green Bob, A quick heads up, which is the presumption that aobh and aobh are different values simply as a consequence of leading/trailing spaces in the source data file within the delimited fields. This is also the likely reason for there being multiple missing/blank values in your imported data set. Presuming that you used one of the read.table() family functions (ie. read.csv() ), take note of the 'strip.white' argument in ?read.table, which defaults to FALSE. If you change it to TRUE, the function will strip leading and trailing blanks, likely resolving this issue. HTH, Marc Schwartz __ 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] Evaluation of defaults in functions
On Thu, 2006-09-28 at 21:49 +0200, Ulrich Keller wrote: Hello, and sorry if this is already explained somewhere. I couldn't find anything. R (2.3.1, Windows) seems to perform some kind of lazy evaluation when evaluating defaults in function calls that, at least for me, leads to unexpected results. Consider the following, seemingly equivalent functions: foo1 - function(x, y=x) { + x - 0 + y + } foo1(1) [1] 0 foo2 - function(x, y=x) { + y - y + x - 0 + y + } foo2(1) [1] 1 Obviously, y is not evaluated until it is used in some way. I would expect it to be evaluated where it is defined. Is this intended behavior? Thanks for clarifying, Uli Yep. This is documented in the R Language Definition Manual, which is available via the GUI in the Windows version and/or online here: http://cran.r-project.org/doc/manuals/R-lang.html Specifically in section 4.3.3 Argument Evaluation: R has a form of lazy evaluation of function arguments. Arguments are not evaluated until needed. It is important to realize that in some cases the argument will never be evaluated. Thus, it is bad style to use arguments to functions to cause side-effects. While in C it is common to use the form, foo(x = y) to invoke foo with the value of y and simultaneously to assign the value of y to x this same style should not be used in R. There is no guarantee that the argument will ever be evaluated and hence the assignment may not take place. You might also want to read section 2.1.8 Promise objects and section 6.2 Substitutions. HTH, Marc Schwartz __ 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] help on plots
On Thu, 2006-09-28 at 23:55 +0800, zhijie zhang wrote: Dear friends, I met a problem on plotting. My dataset is : yearMHBC LHBC MHRC LURC 1993 11.75 4.50 0.43 0.46 19947.25 1.25 0.35 0.51 19958.67 2.17 0.54 0.44 1996 2.67 1.33 0.78 0.47 1997 3.42 4.92 0.69 0.48 1998 1.92 3.08 0.72 0.54 1999 2.33 2.58 0.74 0.41 2000 5.75 4.50 0.45 0.50 2001 3.75 4.42 0.52 0.47 2002 2.33 1.83 0.58 0.45 2003 0.25 2.83 0.50 0.39 I want to get a plot -line with scatters, the requirement is : x-axis is year; two y-axis: y1 corresponds to MHBC and LHBC; y2 corresponds to MHRC and LURC; hope to use different symbols to differentiate the MHBC,LHBC,MHRC and LURC. The following is my program, but very bad ,: *plot(a$year,a$MHBC,type='b') #line1 par(new=T) plot(a$year,a$LHBC,type='b') #line2 par(new=T) plot(a$year,a$MHRC,type='b') #line3 par(new=T) plot(a$year,a$LURC,type='b') #line4 axis(4, at=pretty(range(a$MHRC)))* In the figure, the labels and scales of X-axis are vague, the scale of y-axis is not very good. The better figure should be like the line1 and 2 are in the upper, and line3 and 4 are in the bottom. Any suggestion are welcome! It's not entirely clear to me what you want, so let me offer three possibilities. 1. Do all four lines in a single plot with a common y axis: matplot(a$year, a[, -1], type = o, pch = 15:18) 2. Do all four lines in a single plot with the first two having a separate left hand y axis and the second two having a separate right hand y axis: # Draw the first pair of lines matplot(a$year, a[, 2:3], type = o, pch = c(19, 20), lty = solid, ann = FALSE) # Get the current plot region boundaries usr - par(usr) # Get the range of the second set of columns range.y2 - range(a[, 4:5]) # Change the plot region y axis range for the second # set of columns. Extend them by 4% as per the default par(usr = c(usr[1], usr[2], range.y2[1] * 0.96 , range.y2[2] * 1.04)) # Add the second pair of lines matlines(a$year, a[, 4:5], type = o, pch = c(15, 18), lty = dashed, col = c(blue, green)) # Add the second y axis axis(4) 3. Do the first two lines in an upper plot and the second two lines in a lower plot, each has its own y axis range: # Set plot region to have two rows par(mfrow = c(2, 1)) # Adjust the plot margins par(mar = c(2, 5, 2, 2)) # Draw the first pair of lines matplot(a$year, a[, 2:3], type = o, pch = c(19, 20), lty = solid, ylab = First Pair) par(mar = c(3, 5, 2, 2)) # Add the second pair of lines matplot(a$year, a[, 4:5], type = o, pch = c(15, 18), lty = dashed, col = c(blue, green), ylab = Second Pair) See ?matplot, ?par and ?points for more information. HTH, Marc Schwartz __ 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] a decimal aligned column
On Thu, 2006-09-28 at 12:31 -0700, BBands wrote: Hello, For numbers in the range 100 to 100,000,000 I'd like to decimal align a right-justified comma-delineated column of numbers, but I haven't been able to work out the proper format statement. format(num, justify=right, width=15, big.mark=,) gets me close, but numbers larger than 1,000,000 project a digit beyond the right edge of the column, which I really don't understand. I gather I can get the decimal alignment from sprintf(), but I am not sure about the interaction of the two functions. TIA, jab Is this what you want?: Nums - 10 ^ (2:8) Nums.Pretty - format(Nums, width = 20, justify = right, big.mark = ,, nsmall = 4, scientific = FALSE) Nums.Pretty [1] 100. 1,000. [3]10,000. 100,000. [5] 1,000,000.10,000,000. [7] 100,000,000. cat(Nums.Pretty, sep = \n) 100. 1,000. 10,000. 100,000. 1,000,000. 10,000,000. 100,000,000. HTH, Marc Schwartz __ 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] Splitting a character variable into a numeric one and a character one?
On Mon, 2006-09-25 at 11:30 -0500, Marc Schwartz (via MN) wrote: On Mon, 2006-09-25 at 11:04 -0500, Frank Duan wrote: Hi All, I have a data with a variable like this: Column 1 123abc 12cd34 1e23 ... Now I want to do an operation that can split it into two variables: Column 1Column 2 Column 3 123abc 123 abc 12cd34 12cd34 1e23 1 e23 ... So basically, I want to split the original variabe into a numeric one and a character one, while the splitting element is the first character in Column 1. I searched the forum with key words strsplitand substr, but still can't solve this problem. Can anyone give me some hints? Thanks in advance, FD Something like this using gsub() should work I think: DF V1 1 123abc 2 12cd34 3 1e23 # Replace letters and any following chars with DF$V2 - gsub([A-Za-Z]+.*, , DF$V1) Quick typo correction here. It should be: DF$V2 - gsub([A-Za-z]+.*, , DF$V1) The second 'z' should be lower case. Marc __ 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] Splitting a character variable into a numeric one and a character one?
On Mon, 2006-09-25 at 11:04 -0500, Frank Duan wrote: Hi All, I have a data with a variable like this: Column 1 123abc 12cd34 1e23 ... Now I want to do an operation that can split it into two variables: Column 1Column 2 Column 3 123abc 123 abc 12cd34 12cd34 1e23 1 e23 ... So basically, I want to split the original variabe into a numeric one and a character one, while the splitting element is the first character in Column 1. I searched the forum with key words strsplitand substr, but still can't solve this problem. Can anyone give me some hints? Thanks in advance, FD Something like this using gsub() should work I think: DF V1 1 123abc 2 12cd34 3 1e23 # Replace letters and any following chars with DF$V2 - gsub([A-Za-Z]+.*, , DF$V1) # Replace any initial numbers with DF$V3 - gsub(^[0-9]+, , DF$V1) DF V1 V2 V3 1 123abc 123 abc 2 12cd34 12 cd34 3 1e23 1 e23 See ?gsub and ?regex for more information. HTH, Marc Schwartz __ 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] Can't mix high level and low level plot functions.
On Mon, 2006-09-25 at 19:56 +0200, Lothar Botelho-Machado wrote: Hey R-Comunity, I'd like to print out an histogram of some experimental data and add a smooth curve of a normal distribution with an ideally generated population having the same mean and standard deviation like the experimental data. The experimental data is set as vector x and its name is set to group.name. I paint the histogram as follows: hist(data, freq=FALSE, col=lightgrey, ylab=Density, xlab=group.name) First I did the normal distribution curve this way: lines(x, dnorm(x, mean=mean(x), sd=sd(x)), type=l, lwd=2) This curve just uses as many values as there are in x. When using small amounts of sample populations the curve looks really shaky. I tried this one using a high level plot function as well: curve(dnorm, n=1, add=TRUE, xlim=range(x)) The advantage is, now I can set an ideal population of 1 to get the ideal curve really smooth. But the big disadvantage is, I don't know how to add mean=mean(x), sd=sd(x) arguments to it? It says that it can't mix high level with low level plot functions when I try to set some kind of parameter like n=1 to the low level function, it says that there ain't enough x values. So my question is, how to get a smooth curve placed of dnorm over an histogram of sample data, ideally by using the curve method? TIA, Lothar Rubusch This almost seems like it should be a FAQ. I also checked the R Graphics Gallery (http://addictedtor.free.fr/graphiques/index.php) and didn't see an example there either, unless I missed it. In either case: x - rnorm(50) hist(x, freq = FALSE) # Create a sequence of x axis values with small # increments over the range of 'x' to smooth the lines x.hypo - seq(min(x), max(x), length = 1000) # Now use lines() lines(x.hypo, dnorm(x.hypo, mean=mean(x), sd=sd(x)), type=l, lwd=2) HTH, Marc Schwartz __ 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] paste? 'cmd /c c:\\pheno\\whap --file c:\\pheno\\smri --alt 1'
On Mon, 2006-09-25 at 18:58 +0200, Boks, M.P.M. wrote: Dear R users, This command works (calling a programm -called whap- with file specifiers etc.): system('cmd /c c:\\pheno\\whap --file c:\\pheno\\smri --alt 1 --perm 500', intern=TRUE) Now I need to call it from a loop to replace the 1 by different number, however I get lost using the quotes: I tried numerous versions of: i-1 system(paste(c('cmd /c c:\\pheno\\whap --file c:\\pheno\\smri --alt, i, --perm 500', sep= )), intern=TRUE) However no luck! I would be gratefull for any help. Thanks, Marco You need to escape the quote () chars in the paste()d string so that they get passed to your command properly. Also, you don't want to use c() within the paste() function, as the paste() function already concatenates the component vectors. Note: i - 1 paste('cmd /c c:\\pheno\\whap --file c:\\pheno\\smri --alt, i, --perm 500', sep=) Error: syntax error in paste('cmd /c c R sees the double quote before the second 'c' as the end of the string: 'cmd /c Now use \ to escape the internal quotes: paste('cmd /c \c:\\pheno\\whap --file c:\\pheno\\smri --alt , i, --perm 500\', sep=) [1] 'cmd /c \c:\\pheno\\whap --file c:\\pheno\\smri --alt 1 --perm 500\' Use '\' to escape each of the double quotes within the string, so that R can differentiate string delimiters versus characters within the string. HTH, Marc Schwartz __ 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] extract data from lm object and then use again?
On Fri, 2006-09-22 at 10:45 -0400, Mike Wolfgang wrote: Hi list, I want to write a general function so that it would take an lm object, extract its data element, then use the data at another R function (eg, glm). I searched R-help list, and found this would do the trick of the first part: a.lm$call$data this would return a name object but could not be recognized as a data.frameby glm. I also tried call(as.character(a.lm$call$data)) or eval(call(as.character(a.lm$call$data))) neither works. By eval(call(...)), it acts as evaluating of a function, but what I want is just a data frame object which could be inserted into glm function. Anyone could help? Thanks, Mike If the 'data' argument in lm() is used, then this approach could work: Iris2 - eval(lm(Sepal.Length ~ Species, data = iris)$call$data) str(Iris2) `data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 1 ... However, note that you do not get the actual data used within the lm() function (the model frame) but the entire source data frame. What you likely want instead is the model frame containing the columns actually used in the model formula: Iris3 - lm(Sepal.Length ~ Species, data = iris)$model str(Iris3) `data.frame': 150 obs. of 2 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 1 ... - attr(*, terms)=Classes 'terms', 'formula' length 3 Sepal.Length ~ Species .. ..- attr(*, variables)= language list(Sepal.Length, Species) .. ..- attr(*, factors)= int [1:2, 1] 0 1 .. .. ..- attr(*, dimnames)=List of 2 .. .. .. ..$ : chr [1:2] Sepal.Length Species .. .. .. ..$ : chr Species .. ..- attr(*, term.labels)= chr Species .. ..- attr(*, order)= int 1 .. ..- attr(*, intercept)= int 1 .. ..- attr(*, response)= int 1 .. ..- attr(*, .Environment)=length 15 environment .. ..- attr(*, predvars)= language list(Sepal.Length, Species) .. ..- attr(*, dataClasses)= Named chr [1:2] numeric factor .. .. ..- attr(*, names)= chr [1:2] Sepal.Length Species HTH, Marc Schwartz __ 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] inequality with NA
On Fri, 2006-09-22 at 20:16 +0200, Mag. Ferri Leberl wrote: Dear everybody! take a-c(5,3,NA,6). if(a[1]!=NA){b-7} if(a[3]!=5){b-7} if(a[3]!=NA){b-7} if(a[3]==NA){b-7} will alltogeather return Fehler in if (a[1] != NA) { : Fehlender Wert, wo TRUE/FALSE nötig ist (or simularly). Somehow this is logical. But how else should I get out, whether a certain vector-component has an existing value? Thank you in advance! Yours, Mag. Ferri Leberl NA is not defined, so you cannot predictably perform equality/inequality tests with it. There are specific functions in place for dealing with this. See ?is.na and ?na.omit a [1] 5 3 NA 6 a[is.na(a)] [1] NA a[!is.na(a)] [1] 5 3 6 You can also use which() to find the indices: which(is.na(a)) [1] 3 which(!is.na(a)) [1] 1 2 4 Finally, use na.omit() to remove all NA's: na.omit(a) [1] 5 3 6 attr(,na.action) [1] 3 attr(,class) [1] omit Note that the object attribute 'na.action' shows that a[3] was removed: a.omit - na.omit(a) as.vector(attr(a.omit, na.action)) [1] 3 HTH, Marc Schwartz __ 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] proj4R library will not install
On Fri, 2006-09-22 at 17:16 -0400, Philip Bermingham wrote: I'm hoping someone can help me. I have downloaded the proj4R.zip and under my version of R (2.3.1) I install the package from local zip file. This worked great. I then type library(proj4R) to load the library and I get the error: Error in library(proj4R) : 'proj4R' is not a valid package -- installed 2.0.0? I have read through the install documentation and have downloaded and unpacked PROJ.4 to c: \proj\ so the bin is located at C:\proj\bin. I then set the environmental variables PATH which now looks like : %SystemRoot% \system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files \Intel\DMIX;C:\Program Files\UltraEdit;C:\proj and I created a new user variable PROJ_LIB to c:\proj\nad. I'm not sure if I am missing anything here but I still get the 2.0.0 error. If you can help me in any way I would truly appreciate it. Thanks in advance, Philip Bermingham proj4R is not a base or CRAN R package. Some Googling suggests that the R package might be deprecated, as it has not been updated for some time (hence the error msgs) based upon a review of the R related archive files available at: http://spatial.nhh.no/R/Devel/ I would suggest communicating with Roger Bivand (who I have cc'd here) as to the status of the package and any subsequent replacements. HTH, Marc Schwartz __ 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] R CMD check fails at package dependencies check on Fedora Core 5, works on other systems
On Wed, 2006-09-20 at 13:20 +0200, Kurt Hornik wrote: Marc Schwartz (via MN) writes: On Tue, 2006-09-19 at 22:16 +1000, Robert King wrote: Here is another thing that might help work out what is happening. If I use --no-install, ade4 actually fails as well, in the same way as zipfR. [Desktop]$ R CMD check --no-install ade4 * checking for working latex ... OK * using log directory '/home/rak776/Desktop/ade4.Rcheck' * using Version 2.3.1 (2006-06-01) * checking for file 'ade4/DESCRIPTION' ... OK * this is package 'ade4' version '1.4-1' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... ERROR [Desktop]$ R CMD check --no-install zipfR * checking for working latex ... OK * using log directory '/home/rak776/Desktop/zipfR.Rcheck' * using Version 2.3.1 (2006-06-01) * checking for file 'zipfR/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'zipfR' version '0.6-0' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... ERROR snip Robert, I tried the process last night (my time) using the initial instructions on my FC5 system with: $ R --version R version 2.3.1 Patched (2006-08-06 r38829) Copyright (C) 2006 R Development Core Team I could not replicate the problem. However, this morning, with your additional communication: $ R CMD check --no-install zipfR_0.6-0.tar.gz * checking for working latex ... OK * using log directory '/home/marcs/Downloads/zipfR.Rcheck' * using Version 2.3.1 Patched (2006-08-06 r38829) * checking for file 'zipfR/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'zipfR' version '0.6-0' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking top-level files ... OK * checking index information ... OK * checking package subdirectories ... OK * checking R files for syntax errors ... OK * checking R files for library.dynam ... OK * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK * checking Rd files ... OK * checking Rd cross-references ... WARNING Warning in grep(pattern, x, ignore.case, extended, value, fixed, useBytes) : input string 70 is invalid in this locale * checking for missing documentation entries ... WARNING Warning in grep(pattern, x, ignore.case, extended, value, fixed, useBytes) : input string 70 is invalid in this locale All user-level objects in a package should have documentation entries. See chapter 'Writing R documentation files' in manual 'Writing R Extensions'. * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking DVI version of manual ... OK WARNING: There were 2 warnings, see /home/marcs/Downloads/zipfR.Rcheck/00check.log for details So I am wondering if this raises the possibility of a locale issue on your FC5 system resulting in a problem reading DESCRIPTION files? It may be totally unrelated, but one never knows I suppose. Mine is: $ locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 LC_NUMERIC=en_US.UTF-8 LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8 LC_ALL= HTH, Marc Schwartz That's a bug in tools:::Rd_aliases (it needs to preprocess the Rd lines, which re-encodes if necessary and possible). I'll commit a fix later today. Thanks for spotting this. Best -k Thanks for noting this Kurt! Regards, Marc __ 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] Beginners manual for emacs and ess
On Wed, 2006-09-20 at 17:03 +0200, Rainer M Krug wrote: Hi I heard so much about Emacs and ESS that I decided to try it out - but I am stuck at the beginning. Is there anywhere a beginners manual for Emacs ESS to be used with R? even M-x S tells me it can't start S-Plus - obviously - but I want it to start R... Any help welcome (otherwise I will be stuck with Eclipse and R) Rainer There are some reference materials on the main ESS site at: http://ess.r-project.org/ In addition, there is a dedicated ESS mailing list, with more info here: https://stat.ethz.ch/mailman/listinfo/ess-help HTH, Marc Schwartz __ 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] [Rd] Sweave processes \Sexpr in commented LaTeX source (2.3.1patched and 2.4.0)
On Wed, 2006-09-20 at 09:09 +0200, Antonio, Fabio Di Narzo wrote: Hi. 2006/9/20, Marc Schwartz [EMAIL PROTECTED]: Hi all, On FC5, using: Version 2.3.1 Patched (2006-08-06 r38829) and today's R version 2.4.0 alpha (2006-09-19 r39397) with the following .Rnw file: \documentclass[10pt]{article} \begin{document} This line should print '2': \Sexpr{1 + 1} %% This line should NOT print '2': \Sexpr{1 + 1} If it's just a comment, why don't use something like: % \ Sexpr (del the space) or %\sexpr (change 'sexpr' with 'Sexpr') or %...the 'Sexpr' command (add a backslash in latex code) ? Antonio. See my comments in this post on r-devel, where this thread was originally started: https://stat.ethz.ch/pipermail/r-devel/2006-September/039416.html HTH, Marc __ 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] [ROracle] error loading (undefined symbol: sqlclu)
On Wed, 2006-09-20 at 15:15 -0400, Mathieu Drapeau wrote: I have this error when I load the library ROracle: library(ROracle) Loading required package: DBI Error in dyn.load(x, as.logical(local), as.logical(now)) : unable to load shared library '/usr/local/lib/R/site-library/ROracle/libs/ROracle.so': /usr/local/lib/R/site-library/ROracle/libs/ROracle.so: undefined symbol: sqlclu Error in library(ROracle) : .First.lib failed for 'ROracle' Also, my LD_LIBRARY_PATH seems to be set correctly: drapeau:~ echo $LD_LIBRARY_PATH /home/drapeau/lib:/opt/oracle/xe/app/oracle/product/10.2.0/client/lib I installed the big database applications (10g) and ROracle 0.5-7 Your help will be very appreciated to help me solve this error, Thank you, Mathieu Where did you set LD_LIBRARY_PATH? If in one of your shell config files, it is likely that it is being stepped on during your login and thus not being seen within the R session. I had this problem previously and set the variable in /etc/ld.so.conf (though I am using RODBC instead). Edit that file (as root), add the path: /opt/oracle/xe/app/oracle/product/10.2.0/client/lib and then run [/sbin/]ldconfig to update the current settings. Be sure also that $ORACLE_HOME is set to /opt/oracle/xe/app/oracle/product/10.2.0/client Then try to load ROracle in a new R session. HTH, Marc Schwartz __ 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] R CMD check fails at package dependencies check on Fedora Core 5, works on other systems
On Tue, 2006-09-19 at 22:16 +1000, Robert King wrote: Here is another thing that might help work out what is happening. If I use --no-install, ade4 actually fails as well, in the same way as zipfR. [Desktop]$ R CMD check --no-install ade4 * checking for working latex ... OK * using log directory '/home/rak776/Desktop/ade4.Rcheck' * using Version 2.3.1 (2006-06-01) * checking for file 'ade4/DESCRIPTION' ... OK * this is package 'ade4' version '1.4-1' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... ERROR [Desktop]$ R CMD check --no-install zipfR * checking for working latex ... OK * using log directory '/home/rak776/Desktop/zipfR.Rcheck' * using Version 2.3.1 (2006-06-01) * checking for file 'zipfR/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'zipfR' version '0.6-0' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... ERROR snip Robert, I tried the process last night (my time) using the initial instructions on my FC5 system with: $ R --version R version 2.3.1 Patched (2006-08-06 r38829) Copyright (C) 2006 R Development Core Team I could not replicate the problem. However, this morning, with your additional communication: $ R CMD check --no-install zipfR_0.6-0.tar.gz * checking for working latex ... OK * using log directory '/home/marcs/Downloads/zipfR.Rcheck' * using Version 2.3.1 Patched (2006-08-06 r38829) * checking for file 'zipfR/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'zipfR' version '0.6-0' * checking if this is a source package ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking top-level files ... OK * checking index information ... OK * checking package subdirectories ... OK * checking R files for syntax errors ... OK * checking R files for library.dynam ... OK * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK * checking Rd files ... OK * checking Rd cross-references ... WARNING Warning in grep(pattern, x, ignore.case, extended, value, fixed, useBytes) : input string 70 is invalid in this locale * checking for missing documentation entries ... WARNING Warning in grep(pattern, x, ignore.case, extended, value, fixed, useBytes) : input string 70 is invalid in this locale All user-level objects in a package should have documentation entries. See chapter 'Writing R documentation files' in manual 'Writing R Extensions'. * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking DVI version of manual ... OK WARNING: There were 2 warnings, see /home/marcs/Downloads/zipfR.Rcheck/00check.log for details So I am wondering if this raises the possibility of a locale issue on your FC5 system resulting in a problem reading DESCRIPTION files? It may be totally unrelated, but one never knows I suppose. Mine is: $ locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 LC_NUMERIC=en_US.UTF-8 LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8 LC_ALL= HTH, Marc Schwartz __ 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
On Mon, 2006-09-18 at 19:31 +0200, Iñaki Murillo Arcos wrote: Hello, I don't know if the result of acos(0.5) == pi/3 is a bug or not. It looks strange to me. Inaki Murillo Seems reasonable to me: acos(0.5) == pi/3 [1] FALSE print(acos(0.5), 20) [1] 1.0471975511965978534 print(pi/3, 20) [1] 1.0471975511965976313 See R FAQ 7.31 Why doesn't R think these numbers are equal? HTH, Marc Schwartz __ 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] group bunch of lines in a data.frame, an additional requirement
Try something like this: # Initial data frame DF V1 V2 V3 V4 1 A 1.0 200 ID1 2 A 3.0 800 ID1 3 A 2.0 200 ID1 4 B 0.5 20 ID2 5 B 0.9 50 ID2 6 C 5.0 70 ID1 # Now do the aggregation to get the means DF.1 - aggregate(DF[, 2:3], list(V1 = DF$V1), mean) DF.1 V1 V2 V3 1 A 2.0 400 2 B 0.7 35 3 C 5.0 70 # Now get the unique combinations of letters and IDs in DF DF.U - unique(DF[, c(V1, V4)]) DF.U V1 V4 1 A ID1 4 B ID2 6 C ID1 # Now merge the two data frames together, matching the letters DF.NEW - merge(DF.1, DF.U, by = V1) DF.NEW V1 V2 V3 V4 1 A 2.0 400 ID1 2 B 0.7 35 ID2 3 C 5.0 70 ID1 See ?unique and ?merge for more information. Also, for the sake of clarification, these are not matrices, but data frames. A matrix may contain only one data type, whereas data frames are specifically designed to contain multiple data types as you have here. HTH, Marc Schwartz On Wed, 2006-09-13 at 17:38 +0100, Emmanuel Levy wrote: Thanks for pointing me out aggregate, that works fine! There is one complication though: I have mixed types (numerical and character), So the matrix is of the form: A 1.0 200 ID1 A 3.0 800 ID1 A 2.0 200 ID1 B 0.5 20 ID2 B 0.9 50 ID2 C 5.0 70 ID1 One letter always has the same ID but one ID can be shared by many letters (like ID1) I just want to keep track of the ID, and get a matrix like: A 2.0 400 ID1 B 0.7 35 ID2 C 5.0 70 ID1 Any idea on how to do that without a loop? Many thanks, Emmanuel On 9/12/06, Emmanuel Levy [EMAIL PROTECTED] wrote: Hello, I'd like to group the lines of a matrix so that: A 1.0 200 A 3.0 800 A 2.0 200 B 0.5 20 B 0.9 50 C 5.0 70 Would give: A 2.0 400 B 0.7 35 C 5.0 70 So all lines corresponding to a letter (level), become a single line where all the values of each column are averaged. I've done that with a loop but it doesn't sound right (it is very slow). I imagine there is a sort of apply shortcut but I can't figure it out. Please note that it is not exactly a matrix I'm using, the function typeof tells me it's a list, however I access to it like it was a matrix. Could someone help me with the right function to use, a help topic or a piece of code? Thanks, Emmanuel __ 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] coerce matrix to number
On Tue, 2006-09-12 at 18:42 +0200, Simone Gabbriellini wrote: Dear List, how can I coerce a matrix like this [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0 to be filled with numbers? this is the result of replacing some character (v, d) with 0 and 1, using the code I found with RSiteSearch() z[] - lapply(z, factor, levels = c(d, v), labels = c(0, 1)); thank you, Simone I reverse engineered your (presumably) original data frame: z 1 2 3 4 5 6 1 d v v d d d 2 v d v d d d 3 v v d d d d 4 d d d d v d 5 d d d v d d 6 d d d d d d str(z) `data.frame': 6 obs. of 6 variables: $ 1: Factor w/ 2 levels d,v: 1 2 2 1 1 1 $ 2: Factor w/ 2 levels d,v: 2 1 2 1 1 1 $ 3: Factor w/ 2 levels d,v: 2 2 1 1 1 1 $ 4: Factor w/ 2 levels d,v: 1 1 1 1 2 1 $ 5: Factor w/ 2 levels d,v: 1 1 1 2 1 1 $ 6: Factor w/ 2 levels d,v: 1 1 1 1 1 1 If that is correct, then the following should yield what you want in one step: z.num - sapply(z, function(x) as.numeric(x) - 1) z.num 1 2 3 4 5 6 [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0 str(z.num) num [1:6, 1:6] 0 1 1 0 0 0 1 0 1 0 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:6] 1 2 3 4 ... Alternatively, if you were starting out with the character matrix: z.char [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 1 1 0 0 0 [2,] 1 0 1 0 0 0 [3,] 1 1 0 0 0 0 [4,] 0 0 0 0 1 0 [5,] 0 0 0 1 0 0 [6,] 0 0 0 0 0 0 You could do: storage.mode(z.char) - numeric z.char [,1] [,2] [,3] [,4] [,5] [,6] [1,]011000 [2,]101000 [3,]110000 [4,]000010 [5,]000100 [6,]000000 str(z.char) num [1:6, 1:6] 0 1 1 0 0 0 1 0 1 0 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : NULL Yet another alternative: matrix(as.numeric(z.char), dim(z.char)) [,1] [,2] [,3] [,4] [,5] [,6] [1,]011000 [2,]101000 [3,]110000 [4,]000010 [5,]000100 [6,]000000 HTH, Marc Schwartz __ 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] barplot: different colors for the bar and the strips
On Thu, 2006-09-07 at 12:14 -0500, Hao Chen wrote: Hello Marc Schwartz On Thu, Sep 07, 2006 at 07:54:05AM -0500, Marc Schwartz wrote: On Thu, 2006-09-07 at 06:18 -0500, Hao Chen wrote: Hi, I am using barplot and would like to know if it is possible to have bars filled with one color while use a different color for the shading lines. The following code colors the shading lines, leaving the bars in white: barplot(1:5, col=c(1:5), density=c(1:5)*5) while the colors are applied to the bars when density is removed. barplot(1:5, col=c(1:5)) I did check ?barplot and found the following: col: a vector of colors for the bars or bar components. Thanks, Hao Note the key word 'or' in the description of the 'col' argument. You need to make two separate calls to barplot(). The first using the fill colors, then the second using the shading lines AND setting 'add = TRUE', so that the second plot overwrites the first without clearing the plot device. barplot(1:5, col=c(1:5)) barplot(1:5, col = black, density=c(1:5), add = TRUE) Just be sure that any other arguments, such as axis limits, are identical between the two calls. HTH, Marc Schwartz Thank you very much for your help. It works but only in the order as you put it, since the following code only shows the color, but not the shading lines: barplot(1:5, col = black, density=c(1:5)) barplot(1:5, col=c(1:5), add = TRUE) Hao Chen That is correct. The sequence is important, as the shading lines are drawn with a transparent background, enabling the original color to be seen. Reversing the order, you are overplotting the shading lines with opaque colored rectangles. Hence, the lines are lost. HTH, Marc __ 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] [OT] Important stat dates
On Thu, 2006-09-07 at 11:57 -0500, Erin Hodgess wrote: Dear R People: Way Off Topic: Is anyone aware of a website that contains important dates in statistics history, please? Maybe a sort of This Day in Statistics, please? I thought that my students might get a kick out of that. (actually I will probably enjoy it more than them!) Thanks for any help! I tried (via Google) today in statistics and today in statistics history but nothing worthwhile appeared. Here are two pages that you might find helpful: http://www.york.ac.uk/depts/maths/histstat/welcome.htm http://www.economics.soton.ac.uk/staff/aldrich/Figures.htm Both have additional references and reciprocal links. HTH, Marc Schwartz __ 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] installing the x86_64 R Binary on Fedora Core 5
On Thu, 2006-08-24 at 10:53 -0400, roger bos wrote: I am looking for help install the x86_64 R Binary onto my FC5 machine. At the risk of subjecting myself to tons of criticism, I must confess that I don't know anything about Linux and I have never compiled R from source. Therefore, I choose FC5 because I see that a 64-bit binary is already available. Here is what I tried: I installed FC5 with all options (productivity, software development, and web server). FC5 boots up fine. I downloaded all the R binary files in that FC5 directory to my USB drive and copied them onto my linux machine (where I don't yet have internet access). I created a folder in my rbos's Home directory and copied the files there. I clicked on the R-2.3.1-1.fc5.x86_64.rpm and right-clicked to choose 'open with install software' It asked me for my root password. I put in the same root password I choose when I installed FC5. I get a installing packages screen that shows the R filename and I click Apply. It then give me an Error: Unable to retrieve software information. Can anyone tell me what steps I am missing? The R install guide states that binary installs are platform specific so it only considers building from the sources. I look forward to learning a lot about Linux and using more than just the GUI, but to get started, I just want to learn how to install a binary of R. Thanks so much, Roger Roger, I don't know if the GUI version of the RPM interface (Red Hat/Fedora's Package Manager) is sufficiently robust these days. In the past, there were notable problems trying to use it to install software binaries. I would open a console and change into the folder where you copied the files. If you are running GNOME and do not have a Terminal launcher (icon) on any of your panels, you can go to the menus and select: Applications - Accessories - Terminal This will open a console on your desktop, just like the Windows command line console. Then change to the appropriate folder: cd /Path/To/FolderName Note that unlike Windows, the slashes are '/', not '\'. Then in that folder, type: su You will be prompted for the root password. If successful, you will note that the prompt prefix changes from a '$' to a '#'. Then, type: rpm -ivh R-2.3.1-1.fc5.x86_64.rpm This will begin the R installation process. If you get back to the prompt without any error messages, you should be good to go. Then type: exit at the console, which will exit root status and bring you back to your regular user ID (prompt prefix back to '$'). Needless to say, that while you have root privileges in the console, be careful in what you might type. You have total access to screw up the system... :-) If you get any error messages, post them back here and we can help debug the process. HTH, Marc Schwartz __ 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] installing the x86_64 R Binary on Fedora Core 5
Roger, The Windows packages will not run on Linux. You will need to install them using the Linux versions (.tar.gz files) of the CRAN packages. You can copy them from a CRAN mirror to your USB drive and then install them locally using R CMD INSTALL PackageName.tar.gz This will again need to be done as root using 'su' from the console. Note that depending upon the nature of the package (ie. does it uses C and/or FORTRAN code or require third party libraries), you may get errors during the installation process if you are missing required code/applications/header files. HTH, Marc On Thu, 2006-08-24 at 11:50 -0400, roger bos wrote: Thanks so much Marc Stefan! The GUI wasn't telling me what I was missing. The terminal told me I was missing tk-8.4.12-1.2.x86_64.rpm so I went and got that and it installed without errors. Then I could't figure out how to launch R through the GUI so I went back to the terminal and typed R and it launched. I have a lot to learn but I want to thank you so much for getting me started. Another question: As I said, I don't have internet access on the linux machine, so is there any way to copy the library folder from my windows machine to the linux box and install the packages from there? Roger On 8/24/06, Stefan Grosse [EMAIL PROTECTED] wrote: Hi Roger, I dunno what exactly might be the source of that mistake but I would strongly recommend to install R while you are online. Often other packages must be installed for dependencies. (And then I recommend using the smart package manager ( http://labix.org/smart ) which is a great tool and handling dependencies better.) You could use rpm at the command line level. open a shell, type su and give your password, change to the folder where the rpm is downloaded to, type rpm -ivh R-2.3.1-1.fc5.x86_64.rpm will try to install and give you information on whats missing... Stefan Grosse roger bos schrieb: I am looking for help install the x86_64 R Binary onto my FC5 machine. At the risk of subjecting myself to tons of criticism, I must confess that I don't know anything about Linux and I have never compiled R from source. Therefore, I choose FC5 because I see that a 64-bit binary is already available. Here is what I tried: I installed FC5 with all options (productivity, software development, and web server). FC5 boots up fine. I downloaded all the R binary files in that FC5 directory to my USB drive and copied them onto my linux machine (where I don't yet have internet access). I created a folder in my rbos's Home directory and copied the files there. I clicked on the R-2.3.1-1.fc5.x86_64.rpm and right-clicked to choose 'open with install software' It asked me for my root password. I put in the same root password I choose when I installed FC5. I get a installing packages screen that shows the R filename and I click Apply. It then give me an Error: Unable to retrieve software information. Can anyone tell me what steps I am missing? The R install guide states that binary installs are platform specific so it only considers building from the sources. I look forward to learning a lot about Linux and using more than just the GUI, but to get started, I just want to learn how to install a binary of R. Thanks so much, Roger [[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 and provide commented, minimal, self-contained, reproducible code. [[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 and provide commented, minimal, self-contained, reproducible code. __ 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] generating an expression for a formula automatically
On Thu, 2006-08-24 at 14:01 -0700, Maria Montez wrote: Hi! I would like to be able to create formulas automatically. For example, I want to be able to create a function that takes on two values: resp and x, and then creates the proper formula to regress resp on x. My code: fit.main - function(resp,x) { form - expression(paste(resp, ~ ,paste(x,sep=,collapse= + ),sep=)) z - lm(eval(form)) z } main - fit.main(y,c(x1,x2,x3,x4)) and I get this error: Error in terms.default(formula, data = data) : no terms component Any suggestions? Thanks, Maria See the last example in ?as.formula: BTW, I would pay note to the ability to use subset()'s of data frames in model functions. For example, let's say that your data frame above is called DF and contains columns 'y' and then 'x1' through 'x50' in sequence. However, you only want to use the columns you have indicated in your code above. You can then do: lm(y ~ ., data = subset(DF, select = y:x4)) The use of the '.' on the RHS of the formula indicates to use all other columns besides the response column in the formula. In the subset() function, you can specify a sequential group of columns using the ':' operator. For a specific example, let's use the iris data set, which has columns: names(iris) [1] Sepal.Length Sepal.Width Petal.Length Petal.Width [5] Species We want to use 'Sepal.Length' as the response variable and then all columns, other than 'Species', as terms: lm(Sepal.Length ~ ., data = subset(iris, select = -Species)) Call: lm(formula = Sepal.Length ~ ., data = subset(iris, select = -Species)) Coefficients: (Intercept) Sepal.Width Petal.Length Petal.Width 1.85600.65080.7091 -0.5565 In this case, I excluded the Species columns by using the '-' before the column name. However, I could have easily used: lm(Sepal.Length ~ ., data = subset(iris, select = Sepal.Length:Petal.Width)) Call: lm(formula = Sepal.Length ~ ., data = subset(iris, select = Sepal.Length:Petal.Width)) Coefficients: (Intercept) Sepal.Width Petal.Length Petal.Width 1.85600.65080.7091 -0.5565 See ?subset for additional information. HTH, Marc Schwartz __ 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] Escaping ' character
On Tue, 2006-08-22 at 07:32 -0700, Srinivas Iyyer wrote: Dear marc: thank you for your tip. cat(x.new, \n) 3\',5\'-cyclic-nucleotide phosphodiesterase activity here cat is printing on screen. how can I direct the output to an object. I cannot do: y - cat(x.new, \n) is there any other way. thanks srini Srini, Going back to your initial post, try something like the following using paste(): x - 3',5'-cyclic-nucleotide phosphodiesterase activity x.new - gsub(', ', x) #Note the escaping of the single quotes here: sql.cmd - paste(fetch_count_fterm_sql(\', (x.new), \');, sep = ) # Beware any line wrapping here sql.cmd [1] fetch_count_fterm_sql('3\\',5\\'-cyclic-nucleotide phosphodiesterase activity'); This way the character vector 'sql.cmd' has the full sql query, which you can then pass to your statement processing code. I'm not sure how you are passing the code, but if in a text file as input, you can do something like: sink(sqlfile.txt) cat(sql.cmd, \n) sink() Where the text file 'sqlfile.txt' will contain the single line: fetch_count_fterm_sql('3\',5\'-cyclic-nucleotide phosphodiesterase activity'); See ?sink for more information. HTH, Marc __ 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] Escaping ' character
On Mon, 2006-08-21 at 13:43 -0700, Srinivas Iyyer wrote: Dear all: I have a character object x with ' (single-quote) character. x - c('hydrolase activity,actin binding,3',5'-cyclic-nucleotide phosphodiesterase activity) I want to write a function that will identify ' and replaces with \' myf - function(term){ if (grep(',term)) { sub(',\',term)} } myf(x) [1] hydrolase activity [2] actin binding [3] 3',5'-cyclic-nucleotide phosphodiesterase activity In the result 3',5' is remains unchaned. I expected that to 3\',5\'-cyclic-nucleotide phosphodiesterase activity. Could any one help me here. Thank you. snip Srini, Try this: x - 3',5'-cyclic-nucleotide phosphodiesterase activity x [1] 3',5'-cyclic-nucleotide phosphodiesterase activity gsub(', ', x) [1] 3\\',5\\'-cyclic-nucleotide phosphodiesterase activity Note that I use gsub() to replace both instances of the ', whereas sub() will only replace the first. The escape character itself needs to be escaped when used within the replacement regex, since the \ is a metacharacter. You end up with four \s since R also treats the \ specially. When you cat() the output, you get: x.new [1] 3\\',5\\'-cyclic-nucleotide phosphodiesterase activity cat(x.new, \n) 3\',5\'-cyclic-nucleotide phosphodiesterase activity HTH, Marc Schwartz __ 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] as.data.frame(cbind()) transforming numeric to factor?
On Fri, 2006-08-18 at 10:41 -0400, Tom Boonen wrote: Dear List, why does as.data.frame(cbind()) transform numeric variables to factors, once one of the other variablesused is a character vector? # x.1 - rnorm(10) x.2 - c(rep(Test,10)) Foo - as.data.frame(cbind(x.1)) is.factor(Foo$x.1) Foo - as.data.frame(cbind(x.1,x.2)) is.factor(Foo$x.1) # I assume there is a good reason for this, can somebody explain? Thanks. Best, Tom See the Note section of ?cbind, which states: The method dispatching is not done via UseMethod(), but by C-internal dispatching. Therefore, there is no need for, e.g., rbind.default. The dispatch algorithm is described in the source file (‘.../src/main/bind.c’) as 1. For each argument we get the list of possible class memberships from the class attribute. 2. We inspect each class in turn to see if there is an an applicable method. 3. If we find an applicable method we make sure that it is identical to any method determined for prior arguments. If it is identical, we proceed, otherwise we immediately drop through to the default code. If you want to combine other objects with data frames, it may be necessary to coerce them to data frames first. (Note that this algorithm can result in calling the data frame method if the arguments are all either data frames or vectors, and this will result in the coercion of character vectors to factors.) Thus, note the result of: str(cbind(x.1, x.2)) chr [1:10, 1:2] -0.265756038510064 2.13220714034528 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:2] x.1 x.2 Since a matrix can only contain a single data type, the numeric vector is coerced to character. Then using as.data.frame() coerces the character matrix to factors, which is the default behavior. If you want to create a data frame, do it this way: str(data.frame(x.1, x.2)) `data.frame': 10 obs. of 2 variables: $ x.1: num -0.266 2.132 2.096 -0.128 -0.466 ... $ x.2: Factor w/ 1 level Test: 1 1 1 1 1 1 1 1 1 1 or if you want to retain the character vector, use I(): str(data.frame(x.1, I(x.2))) `data.frame': 10 obs. of 2 variables: $ x.1: num -0.266 2.132 2.096 -0.128 -0.466 ... $ x.2:Class 'AsIs' chr [1:10] Test Test Test Test ... See ?data.frame for more information. HTH, Marc Schwartz __ 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] apply least angle regression to generalized linear models
On Fri, 2006-08-18 at 11:17 -0400, Mike Wolfgang wrote: Hello list, I've been searching around trying to find whether somebody has written such a package of least angle regression on generalized linear models, like what Lasso2 package does. The extension to generalized linear models is briefly discussed in the comment by D. Madigan and G. Ridgeway. Is such a package available? Thanks, Mike See the aptly named 'lars' package on CRAN and the attendant paper here: http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf You might also want to review Professor Hastie's presentation at useR! 2006 this past spring: http://www.r-project.org/useR-2006/Slides/Hastie.pdf HTH, Marc Schwartz __ 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] apply least angle regression to generalized linear models
Andy, Upon further review of the documentation for lars, you are correct. Thanks for the pointer to the work by Tim et al. Regards, Marc On Fri, 2006-08-18 at 12:48 -0400, Liaw, Andy wrote: I believe `lars' does not currently fit glms. For that you'll probably need to look at `glar', at: http://www.insightful.com/Hesterberg/glars/default.asp HTH, Andy From: Marc Schwartz On Fri, 2006-08-18 at 11:17 -0400, Mike Wolfgang wrote: Hello list, I've been searching around trying to find whether somebody has written such a package of least angle regression on generalized linear models, like what Lasso2 package does. The extension to generalized linear models is briefly discussed in the comment by D. Madigan and G. Ridgeway. Is such a package available? Thanks, Mike See the aptly named 'lars' package on CRAN and the attendant paper here: http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf You might also want to review Professor Hastie's presentation at useR! 2006 this past spring: http://www.r-project.org/useR-2006/Slides/Hastie.pdf HTH, Marc Schwartz __ 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. -- Notice: This e-mail message, together with any attachment...{{dropped}} __ 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] list of lists to a data.frame
On Fri, 2006-08-18 at 16:44 -0400, Rajarshi Guha wrote: Hi, I have a situation where I have a list of lists. Each list can contain elements of different types (but each one will be a scalar) say of double, integer or character. However the elements of each list are always in the same order: x - list('a', 1, 2) y - list('b', 3, 4) z - list('c', 5, 6) a - list(x,y,z) What I'd like to do is to convert a to a data.frame. Currently I am doing: b - do.call(rbind, a) However, when I do b[,1] I get a list returned rather than a vector of characters and similarly for b[,2] and so on. I am clearly missing something, but how do I convert the list of lists to a data.frame where a column is represented as a vector rather than a list? Thanks, How about: as.data.frame(sapply(a, rbind)) V1 V2 V3 1 a b c 2 1 3 5 3 2 4 6 or: as.data.frame(t(sapply(a, rbind))) V1 V2 V3 1 a 1 2 2 b 3 4 3 c 5 6 depending upon how you wanted the columns versus rows to be structured. HTH, Marc Schwartz __ 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] Boxplot Help: Re-ordering the x-axis
On Thu, 2006-08-17 at 11:46 -0700, Pamela Allen wrote: I am having a problem using boxlpot with my data. I have my data arranged in a data table, and two of my columns are mass and month. I am trying to plot the mass of my study animals by month, thus I would like to have it in the order of January to December. The problem is that R orders each month in alphabetical order, and gives each month an integer value corresponding to this (i.e. April is integer=1, August=2, September=12). I have tried many different ways to solve this but nothing is working. If anyone knows how to order the x-axis in boxplot, or alternatively, re-assign integer values to each month that would be very helpful. Thank you in advance! Note the following in the Details section of ?boxplot: If multiple groups are supplied either as multiple arguments or via a formula, parallel boxplots will be plotted, in the order of the arguments or the order of the levels of the factor (see factor). If you are using a formula approach, then something like the following: month - factor(month, levels = c(January, February, ..., November, December) boxplot(mass ~ month) See ?factor For future reference, using: RSiteSearch(boxplot order) will search the r-help archive using the indicated key words, where you will see that this has been covered previously. HTH, Marc Schwartz __ 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] separate row averages for different parts of an array
On Wed, 2006-08-16 at 11:36 -0600, Spencer Jones wrote: I have an array with 44800 columns and 24 rows I would like to compute the row average for the array 100 columns at a time, so I would like to end up with an array of 24 rows x 448 columns. I have tried using apply(dataset, 1, function(x) mean(x[])), but I am not sure how to get it to take the average 100 columns at a time. Any ideas would be welcomed. thanks, Spencer Something along the lines of the following, presuming that 'mat' is your 24 * 44800 matrix: sapply(seq(1, 44800, 100), function(x) rowMeans(mat[, x:(x + 99)])) The first argument in sapply() creates a sequence from 1:44800 by increments of 100. sapply() then passes each value in the sequence as the starting index value 'x' to use to subset 'mat' in 100 column sets and gets the rowMeans() for each sub-matrix. The returned object will be a 24 * 448 matrix. HTH, Marc Schwartz __ 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] read.csv issue
On Wed, 2006-08-16 at 14:43 -0400, Doran, Harold wrote: I'm trying to read in some data from a .csv format and have come across the following issue. Here is a simple example for replication # A sample .csv format schid,sch_name 331-802-7081,School One 464-551-7357,School Two 388-517-7627,School Three \ Four 388-517-4394,School Five Note the third line includes the \ character. However, when I read the data in I get read.csv(file.choose()) schid sch_name 1 331-802-7081School One 2 464-551-7357School Two 3 388-517-7627 School Three Four 4 388-517-4394 School Five It turns out to be very important to read in this character as I have a program that loops through a data set and Sweave's about 30,000 files. The variable sch_name gets dropped into the tex file using \Sexpr{tmp$sch_name}. However, if there is an , the latex file won't compile properly. So, what I need is for the data to be read in as schid sch_name 1 331-802-7081School One 2 464-551-7357School Two 3 388-517-7627 School Three \ Four 4 388-517-4394 School Five I am obligated by a client to include the in the school name, so eliminating that isn't an option. I thought maybe comment.char or quote would be what I needed, but they didn't resolve the issue. I'm certain I'm missing something simple, I just can't see it. Any thoughts? Harold Harold, What version of R and OS are you running? Under: Version 2.3.1 Patched (2006-08-06 r38829) on FC5: read.csv(test.csv) schid sch_name 1 331-802-7081School One 2 464-551-7357School Two 3 388-517-7627 School Three \\ Four 4 388-517-4394 School Five The '\' is doubled. Take note of the impact of the 'allowEscapes' argument: read.csv(test.csv, allowEscapes = TRUE) schidsch_name 1 331-802-7081 School One 2 464-551-7357 School Two 3 388-517-7627 School Three Four 4 388-517-4394 School Five The '\' is lost. Try it with 'allowEscapes = FALSE' explicitly. HTH, Marc Schwartz __ 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] How to show classes of all columns of a data frame?
On Tue, 2006-08-15 at 13:10 -0400, T Mu wrote: Hi all, Suppose I have a data frame myDF, col A is factor, col B is numeric, col C is character. I can get their classes by class(myDF$A) but is there a quick way to show what classes of all columns are? Thank you. Tian Depending upon the output format you desire: lapply(iris, class) $Sepal.Length [1] numeric $Sepal.Width [1] numeric $Petal.Length [1] numeric $Petal.Width [1] numeric $Species [1] factor or sapply(iris, class) Sepal.Length Sepal.Width Petal.Length Petal.Width Species numericnumericnumericnumeric factor See ?lapply and ?sapply HTH, Marc Schwartz __ 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] Making R script to run in a console
On Mon, 2006-08-14 at 14:12 -0300, Ronaldo Reis-Jr. wrote: Hi, is possible to make a R script to run under a console without open the R environment? Something like this example.R #!/usr/bin/R function(name=Put here your name) { print(name) } In a console I make ./example.R name=Ronaldo Reis Júnior then program print my name. It is possible? Thanks Ronaldo Ronaldo, You might want to review these web pages: http://wiki.r-project.org/rwiki/doku.php?id=developers:rinterp http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell HTH, Marc Schwartz __ 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] Sampling from a Matrix
On Tue, 2006-08-08 at 14:10 -0400, Liaw, Andy wrote: From: Marc Schwartz On Fri, 2006-08-04 at 12:46 -0400, Daniel Gerlanc wrote: Hello all, Consider the following problem: There is a matrix of probabilities: set.seed(1) probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), dimnames = list(1:5, letters[1:5])) probs a b c de 1 0.21 0.27 0.50 0.0148 0.303 2 0.06 0.16 0.13 0.0053 0.258 3 0.28 0.24 0.21 0.3115 0.025 4 0.53 0.19 0.73 0.2710 0.656 5 0.11 0.10 0.37 0.1960 0.205 I want to sample 3 values from each row. One way to do this follows: index - 1:ncol(probs) for(i in 1:nrow(probs)){ ## gets the indexes of the values chosen sample(index, size = 3, replace = TRUE, prob = probs[i, ]) } Is there a another way to do this? Thanks! t(apply(probs, 1, function(x) sample(x, 3))) [,1] [,2] [,3] 1 0.210 0.5000 0.0148 2 0.258 0.0053 0.1300 3 0.025 0.2800 0.3115 4 0.190 0.5300 0.2710 5 0.196 0.1000 0.1100 Hmm... If I read Daniel's code (which is different from his description) correctly, that doesn't seem to be what he wanted. Perhaps something like this: apply(probs, 1, function(p) sample(1:ncol(probs), 3, replace=TRUE, prob=p)) Andy Andy, You are of course correct. I had focused on the description of the problem, rather than the code provided, presuming that the code was not correct, including the use of 'replace' and 'prob' in sample(). I suppose it would be up to Daniel for clarification. Regards, Marc __ 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] parameter yaxs / function hist (graphics)
Paulo, Try the following: x - rnorm(100) par(xaxs = i) par(yaxs = i) hist(x, breaks = seq(-4, 4, 0.5), ylim = c(0, 40), xlim = c(-4, 4)) box() The problem is that graphics:::plot.histogram() is coded in such a way that the use of 'xaxs' and 'yaxs' are ineffectual, as they are not passed to the internal call to plot.window(), which does not provide for '...' args in this case. So they cannot be passed 'inline' as you attempted. Thus, you need to set these prior to the plotting of the histogram. BTW, a better option for the call to axis() relative to using the 'line' argument, is to use the 'pos' argument and set it to 0: x - rnorm(100) par(xaxs = i) par(yaxs = i) hist(x, breaks = seq(-4, 4, 0.5), ylim = c(0, 40), xlim = c(-4, 4), xaxt = n) axis(1, pos = 0) box() HTH, Marc Schwartz On Tue, 2006-08-08 at 18:49 -0300, Paulo Barata wrote: Dear Paulo, Thank you for your reply. But I doubt yours is a proper solution to my request, for some reasons: 1. The position of the axis graphed with the command axis(1, line=-1) depends on the size of the graphics window. 2. After your graph is on the screen, in case one may want a boxed graph, a box() command will produce a histogram floating in the air, not touching the horizontal axis. Of course, one could build a proper box (with labels, etc.) by means of more primitive graphics functions like lines (package graphics) and others, but I think that would mean a lot of work. Thank you again. Paulo Justiniano Ribeiro Jr wrote: Paulo One possibility is to draw the histogram without axes and then add them wherever you want. For instance with something along the lines: x - rnorm(500) hist(x, axes=F) axis(1, line=-1) For more details: ?axis best P.J. On Mon, 7 Aug 2006, Paulo Barata wrote: Dear R users, The parameters xaxs and yaxs (function par, package graphics) seem not to work with the function hist (package graphics), even when the parameters xlim and ylim are defined. Is there any way to make yaxs=i and xaxs=i work properly with the function hist, mainly to produce histograms that touch the horizontal axis? The R documentation and the R mailing lists archive don't seem to be of help here. I am using R 2.3.1, running under Windows XP. ## Example: x - rnorm(100) hist(x,breaks=seq(-4,4,0.5),ylim=c(0,40),yaxs=i, xlim=c(-4,4),xaxs=i) box() Thank you very much. Paulo Barata __ 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] categorical data
On Wed, 2006-08-09 at 18:07 +0200, Christian Oswald wrote: Dear List, I neeed a grouped list with two sort of categorical data. I have a data .frame like this. yearcat.b c 1 2006a1 125 212 2 2006a2 256 212 3 2005a1 14 12 4 2004a3 565 123 5 2004a2 156 789 6 2005a1 1 456 7 2003a2 786 123 8 2003a1 421 569 9 2002a2 425 245 I need a list with the sum of b and c for every year and every cat (a1, a2 or a3) in this year. I had used the tapply function to build the sum for every year or every cat. How can I combine the two grouping values? Christian, Is that what you want (using DF as your data.frame): aggregate(DF[, c(b, c)], by = list(Year = DF$year, Cat = DF$cat.), sum) Year Cat b c 1 2003 a1 421 569 2 2005 a1 15 468 3 2006 a1 125 212 4 2002 a2 425 245 5 2003 a2 786 123 6 2004 a2 156 789 7 2006 a2 256 212 8 2004 a3 565 123 You can also reorder the results by Year and Cat: DF.result - aggregate(DF[, c(b, c)], by = list(Year = DFyear, Cat = DF$cat.), sum) DF.result[order(DF.result$Year, DF.result$Cat), ] Year Cat b c 4 2002 a2 425 245 1 2003 a1 421 569 5 2003 a2 786 123 6 2004 a2 156 789 8 2004 a3 565 123 2 2005 a1 15 468 3 2006 a1 125 212 7 2006 a2 256 212 Note that tapply() can only handle one 'X' vector at a time, whereas aggregate can handle multiple 'X' columns in one call. For example: tapply(DF$b, list(DF$year, DF$cat.), sum) a1 a2 a3 2002 NA 425 NA 2003 421 786 NA 2004 NA 156 565 2005 15 NA NA 2006 125 256 NA will give you the sum of 'b' for each combination of Year and Cat within the 2d table, but I suspect this is not the output format you want. You also get NA's in the cells where there was not the given combination present in your data. HTH, Marc Schwartz __ 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] Netiquette, was Re: ... gfortran and gcc...
[Re-sending to the list only for archiving, as my original reply had too many recipients and I cancelled it.] 1. One need not be subscribed to the list to be able to post. Thus, indeed, a poster may not see all postings. 2. On the relatively rare occasion (thanks to Martin) where the server seems to incur delays in sending out posts and replies, copying the original poster on your reply ensures that they will get the reply in a timely fashion. HTH, Marc Schwartz On Tue, 2006-08-08 at 17:41 +0100, Heinz Tuechler wrote: What could be the reason, to respond not only to the list? I did not see an advantage, to receive a response twice, once directly, once by the list. Is it wrong, to assume that someone who writes to the list, does also receive all the postings on the list? Heinz At 08:09 08.08.2006 -0500, Mike wrote: Thank you both. I would prefer to communicate through the list only. Mike. On Tue August 8 2006 04:47, Prof Brian Ripley wrote: On Tue, 8 Aug 2006, Peter Dalgaard wrote: Prof Brian Ripley [EMAIL PROTECTED] writes: First, you replied to the list and not to me, which was discourteous. You mean that he replied to the list *only*, I hope. Yes, and it was written as if to me, and was a reply to an email from me. I usually consider it offensive when people reply to me and not the list (reasons including: It feels like being grabbed by the sleeve, I might not actually be the best source for the answer, and it's withholding the answer from the rest of the subscribers.) We do ask people to copy to the list. __ 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-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-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] Retain only those records from a dataframe that exist in another dataframe
On Mon, 2006-08-07 at 14:05 -0600, Mark Na wrote: Dear R community, I have two dataframes first and second which share a unique identifier. I wish to make a new dataframe third retaining only the rows in first which also occur in second. I have tried using merge but can't seem to figure it out. Any ideas? Thanks! Mark Do you want to actually join (merge) matching rows from 'first' and 'second' into 'third', or just get a subset of the rows from 'first' where there is a matching UniqueID in 'second'? In the first case: third - merge(first, second, by = UniqueID) Note that the UniqueID column is quoted. In the second case: third - subset(first, UniqueID %in% second$UniqueID) See ?merge, ?%in% and ?subset HTH, Marc Schwartz __ 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] expression() - Superscript in y-axis, keeping line break in string
On Fri, 2006-08-04 at 09:47 -0600, Andrew Kniss wrote: I've tried several different ways to accomplish this, but as yet to no avail. My y-axis for a plot has a rather long label, and thus I have been using /n to break it into two lines. However, to make it technically correct for publication, I also need to use superscript in the label. For example: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=14C-glyphosate line1\n line2) will provide the text in two lines as I would like it. However, I am trying to keep those same line breaks when using expression() to get my superscript number. This will not work, as it aligns the 14C section with the bottom line of the expression making little sense to the reader. par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=expression( ^14*C*-glyphosate line1\n line2)) Is there a way to align the 14C portion of the expression with the top line of the string rather than the bottom line? Any suggestions are greatly appreciated. Andrew plotmath, as has been covered many times previously, does not support multi-line expressions. A note should probably be added to ?plotmath on this. Thus, you need to create each line in the label separately: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab = ) # Now use mtext() to place each line of the y axis label mtext(2, text = expression( ^14*C*-glyphosate line1), line = 3) mtext(2, text = line2, line = 2) See ?mtext for more information. HTH, Marc Schwartz __ 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] expression() - Superscript in y-axis, keeping line break in string
Actually Gabor, using your solution with 'atop', which I had not considered, it will work with base graphics: par(oma = c(0, 0, 2, 0), mar = c(5, 6, 0.25, 2), lheight = 1) plot(1:10, ylab = expression(atop( ^14*C*-glyphosate line1, line2))) HTH, Marc On Fri, 2006-08-04 at 12:09 -0400, Gabor Grothendieck wrote: Sorry, you wanted a ylab=, not a main=. Try using xyplot in lattice: library(lattice) xyplot(1~1, ylab = expression(atop(phantom(0)^14*C*-glyphosate line, line2))) On 8/4/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Use atop: plot(1, main = expression(atop( ^14*C*-glyphosate line, line2))) On 8/4/06, Andrew Kniss [EMAIL PROTECTED] wrote: I've tried several different ways to accomplish this, but as yet to no avail. My y-axis for a plot has a rather long label, and thus I have been using /n to break it into two lines. However, to make it technically correct for publication, I also need to use superscript in the label. For example: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=14C-glyphosate line1\n line2) will provide the text in two lines as I would like it. However, I am trying to keep those same line breaks when using expression() to get my superscript number. This will not work, as it aligns the 14C section with the bottom line of the expression making little sense to the reader. par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=expression( ^14*C*-glyphosate line1\n line2)) Is there a way to align the 14C portion of the expression with the top line of the string rather than the bottom line? Any suggestions are greatly appreciated. Andrew __ 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] Simulate an Overdispersed(extra-variance poisson process)?
On Fri, 2006-08-04 at 10:33 -0600, Spencer Jones wrote: Is there a function in R comparable to rpois that can simulate random variables from an overdispersed poisson distribution? If there is not a function any ideas/references on how to program one? Take a look at ?rnbinom or library(MASS) ?rnegbin HTH, Marc Schwartz __ 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] Sampling from a Matrix
On Fri, 2006-08-04 at 12:46 -0400, Daniel Gerlanc wrote: Hello all, Consider the following problem: There is a matrix of probabilities: set.seed(1) probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), dimnames = list(1:5, letters[1:5])) probs a b c de 1 0.21 0.27 0.50 0.0148 0.303 2 0.06 0.16 0.13 0.0053 0.258 3 0.28 0.24 0.21 0.3115 0.025 4 0.53 0.19 0.73 0.2710 0.656 5 0.11 0.10 0.37 0.1960 0.205 I want to sample 3 values from each row. One way to do this follows: index - 1:ncol(probs) for(i in 1:nrow(probs)){ ## gets the indexes of the values chosen sample(index, size = 3, replace = TRUE, prob = probs[i, ]) } Is there a another way to do this? Thanks! t(apply(probs, 1, function(x) sample(x, 3))) [,1] [,2] [,3] 1 0.210 0.5000 0.0148 2 0.258 0.0053 0.1300 3 0.025 0.2800 0.3115 4 0.190 0.5300 0.2710 5 0.196 0.1000 0.1100 See ?apply and ?t HTH, Marc Schwartz __ 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] expression() - Superscript in y-axis, keeping line break in string
On Fri, 2006-08-04 at 19:44 +0100, Prof Brian Ripley wrote: On Fri, 4 Aug 2006, Marc Schwartz (via MN) wrote: On Fri, 2006-08-04 at 09:47 -0600, Andrew Kniss wrote: I've tried several different ways to accomplish this, but as yet to no avail. My y-axis for a plot has a rather long label, and thus I have been using /n to break it into two lines. However, to make it technically correct for publication, I also need to use superscript in the label. For example: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=14C-glyphosate line1\n line2) will provide the text in two lines as I would like it. However, I am trying to keep those same line breaks when using expression() to get my superscript number. This will not work, as it aligns the 14C section with the bottom line of the expression making little sense to the reader. par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=expression( ^14*C*-glyphosate line1\n line2)) Is there a way to align the 14C portion of the expression with the top line of the string rather than the bottom line? Any suggestions are greatly appreciated. Andrew plotmath, as has been covered many times previously, does not support multi-line expressions. A note should probably be added to ?plotmath on this. I've added a note. I think what is exact is that control chars are not interpreted ('expresssion' is an overloaded work in this context). Thanks for the nudge (and please do continue to make such remarks). Brian snip Happy to help and thanks for both noticing and taking the time to incorporate the update. Best regards, Marc __ 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] Vectorizing a for loop
On Thu, 2006-08-03 at 10:10 -0400, Daniel Gerlanc wrote: Hello all, Consider the following problem: There are two vectors: rows - c(1, 2, 3, 4, 5) columns - c(10, 11, 12, 13, 14) I want to create a matrix with dimensions length(rows) x length(columns): res - matrix(nrow = length(rows), ncol = length(columns)) If i and j are the row and column indexes respectively, the values of the cells are abs(rows[i] - columns[j]). The resultant matrix follows: [,1] [,2] [,3] [,4] [,5] [1,]9 10 11 12 13 [2,]8910 11 12 [3,]78 9 10 11 [4,]67 8 9 10 [5,]56 7 89 This matrix may be generated by using a simple for loop: for(i in 1:length(rows)){ for(j in 1:length(columns)){ res[i,j] - abs(rows[i] - columns[j]) } } Is there a quicker, vector-based approach for doing this or a function included in the recommended packages that does this? Thanks! See ?outer outer(rows, columns, function(x, y) abs(x - y)) [,1] [,2] [,3] [,4] [,5] [1,]9 10 11 12 13 [2,]89 10 11 12 [3,]789 10 11 [4,]6789 10 [5,]56789 HTH, Marc Schwartz __ 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] bullseye or polar display of circular data
On Thu, 2006-08-03 at 07:46 -0700, Michael Jerosch-Herold wrote: I have data for several rings of a left heart chamber, and which I would like to display in concentric rings, with color-encoding of the values. Each ring corresponds to one slice through the heart, and the rings correspond to positions from the base to the apex of the heart as you move from the outermost ring to the innermost one. The data have a circular pattern. These types of displays are referred to as bullseye displays in the nuclear medicine literature. Does any reader of these messages know of a R function/package that offers this functionality? Also I noticed that in some contexts you can define a circular attribute for your data. Are there plot routines for such circular data? thank you! Michael Jerosch-Herold You might want to take a look at the 'circular' or 'CircStats' packages on CRAN: http://cran.us.r-project.org/src/contrib/Descriptions/circular.html http://cran.us.r-project.org/src/contrib/Descriptions/CircStats.html There are some examples of plots generated using the packages in the R Graphics Gallery here: http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=121 http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=97 HTH, Marc Schwartz __ 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] How to access a column by its label?
On Thu, 2006-08-03 at 14:44 -0400, Neil McLeod wrote: Hi all, Is there any way to access a column of a data frame by its label (title) rather than its column index? For example, I'd like to be able to select animals[,weight] rather than animals[,3], if the third column of the animals data frame has the label weight. Thank you! You answered your own question...animals[,weight] You can also do: animals$weight or animals[[weight]] or subset(animals, select = weight) See ?Extract and ?subset for more information. HTH, Marc Schwartz __ 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] Extracting a row number from a matrix
On Tue, 2006-08-01 at 07:03 -0700, Kartik Pappu wrote: Hi all, I have a matrix with each column containing a large number of integers (0 and above). in each column beyond a certain row (say row 120 in column 1, row 134 in column 2, 142 in column 3...) there are only 0's. I want to find, for each column the row number of the last row which contains a positive integer beyond which there are 10 or more 0's. so in the following example (single column, but my real data has multiple columns) how do I get the row number of the last row of x beyond which there are 10 or more 0's (which in this case is row#100). x - as.matrix(c(rep(seq(1:20),5),rep(0,20))) I am still new to R so I was wondering if anyone had a quick fix. Thanks Kartik Not fully tested, but something like the following: x - as.matrix(c(rep(seq(1:20),5),rep(0,20))) get.zeros - function(x) { runs - rle(x == 0) pos - max(which(runs$values runs$lengths = 10)) sum(runs$lengths[1:(pos - 1)]) } apply(x, 2, get.zeros) [1] 100 See ?rle for getting information about sequences of values in a vector. HTH, Marc Schwartz __ 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] greek letters, text, and values in labels
On Tue, 2006-07-25 at 13:12 -0700, Adrian Dragulescu wrote: Hello, I want to have a title that will look something like: Results for \theta=2.1, given that I have a variable theta=2.1, and \theta should show on the screen like the greek letter. I've tried a lot of things: theta - 2.1 plot(1:10, main=expression(paste(Results for, theta, =, eval(theta or using bquote plot(1:10, main=paste(Results for , bquote(theta == .(theta or using substitute, etc. I could not make it work. This should be easy. I would appreciate your help. Thanks, Adrian Adrian, Try this: theta - 2.1 plot(1:10, main = bquote(paste(Results For: , theta == .(theta You need to surround the full expression with bquote() so that the paste()d text is within it. bquote() then returns an expression that is passed to plotmath. HTH, Marc Schwartz __ 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] unique, but keep LAST occurence
On Mon, 2006-07-24 at 12:00 -0500, [EMAIL PROTECTED] wrote: ?unique says Value: An object of the same type of 'x'. but if an element is equal to one with a smaller index, it is removed. However, I need to keep the one with the LARGEST index. Can someone please show me the light? I thought about reversing the row order twice, but I couldn't get it to work right (My data frame has 125000 rows and 7 columns, and I'm 'uniqueing' on column #1 (chron) only, although the class of the column may not matter.) Say, e.g., DF - data.frame(t = c(1,2,3,1,4,5,1,2,3), x = c(0,1,2,3,4,5,6,7,8)) I would like the result to be (sorted as well) t x 1 6 2 7 3 8 4 4 5 5 If I got the original rownames, that would be a bonus (for debugging.) Does this get it? DF[sapply(unique(DF$t), function(x) max(which(DF$t == x))), ] t x 7 1 6 8 2 7 9 3 8 5 4 4 6 5 5 HTH, Marc Schwartz __ 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] Reading data with blank elements
On Fri, 2006-07-21 at 05:43 -0700, Ahamarshan jn wrote: Hi, I have a dataset saved in *.csv format, that contains 13 columns (the first column being the title name and the rest experiments) and about 2500 rows. Not all columns in the row have data in it i.e for eg BS00,-0.084,0.0136,-0.1569,-0.6484,1.103,1.7859,0.40287,0.5368,0.08461,-0.1935,-0.147974,0.30685 BS01,0.491270283,0.875826172,, BS02,0.090794476,0.225858954,,,0.32643,0.34317,0.133145295,,,0.115832599,0.47636458, BS03,0.019828221,-0.095735935,-0.122767219,-0.0676,0.002533,-0.1510361,0.736247,2.053192,-0.423658,0.4591219,1.1245015, BS04,-0.435189342,-0.041595955,-0.781281128,-1.923036,-3.2301671020.152322609,-1.495513519,, I am using R to perform a correlation, but I am getting an error while trying to read the data as person.data-read.table(datafile.csv,header=TRUE,sep=',',row.names=1) Error in scan (file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1919 did not have 13 elements Execution halted The error looks as though there is a problem with the last element being not read when it is blank. I could introduce terms like na to the blank elements but I donot want to do that because this will hinder my future analysis. Can some one suggest me a solution to overcome this problem while reading the data? , or is there something that I have missed to make the data readable. Thank you in advance, PS: The data was imported from a experiment and saved in excel sheet as a *.csv and then used. You have already had other replies, to which I would add, be sure to read Chapter 8 in the R Import/Export Manual regarding importing Excel files and other options besides exporting to a CSV file. In addition, the issue of Excel generating CSV files with the last column missing on some rows is a known issue and is reported in the MSKB here: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q77295 Even though the latest version of Excel listed in the article as being relevant is 97, I had this problem with 2000 and 2003 as well. I would instead use OpenOffice.org's Calc to do the export when this was required. Calc did not have this problem. HTH, Marc Schwartz __ 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] connection to X11 problem
On Fri, 2006-07-21 at 11:47 -0700, Paquet, Agnes wrote: Dear List, I am a new Mac user and I am having problem generating png (or jpeg) using the GUI version of R. I installed R-2.3.1.dmg (custom install with everything selected) and X11User.pkg but I am still getting the following X11 connection error when I try to generate a png (or a jpeg): Error in X11(paste(png::, filename, sep = ), width, height, pointsize, : unable to start device PNG In addition: Warning message: unable to open connection to X11 display '' I tried to set up the DISPLAY variable using the command: Sys.putenv(DISPLAY=:0) but I am still running into the same problem. Is there anything else I need to do or install in order to use X11? I am using a intel Core Duo processor and OSX 10.4.7. Thank you for your help, Agnes I don't use a Mac, but the following might be helpful: http://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html#X11-window-server-_0028optional_0029 http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77424.html http://finzi.psych.upenn.edu/R/Rhelp02a/archive/46097.html Also note that there is a R-sig-Mac e-mail list: https://stat.ethz.ch/mailman/listinfo/r-sig-mac HTH, Marc Schwartz __ 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] character to vector
On Fri, 2006-07-21 at 15:15 -0500, [EMAIL PROTECTED] wrote: I have an object of mode character that contains a long sequence of letters. How can I convert this object into a vector with each element of the vector containing a single letter? Essentially, I want to break the long string of letters into many individual letters. Thanks for the help. Alex letters [1] a b c d e f g h i j k l m n o p q [18] r s t u v w x y z MyChar - paste(letters, collapse = ) MyChar [1] abcdefghijklmnopqrstuvwxyz MyVec - unlist(strsplit(MyChar, )) MyVec [1] a b c d e f g h i j k l m n o p q [18] r s t u v w x y z See ?strsplit and ?unlist and of course, ?paste for the reverse operation as above. HTH, Marc Schwartz __ 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] R-help in a newsgroup
On Tue, 2006-07-18 at 11:30 -0700, Darren Weber wrote: Hi, I find a lot of the R-help email traffic overloads my inbox. My IT managers are not really happy for me to be subscribed to several high-traffic email lists. I don't want to lose my contact with the R-help emails, so I'm having to consider various ways of handling the traffic. Anyhow, I'm wondering how many people on the R-help email list would prefer that most of the traffic were in a newsgroup? In case your interested in that option, there is a group available at: [EMAIL PROTECTED] I think the subscription is through normal news group channels. The google search services on this group are nice too. This group is not divided into useful categories, like help, admin, develop etc., but it's not too difficult to create new groups for that. Best, Darren r-help is already available with an NNTP interface at gmane.org: http://dir.gmane.org/gmane.comp.lang.r.general There is also a web based interface, where you can see that your post is already available: http://news.gmane.org/gmane.comp.lang.r.general Similarly, r-devel is also present: http://dir.gmane.org/gmane.comp.lang.r.devel The Google group you reference is completely independent of the R e-mail lists, whereas the gmane interface is synchronized with the R e-mail lists. HTH, Marc Schwartz __ 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] String manipulation and formatting
On Mon, 2006-07-17 at 16:07 +0200, Bashir Saghir (Aztek Global) wrote: I'm trying to write a simple function that does the following: [command] xify(5.2) [output] XXX.XX [command] xify(3) [output] XXX Any simple solutions (without using python/perl/unix script/...)? Thanks, Saghir Here are two variations: xify - function(x) { exxes - as.numeric(unlist(strsplit(as.character(x), \\.))) ifelse(length(exxes) == 2, paste(paste(rep(X, exxes[1] - exxes[2]), collapse = ), paste(rep(X, exxes[2]), collapse = ), sep = .), paste(rep(X, exxes[1]), collapse = )) } xify - function(x) { exxes - as.numeric(unlist(strsplit(as.character(x), \\.))) tmp - sapply(exxes, function(x) paste(rep(X, x), collapse = )) ifelse(length(tmp) == 2, paste(substr(tmp[1], 1, exxes[1] - exxes[2]), tmp[2], sep = .), tmp) } HTH, Marc Schwartz __ 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] Generate object names from variables
On Fri, 2006-07-14 at 16:57 +0200, Georg Otto wrote: Hi, I want to generate object names out of variables in a sort of variable substitution. first i generate some vectors and an empty list: vector.a-c(a,b) vector.b-c(c,d) vector.c-c(e,f) vectors-c(vector.a, vector.b, vector.c) vectors [1] vector.a vector.b vector.c vectorlist-list() What I would then like to do is to generate elements of the list by using variables, somehow like this (does not work): for (i in vectors) { + list$i-i + } To end up with a list like this: list $vector.a [1] a b $vector.b [1] c d $vector.c [1] e f Any hint will be appreciated. Cheers, Georg Presuming that your vectors fit a naming pattern of vector.x: # Use grep() to get the vector names from ls() vectors - grep(vector[\.], ls(), value = TRUE) vectors [1] vector.a vector.b vector.c # Use sapply to create the list sapply(vectors, get, simplify = FALSE) $vector.a [1] a b $vector.b [1] c d $vector.c [1] e f HTH, Marc Schwartz __ 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] SAS to R translator for particular procedures
On Fri, 2006-07-14 at 18:35 +0200, Mehmet Somel wrote: Dear /Bill Paterson, while trying to find a way to convert SAS code into R, I came across your one time e-mail message (http://www.ens.gu.edu.au/robertk/R/help/99b/0908.html). I'd appreciate to learn if anything came out of this, or any suggestions. Thank you in advance, Mehmet Somel / There was this post back in 2004: https://stat.ethz.ch/pipermail/r-help/2004-April/048750.html HTH, Marc Schwartz N.B. Pay attention to the date... :-) __ 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] Keep value lables with data frame manipulation
On Wed, 2006-07-12 at 17:41 +0100, Jol, Arne wrote: Dear R, I import data from spss into a R data.frame. On this rawdata I do some data processing (selection of observations, normalization, recoding of variables etc..). The result is stored in a new data.frame, however, in this new data.frame the value labels are lost. Example of what I do in code: # read raw data from spss rawdata - read.spss(./data/T50937.SAV, use.value.labels=FALSE,to.data.frame=TRUE) # select the observations that we need diarydata - rawdata[rawdata$D22==2 | rawdata$D22==3 | rawdata$D22==17 | rawdata$D22==18 | rawdata$D22==20 | rawdata$D22==22 | rawdata$D22==24 | rawdata$D22==33,] The result is that rawdata$D22 has value labels and that diarydata$D22 is numeric without value labels. Question: How can I prevent this from happening? Thanks in advance! Groeten, Arne Two things: 1. With respect to your subsetting, your lengthy code can be replaced with the following: diarydata - subset(rawdata, D22 %in% c(2, 3, 17, 18, 20, 22, 24, 33)) See ?subset and ?%in% for more information. 2. With respect to keeping the label related attributes, the 'value.labels' attribute and the 'variable.labels' attribute will not by default survive the use of [.data.frame in R (see ?Extract and ?[.data.frame). On the other hand, based upon my review of ?read.spss, the SPSS value labels should be converted to the factor levels of the respective columns when 'use.value.labels = TRUE' and these would survive a subsetting. If you want to consider a solution to the attribute subsetting issue, you might want to review the following post by Gabor Grothendieck in May, which provides a possible solution: https://stat.ethz.ch/pipermail/r-help/2006-May/106308.html and this post by me, for an explanation of what is happening in Gabor's solution: https://stat.ethz.ch/pipermail/r-help/2006-May/106351.html HTH, Marc Schwartz __ 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] replace values in data frame
On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote: Hi all, I have a three columned list that I have imported into R. The first column is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the third a numeric value. I want to replace some of the second column names with other names (for example replace ACERRUB with ACERDRU). The original and replacement values are in separate lists (not vectors), but I can't seem to find the right function to perform this. The replace function seems to only want to work with numbers. Any clues? Wade Without seeing the code you are using, we can only guess a syntax error of some sort. It works fine using the iris dataset: head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 7 4.6 3.4 1.4 0.3 setosa 8 5.0 3.4 1.5 0.2 setosa 9 4.4 2.9 1.4 0.2 setosa 10 4.9 3.1 1.5 0.1 setosa iris$Species - replace(iris$Species, iris$Species == setosa, NewValue) head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 NewValue 2 4.9 3.0 1.4 0.2 NewValue 3 4.7 3.2 1.3 0.2 NewValue 4 4.6 3.1 1.5 0.2 NewValue 5 5.0 3.6 1.4 0.2 NewValue 6 5.4 3.9 1.7 0.4 NewValue 7 4.6 3.4 1.4 0.3 NewValue 8 5.0 3.4 1.5 0.2 NewValue 9 4.4 2.9 1.4 0.2 NewValue 10 4.9 3.1 1.5 0.1 NewValue HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to change the type of segments ends?
On Fri, 2006-07-07 at 11:47 -0400, Lu, Jiang Jane wrote: Hi, I am trying to plot odds ratios and the corresponding confidence intervals in horizontal segments. It would be ideal if the confidence interval segment can be drawn with little vertical bars at both ends. I have tried very hard to change the type of ends by using 'lend' arguments, but cannot make it. I even tried 'arrows()', but still failed. Following is the code I use: drug.or - c(1.017,1.437,1.427,2.211) drug.orl - c(0.715,1.075,1.103,1.696) drug.oru - c(1.446,1.922,1.845,2.882) yaxis - seq(1,4,by=1) plot(x=drug.or,y=yaxis,type='p',pch=17,xlim=c(0,3),axes=FALSE, xlab='Odds Ratio',ylab='',main='Reference Group: A only') axis(1,at=seq(0,3,by=0.5),labels=paste(seq(0,3,by=0.5))) axis(2,at=yaxis,las=2) segments(x0=drug.orl,x1=drug.oru,y0=yaxis,y1=yaxis,col=4,lend=2) # or try #arrows(x0=drug.orl,x1=drug.oru,y0=yaxis,y1=yaxis,length=0.1,angle=0,cod e=3,col=4,lend=2) box() = Try this using arrows(): drug.or - c(1.017,1.437,1.427,2.211) drug.orl - c(0.715,1.075,1.103,1.696) drug.oru - c(1.446,1.922,1.845,2.882) yaxis - seq(1, 4, by=1) plot(drug.or, yaxis ,type='p', pch=17, xlim=c(0,3), axes=FALSE, xlab='Odds Ratio', ylab='', main='Reference Group: A only') axis(1, at=seq(0,3,by=0.5), labels=paste(seq(0,3,by=0.5))) axis(2, at=yaxis, las=2) arrows(drug.orl, yaxis, drug.oru, yaxis, angle=90, length=0.1, code=3, col=4, lend=2) You need to specify an angle of 90 degrees to get the arrow heads to be perpendicular to the primary line segment. An angle of 0 superimposes the heads on the primary line segment, so they don't show. HTH, Marc Schwartz __ 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] replace values in data frame
Thanks for re-posting onlist. My offlist reply: The 'list' argument, as per ?replace, needs to be a vector of one or more indices into another vector, generally the source vector argument 'x', not the actual values that you want to replace. 'list' can either be explicit integers as the indices, or the result of a logical operation as I used in my example. In addition, the objects 'list' and 'replace' in your code are not vectors, but they are data frames. That is the default type of object created when using read.table(). You can use str(list) and str(replace) to check this. The str() function returns the structure of an object. It is likely that you really want list$V1 and replace$V1 as the vectors of interest here. V1 thru Vx are the default column names created when using read.table() if no header row exists in the incoming file. In addition, note that replace() is not vectorized. It will not handle multiple search and replace arguments in a single call. See my example using the iris dataset. You would need to do each one separately. You can create a loop of one type or another to cycle through multiple arguments if you wish, where each cycle through the loop is a single call to replace() with a new set of values as appropriate for the arguments. Finally, you should generally not use R object or function names for user created objects. Both 'list' and 'replace' are such objects. R will generally be able to differentiate, but there is no guarantee and it will help you avoid code debugging headaches. HTH, Marc Schwartz On Fri, 2006-07-07 at 12:58 -0400, Wade Wall wrote: The format is like this. Plot species Value P1 ACERRUB 3 P2 MAGNVIR2 P3 ARONARB 2 etc. imported using x-read.table(file=filename.txt) I want to replace a list of values in the 2nd column with another list. For example, I want to replace ARONARB with PHOTPYR. list-read.table(file=originalnames.txt) replace-read.table(file=replacementnames.txt) I tried to use replace in this manner: newx-replace(x,list,replace) however, I get the error message: error in replace, invalid subscript type. I have tried transforming the above list and modifying the column names (column 2), but to no avail. I hope this clarifies a little. Sorry about that. On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote: Hi all, I have a three columned list that I have imported into R. The first column is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the third a numeric value. I want to replace some of the second column names with other names (for example replace ACERRUB with ACERDRU). The original and replacement values are in separate lists (not vectors), but I can't seem to find the right function to perform this. The replace function seems to only want to work with numbers. Any clues? Wade __ 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] replace values in data frame
Wade, Given that you appear to have multiple search and replace items to deal with, here is a possible loop based Global Search and Replace solution: gsr - function(Source, Search, Replace) { if (length(Search) != length(Replace)) stop(Search and Replace Must Have Equal Number of Items\n) Changed - as.character(Source) for (i in 1:length(Search)) { cat(Replacing: , Search[i], With: , Replace[i], \n) Changed - replace(Changed, Changed == Search[i], Replace[i]) } cat(\n) Changed } Source: The source vector, which will be coerced to character Search: The Search values to be matched in Source as a character vector Replace: The values that will replace those found in 'Search' on a one-for-one basis. This function returns a character vector. As with replace(), the result must be assigned. The change is not done 'in place'. The function will also output the search and replace values to the console during the loop. Again, using the iris dataset as an example: iris$Species [1] setosa setosa setosa setosa setosa setosa [7] setosa setosa setosa setosa setosa setosa [13] setosa setosa setosa setosa setosa setosa [19] setosa setosa setosa setosa setosa setosa [25] setosa setosa setosa setosa setosa setosa [31] setosa setosa setosa setosa setosa setosa [37] setosa setosa setosa setosa setosa setosa [43] setosa setosa setosa setosa setosa setosa [49] setosa setosa versicolor versicolor versicolor versicolor [55] versicolor versicolor versicolor versicolor versicolor versicolor [61] versicolor versicolor versicolor versicolor versicolor versicolor [67] versicolor versicolor versicolor versicolor versicolor versicolor [73] versicolor versicolor versicolor versicolor versicolor versicolor [79] versicolor versicolor versicolor versicolor versicolor versicolor [85] versicolor versicolor versicolor versicolor versicolor versicolor [91] versicolor versicolor versicolor versicolor versicolor versicolor [97] versicolor versicolor versicolor versicolor virginica virginica [103] virginica virginica virginica virginica virginica virginica [109] virginica virginica virginica virginica virginica virginica [115] virginica virginica virginica virginica virginica virginica [121] virginica virginica virginica virginica virginica virginica [127] virginica virginica virginica virginica virginica virginica [133] virginica virginica virginica virginica virginica virginica [139] virginica virginica virginica virginica virginica virginica [145] virginica virginica virginica virginica virginica virginica Levels: setosa versicolor virginica Note that iris$Species is a factor with specific levels. The gsr() function above will coerce the Source argument to a character vector internally and return a character vector: iris$Species - gsr(iris$Species, c(setosa, versicolor, virginica), c(s1, v1, v2)) Replacing: setosa With: s1 Replacing: versicolor With: v1 Replacing: virginica With: v2 iris$Species [1] s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 [14] s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 [27] s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 [40] s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 v1 v1 [53] v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 [66] v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 [79] v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 [92] v1 v1 v1 v1 v1 v1 v1 v1 v1 v2 v2 v2 v2 [105] v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 [118] v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 [131] v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 v2 [144] v2 v2 v2 v2 v2 v2 v2 HTH, Marc Schwartz __ 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] probably need to se sapply but i can't get it
On Mon, 2006-06-26 at 12:40 -0500, [EMAIL PROTECTED] wrote: Hi : I think I need to use sapply but I can't figure this out. Suppose I have two vectors : tempa ( 4, 6,10 ) and tempb ( 11,23 ,39 ) I want a function that returns 4:11,6:23 and 10:39 as vectors. I tried : sapply(1:length(tempa) function (z) seq(tempa[z],tempb[z]) but i got 3 really strange vectors back in the sense that the numbers in them did not make no sense to me. obviously, i must be doing something wrong. thanks a lot. mark Mark, Try this using mapply(): tempa - c(4, 6, 10) tempb - c(11, 23, 39) mapply(seq, from = tempa, to = tempb) [[1]] [1] 4 5 6 7 8 9 10 11 [[2]] [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [[3]] [1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [23] 32 33 34 35 36 37 38 39 You will get a list back in this case and you can then deal with the 3 vectors as you require. Each vector is a different length, so a list is about the only way to return them here. See ?mapply for more info. HTH, Marc Schwartz __ 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] columnwise multiplication?
On Fri, 2006-06-23 at 11:53 -0400, Mu Tian wrote: Hi all, I'd like to do a multiplication between 2 matrices buy only want resulsts of cloumn 1 * column 1, column 2 * column 2 and so on. Now I do C - diag(t(A) %*% B) Is there a bulit in way to do this? Thank you. You just want: A * B for the initial multiplication. This technically gives you element by element multiplication. Since matrices are vectors with a dim attribute and elements stored by default in column order (top to bottom, then left to right), the result matrix will yield what you require on a column by column basis. For example: A - matrix(1:12, ncol = 3) B - matrix(1:12, ncol = 3) A [,1] [,2] [,3] [1,]159 [2,]26 10 [3,]37 11 [4,]48 12 B [,1] [,2] [,3] [1,]159 [2,]26 10 [3,]37 11 [4,]48 12 A * B [,1] [,2] [,3] [1,]1 25 81 [2,]4 36 100 [3,]9 49 121 [4,] 16 64 144 To then get cumulative column totals, you can do: colSums(A * B) [1] 30 174 446 HTH, Marc Schwartz __ 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] PowerPoint
Has anyone tried this with OO.org's Impress or Writer on Windows to see if the same behavior occurs? My recollection from prior experience on Windows (it's been a while) is that a subtle resize takes place when pasting/importing graphics into the aforementioned apps. You can right click on the graphic in the app and then select Original Size or something worded similarly on the graphic object formatting dialog window. Not sure if that is enough to get the lines back or if one has to go slightly larger than the original size to resolve the issue. It also seems to me that there was some behavior on the R Windows graphic device relative to re-sizing the plot region and then doing the metafile copy and paste, but it has been long enough that my memory may not be intact (which my wife would suggest anyway ;-). HTH, Marc Schwartz On Fri, 2006-06-23 at 08:08 -0700, Berton Gunter wrote: I've always assumed that this was a rendering problem in the MS application, as the reappearance of the missing lines on re-sizing shows that that the necessary information **is** in the imported .wmf file, right? -- Bert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Sundar Dorai-Raj Sent: Friday, June 23, 2006 7:55 AM To: Johannes Ranke Cc: r-help@stat.math.ethz.ch; Marc Bernard Subject: Re: [R] PowerPoint Hi, all, (Sorry to highjack the thread, but I think the OP should also know this) One of the plots Marc mentions is xyplot. Has anybody else on this list had a problem with lattice and win.metafile (or Ctrl-W in the R graphics device)? I will sometimes import wmf files (or Ctrl-V) with lattice graphics into powerpoint and notice some of the border lines are missing. I can re-size the plot to make the lines reappear but have to find just the right size to make it look right. This seems to be a problem with PPT, XLS, and Word. I never have this problem with traditional graphics (e.g. plot.default, etc.). I'm using Windows XP Pro with R-2.3.1 and lattice-0.13.8, though I've also experienced the problem on earlier versions of R and earlier versions of lattice. Thanks, --sundar Johannes Ranke wrote: Dear Bernard, if you use MS Powerpoint, it seems likely to me that you are using the Windows version of R. Are you aware of the fact, that you can just right-click on any graph and copy it to the clipboard (copy as metafile or similar). That way you get a vectorized version of the graph, which you can nicely paste into Powerpoint and edit. Johannes * Marc Bernard [EMAIL PROTECTED] [060623 13:40]: Dear All, I am looking for the best way to use graphs from R (like xyplot, curve ...) for a presentation with powerpoint. I used to save my plot as pdf and after to copy them as image in powerpoint but the quality is not optimal by so doing. Another completely independent question is the following: when I use main in the xyplot, the main title is very close to my plot, i.e. no ligne separate the main and the plot. I would like my title to be well distinguished from the plots. I would be grateful for any improvements... Many thanks, Bernard, __ 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] PowerPoint
On Fri, 2006-06-23 at 18:16 +0200, Philipp Pagel wrote: On Fri, Jun 23, 2006 at 09:21:37AM -0400, Gabor Grothendieck wrote: Note that jpg, bmp and png are in less desirable bit mapped formats whereas eps is in a more desirable vector format (magnification and shrinking does not involve loss of info) and so would be preferable from a quality viewpoint. In addition to seconding the above statement I'd like to add that in cases where you are forced to use a bitmap format png tends to produce much better results than jpg where line drawings (e.g. most plots) are concerned. JPG format on the other hand is great for anyting which can be discirbed as photography-like. jpg images of plots tend to suffer from bad artifacts... cu Philipp That is generally because png files are not compressed, whereas jpg files are. The compression algorithms that are typically used are lossy, which means that you give up image quality in order to gain the reduction in file size. The greater the compression you use, the greater the loss in image quality. Yet another reason to use EPS for plots. HTH, Marc Schwartz __ 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] Producing png plot in batch mode
On Fri, 2006-06-23 at 17:23 +0100, Vittorio wrote: I have set up an R procedure that is launched every three hours by crontab in a unix server. Crontab runs at regular intervals the following line: R CMD BATH myprog.R myprog.R (which by the way uses R2HTML) should create an updated png graph to be referred to and seen in an intranet web-page index.html. The problem is that both: png () plot(...) dev.off() AND: plot(...) HTMLplot(...) fail when launched in a batch manner compalining that they need an X11() instance to be used (I understand that they work only in a graphic context and intarictively). How can I obtain that png file? See R FAQ 7.19 How do I produce PNG graphics in batch mode? HTH, Marc Schwartz __ 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] PowerPoint - eps not suitable
On Fri, 2006-06-23 at 14:02 -0400, Michael H. Prager wrote: Previous posters have argued for EPS files as a desirable transfer format for quality reasons. This is of course true when the output is through a Postscript device. However, the original poster is making presentations with PowerPoint. Those essentially are projected from the screen -- and screens of Windows PCs are NOT Postscript devices. The version of PowerPoint I have will display a bitmapped, low-resolution preview when EPS is imported, and that is what will be projected. It is passable, but much better can be done! In this application, I have had best results using cut and paste or the Windows metafile format, both of which (as others have said) give scalable vector graphics. When quirks of Windows metafile arise (as they can do, especially when fonts differ between PCs), I have had good results with PNG for line art and JPG for other art. Mike Just so that it is covered (though this has been noted in other threads), even in this situation, one can still use EPS files embedded in PowerPoint (or Impress) presentations. The scenario is to print out the PowerPoint presentation to a Postscript file (using a PS printer driver). If you have Ghostscript installed, you can then use ps2pdf to convert the PS file to a PDF file. If you have OO.org, there is a Distiller type of printer driver called PDF Converter (configured via the printer admin program) available, which you can use to go directly to a PDF in a single step. This also uses Ghostscript (-sDEVICE=pdfwrite) as an intermediary (though hidden from the user) step. The standard OO.org PDF export mechanism (using the toolbar icon) only exports the bitmapped preview, not the native EPS image. This is what you see as the preview image in these Office type of apps by default. Most PDF file viewers (Acrobat, xpdf, Evince, etc.) have a full screen mode, whereby you can the use the viewer to display the presentation in a landscape orientation to an audience. I have done this frequently (under Linux with OO.org) to facilitate presentations, when for any number of reasons, using LaTeX (ie. Beamer) was not practical. Even when using Beamer, the net result is still the same: creating a PDF file via pdflatex, which is then displayed landscape in a PDF rendering application full screen. This was the typical mode of operation at last week's useR! meeting in Vienna. All that being said, the ultimate test is in the eye of the user. So whatever gives you sufficient quality for your application with minimal hassle is the way to go. HTH, Marc Schwartz __ 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] useR! Thanks
On Thu, 2006-06-22 at 15:40 -0500, Jeffrey Horner wrote: Achim Zeileis wrote: [...] The plan is to provide the original presentation slides of the panelists and a video of the whole panel disc, and probably some minutes and/or further information. Did you happen to video Ivan Mizera's talk as well? I'd really like to see that again! Hear! Hear! A most enjoyable presentation! I now suffer from one newly engendered fear: Becoming an abuseR! ;-) Thanks to the Organizers, Hosts and Presenters for another wonderful meeting. It is great to see the continued healthy growth and diversity in this community. I look forward to the next one. Best regards, Marc Schwartz __ 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] help on ploting various lines
On Wed, 2006-06-21 at 12:22 -0400, Baoqiang Cao wrote: Dear All, I tried to plot a variety of lines(curves) on same figure. What I did is, plot(x=x1,y=y1) lines(x=x2,y=y2) lines(x=x3,y=y3) ... In my data, the maximum of y1 is much smaller than those maximums of other y vectors. So, in the figure I got, there are some curves which are not complete, I mean, they were cut off at the maximum of y1 at the y axis. Could anybody point out some right commands I need use? Thanks! Best, Cao You will want to use the 'xlim' and 'ylim' arguments in plot() to set the initial axis ranges for the scatter plot based upon the ranges of the combined x* or y* vectors. That way, the plot region ranges are set to include all of your values. x.range - range(x1, x2, x3) y.range - range(y1, y2, y3) plot(x1, y1, xlim = x.range, ylim = y.range) lines(x2, y2) lines(x3, y3) See ?plot.default for more information. HTH, Marc Schwartz __ 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] help on ploting various lines
On Wed, 2006-06-21 at 11:55 -0500, Marc Schwartz (via MN) wrote: On Wed, 2006-06-21 at 12:22 -0400, Baoqiang Cao wrote: Dear All, I tried to plot a variety of lines(curves) on same figure. What I did is, plot(x=x1,y=y1) lines(x=x2,y=y2) lines(x=x3,y=y3) ... In my data, the maximum of y1 is much smaller than those maximums of other y vectors. So, in the figure I got, there are some curves which are not complete, I mean, they were cut off at the maximum of y1 at the y axis. Could anybody point out some right commands I need use? Thanks! Best, Cao You will want to use the 'xlim' and 'ylim' arguments in plot() to set the initial axis ranges for the scatter plot based upon the ranges of the combined x* or y* vectors. That way, the plot region ranges are set to include all of your values. x.range - range(x1, x2, x3) y.range - range(y1, y2, y3) plot(x1, y1, xlim = x.range, ylim = y.range) lines(x2, y2) lines(x3, y3) See ?plot.default for more information. One other note, which is to review: ?matplot which also has help for matpoints() and matlines(), which will do the common axis range adjustments for you. This will be helpful if you are going to be plotting a larger number of points/lines in a single graphic. HTH, Marc __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How add degree character in axis?
On Fri, 2006-06-09 at 11:01 +, fernando espindola wrote: Hi user R, I am try to put degree character in axis x, but don't make this. I have the next code: plot(mot[,5], time1,xlim=c(-45,-10),type=l,yaxt=n,ylab=,col=1,lwd=2,xlab=,xaxt=n) The range of value in axis-x is -45 to -10, this values represents the longitudes positions in space. I try to put 45°S for real value (-45) in the axis-x, and for all elements . Anybody can give an advance in this problems. Thank for all In general, as Roger noted, see ?plotmath for adding mathematical annotation to R plots. To do the degree symbol is relatively straightforward, but adding the S takes a bit of a trick: # Create vector of x values at - seq(-45, -10, 5) # Create a mock plot plot(at, 1:8, xlim = range(at), xaxt = n) # Now create a set of plotmath expressions, one for # each value of 'at' using paste() and parse() # The general format for the degrees symbol is x*degree # However, to add the S we use the ~ to create # a right and left hand side for the expression and # place the S after it. The ~ will not print. L - parse(text = paste(at, *degree ~ S, sep = )) # Now do the x axis axis(1, at = at, labels = L) HTH, Marc Schwartz __ 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] Rotate numbers on the Y axis on a multiple Boxplot chart
On Thu, 2006-06-08 at 15:44 +0100, d d [EMAIL PROTECTED] ac uk wrote: Hello All, I am trying to format a box plot chart for a report so it matches other charts I have created in other software programs. My problem is that I need to rotate the values on the Y axis by 45 degrees, I have tried to use the srt parameter but with no luck? Does anybody have any suggestions? Regards, Daniel My syntax is as follows: boxplot(int~sortf,notch=TRUE,outline=FALSE,col=DarkSeaGreen4, ylab = Length of stay in days,ylim=c(0, 100),xlab=Trust (see key)) See R FAQ 7.27 How can I create rotated axis labels? The example there is for the x axis, but the same concept applies for y. HTH, Marc Schwartz __ 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] multiple data sets on one plot
On Wed, 2006-06-07 at 10:30 -0500, Mark L Sessing wrote: Hello, I am learning how to use R, and I cannot figure out how to plot more than one data set on a single plot. Can you help me out? Cheers, Mark It depends upon the type of plot (scatter, lines, bar, etc.) and whether or not you are using R's base graphics or lattice graphics. With more information, we can offer specific guidance. For example, with base graphics, you can add additional plot components with: ?lines ?points ?segments ?matpoints (Note also matplot() on the same page) ?curve ?arrows A good starting place would be Chapter 12 Graphical Procedures in An Introduction to R, which is available within your R installation (on Windows from the GUI menus) or from the Documentation links on the R home page. An additional resource is the R Graph Gallery: http://addictedtor.free.fr/graphiques/index.php and Chapter 3 From Data to Graphics by Vincent Zoonekynd here: http://zoonek2.free.fr/UNIX/48_R/all.html HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] how to do multiple comparison in the nonparametric statistical analysis?
On Thu, 2006-06-08 at 00:10 +0800, zhijie zhang wrote: Dear Rusers, As we all know , there are many methods to do multiple comparison in the parametric statistical analysis, But i can't find some in nonparametric statistical analysis. Could anybody give some suggestions? Have you looked at the npmc package on CRAN? As a text reference, there is also: Multiple Comparisons: Theory and methods by Jason C. Hsu Chapman Hall 1996 More information here: http://www.stat.ohio-state.edu/~jch/mc.html Amazon.com link: http://www.amazon.com/gp/product/0412982811 HTH, Marc Schwartz __ 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] Strange behaviour of cbind
On Tue, 2006-06-06 at 13:44 -0700, Dirk Vandekerckhove wrote: Hi, Is this intended behaviour of cbind? a-c(0,1,2,3) a [1] 0 1 2 3 a-as.ordered(a) a [1] 0 1 2 3 Levels: 0 1 2 3 a-a[a!=0] #remove the zero from a a [1] 1 2 3 Levels: 0 1 2 3 cbind(a) a [1,] 2 [2,] 3 [3,] 4 #cbind adds +1 to each element Does this help? a [1] 1 2 3 Levels: 0 1 2 3 as.integer(a) [1] 2 3 4 Note in ?cbind, the Details section indicates: In the default method, all the vectors/matrices must be atomic (see vector) or lists (e.g., not expressions). For a factor, the atomic data type is the underlying integer vector. You eliminated '0' from the original ordered factor, which had an integer value of 1 (not 0!): a [1] 0 1 2 3 Levels: 0 1 2 3 as.integer(a) [1] 1 2 3 4 Unless you re-level the factor (as you do below) the other elements retain the original integer values. a-as.ordered(as.vector(a)) a [1] 1 2 3 Levels: 1 2 3 cbind(a) a [1,] 1 [2,] 2 [3,] 3 #now it works... Yep, you re-leveled 'a', so the integer values now correspond to the levels: a-as.ordered(as.vector(a)) a [1] 1 2 3 Levels: 1 2 3 as.integer(a) [1] 1 2 3 HTH, Marc Schwartz __ 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] simple index question
On Thu, 2006-06-01 at 11:06 -0400, Mu Tian wrote: I want to get index number of 0s, like x - c(1, 2, 0, 3, 0, 4) I want an output of 3, 5 Thank you. See ?which which(x == 0) [1] 3 5 HTH, Marc Schwartz __ 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] merging
On Tue, 2006-05-30 at 19:08 +0100, Gavin Simpson wrote: Dear List, Given, y - matrix(c(0,1,1,1,0,0,0,4,4), ncol = 3, byrow = TRUE) rownames(y) - c(a,b,c) colnames(y) - c(1,2,3) y y2 - y[2:3, ] rownames(y2) - c(x,z) y2 how can I stop merge(y, y2, all = TRUE, sort = FALSE) squishing the extra rows? Ideally I want the same as: rbind(y, y2) in this case. This is specific example of situation where two data matrices have same column variables and all I want is to stick the two sets of rows together, but I have been using merge for cases such as the one below, where the second matrix has extra column(s): y3 - matrix(c(0,1,1,1,0,0,0,4,4,5,6,7), ncol = 4, byrow = TRUE) rownames(y3) - c(d,e,f) colnames(y3) - c(1,2,3,4) y3 merge(y, y3, all = TRUE, sort = FALSE) We don't know before hand if the columns will match. But I see now that even this doesn't work as I was expecting/thinking! So I'm looking for a general way to merge two matrices such that the number of rows in the merged matrix is nrow(mat1) + nrow(mat2) and the number of columns in the merged matrix is length(unique(colnames(mat1), colnames(mat2). Is there a function in R to do this, or can someone suggest a way to achieve this? My R version info is at the end. Just to be clear, for the y, y3 example I want something like this returned: 1 2 3 4 a 0 1 1 NA b 1 0 0 NA c 0 4 4 NA d 0 1 1 1 e 0 0 0 4 f 4 5 6 7 and for the y, y2 example, I want something like this returned: 1 2 3 a 0 1 1 b 1 0 0 c 0 4 4 x 1 0 0 z 0 4 4 Gavin, Here is a possible solution, though not fully tested. It uses the row.names for the two matrices as part of the 'by' matching process. This is noted in the Details section in ?merge. So for y and y2: res - merge(y, y2, by = c(row.names, intersect(colnames(y), colnames(y2))), all = TRUE) # Note that the row names are now the first col res Row.names 1 2 3 1 a 0 1 1 2 b 1 0 0 3 c 0 4 4 4 x 1 0 0 5 z 0 4 4 # Subset res, leaving out the first col mat - res[, -1] # Set the rownames from res rownames(mat) - res[, 1] mat 1 2 3 a 0 1 1 b 1 0 0 c 0 4 4 x 1 0 0 z 0 4 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] merging
On Tue, 2006-05-30 at 15:38 -0500, Marc Schwartz (via MN) wrote: On Tue, 2006-05-30 at 19:08 +0100, Gavin Simpson wrote: Dear List, Given, y - matrix(c(0,1,1,1,0,0,0,4,4), ncol = 3, byrow = TRUE) rownames(y) - c(a,b,c) colnames(y) - c(1,2,3) y y2 - y[2:3, ] rownames(y2) - c(x,z) y2 how can I stop merge(y, y2, all = TRUE, sort = FALSE) squishing the extra rows? Ideally I want the same as: rbind(y, y2) in this case. This is specific example of situation where two data matrices have same column variables and all I want is to stick the two sets of rows together, but I have been using merge for cases such as the one below, where the second matrix has extra column(s): y3 - matrix(c(0,1,1,1,0,0,0,4,4,5,6,7), ncol = 4, byrow = TRUE) rownames(y3) - c(d,e,f) colnames(y3) - c(1,2,3,4) y3 merge(y, y3, all = TRUE, sort = FALSE) We don't know before hand if the columns will match. But I see now that even this doesn't work as I was expecting/thinking! So I'm looking for a general way to merge two matrices such that the number of rows in the merged matrix is nrow(mat1) + nrow(mat2) and the number of columns in the merged matrix is length(unique(colnames(mat1), colnames(mat2). Is there a function in R to do this, or can someone suggest a way to achieve this? My R version info is at the end. Just to be clear, for the y, y3 example I want something like this returned: 1 2 3 4 a 0 1 1 NA b 1 0 0 NA c 0 4 4 NA d 0 1 1 1 e 0 0 0 4 f 4 5 6 7 and for the y, y2 example, I want something like this returned: 1 2 3 a 0 1 1 b 1 0 0 c 0 4 4 x 1 0 0 z 0 4 4 Gavin, Here is a possible solution, though not fully tested. It uses the row.names for the two matrices as part of the 'by' matching process. This is noted in the Details section in ?merge. So for y and y2: res - merge(y, y2, by = c(row.names, intersect(colnames(y), colnames(y2))), all = TRUE) # Note that the row names are now the first col res Row.names 1 2 3 1 a 0 1 1 2 b 1 0 0 3 c 0 4 4 4 x 1 0 0 5 z 0 4 4 # Subset res, leaving out the first col mat - res[, -1] # Set the rownames from res rownames(mat) - res[, 1] mat 1 2 3 a 0 1 1 b 1 0 0 c 0 4 4 x 1 0 0 z 0 4 4 Ack...hit the wrong button. Sorry. Must be the long weekendyeah, that's my story and I'm sticking to it... ;-) Here is the solution for y and y3: res2 - merge(y, y3, by = c(row.names, intersect(colnames(y), colnames(y3))), all = TRUE) res2 Row.names 1 2 3 4 1 a 0 1 1 NA 2 b 1 0 0 NA 3 c 0 4 4 NA 4 d 0 1 1 1 5 e 0 0 0 4 6 f 4 5 6 7 mat2 - res2[, -1] rownames(mat2) - res2[, 1] mat2 1 2 3 4 a 0 1 1 NA b 1 0 0 NA c 0 4 4 NA d 0 1 1 1 e 0 0 0 4 f 4 5 6 7 HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to make attributes persist after indexing?
On Wed, 2006-05-24 at 17:20 +0100, Heinz Tuechler wrote: Dear All! For descriptive purposes I would like to add attributes to objects. These attributes should be kept, even if by indexing only part of the object is used. I noted that some attributes like levels and class of a factor exist also after indexing, while others, like comment or label vanish. Is there a way to make an arbitrary attribute to be kept after indexing? This would be especially useful when indexing a data.frame. ## example for loss of attributes fx - factor(1:5, ordered=TRUE) attr(fx, 'comment') - 'Comment for fx' attr(fx, 'label') - 'Label for fx' attr(fx, 'testattribute') - 'just for fun' attributes(fx) # all attributes are shown attributes(fx[])# all attributes are shown attributes(fx[1:5]) # only levels and class are shown attributes(fx[1]) # only levels and class are shown Thanks, Heinz Tüchler Non-standard attributes do not survive the use of [. You could create a new class and subset method for the objects where you require this type of functionality. Frank Harrell has done that in the Hmisc package to support the use of the labeling attributes, which in turn are used by some of his functions such as latex(). You might want to review what he has done in ?label where the [.labelled method has been added. Alternatively, you could create your own function to save the attributes, do the subsetting and then restore the attributes to the resultant object. HTH, Marc Schwartz __ 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] Regression line limited by the rage of values
On Wed, 2006-05-24 at 18:51 +0200, Andreas Svensson wrote: Hi In R, using plot(x,y) followed by abline(lm(y~x)) produces a graph with a regression line spanning the whole plot . This means that the line extends beyond the swarm of data points to the defined of default plot region. With par(xpd=T) it will span the entire figure region. But how can I limit a regression line to the data range, i.e between (xmin,ymin) and (xmax,ymax)? Sorry for not knowing the lingo here. If you don't understand the question, please run the following script: x1-c(1,2,3,4) x2-c(5,6,7,8) y1-c(2,4,5,8) y2-c(10,11,12,16) plot(x1,y1,xlim=c(0,10),ylim=c(0,20),col=blue) points(x2,y2,col=red) abline(lm(y1~x1),col=blue) abline(lm(y2~x2),col=red) The resulting plot isn't very informative. There is no overlap in the two groups of data, yet the two ablines overlap. I instead want the blue line to go from (1,2) to (4,8) and the red line from (5,10) to (8,16). So, how can I constrain the abline to the relevant region, i.e stop abline from extrapolating beyond the actual range of data. Or should I use a function line 'lines' to do this? Cheers Andres Try this instead of the two abline()'s: lines(x1, fitted(lm(y1 ~ x1)), col = blue) lines(x2, fitted(lm(y2 ~ x2)), col = red) The function fitted() will extract the model fitted y values from the lm() model object. See ?lines and ?fitted HTH, Marc Schwartz __ 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] Joining variables
On Wed, 2006-05-24 at 14:45 -0400, Guenther, Cameron wrote: Hello, If I have two variables that are factors or characters and I want to create a new variable that is the combination of both what function can I use to accomplish this? Ex. Var1 Var2 SA100055113 19851113 And I want NewVar SA10005511319851113 Thanks in advance. See ?paste and note the 'sep' argument: NewVar - paste(Var1, Var2, sep = ) NewVar [1] SA10005511319851113 HTH, Marc Schwartz __ 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] Regression line limited by the range of values
On Wed, 2006-05-24 at 21:53 +0200, Andreas Svensson wrote: Thankyou very much Marc for that nifty little script. When I use it on my real dataset though, the lines are fat in the middle and thinner towards the ends. I guess it's because lines draw one fitted line for each x, and if you have hundreds of x, this turns into a line that is thicker that it should be (due to rounding errors perhaps). I got the tip to use segments, and draw one line from min(x),min(y) to max(x),max(y) but with real data with a bunch of na.rm and na.actions this becomes very long and bulky. For a regression with two data swarms and two trend lines it becomes this long mess: plot(TCgonad[Period==1], ABelly[Period==1],xlim=c(0,20),ylim=c(130,160), col=”blue”) points(TCgonad[Period==2], ABelly[Period==2],col=”red”) segments( min(TCgonad[Period==1],na.rm=T), min(fitted(lm(ABelly[Period==1]~TCgonad[Period==1], na.action=na.exclude)),na.rm=T), max(TCgonad[Period==1],na.rm=T), max(fitted(lm(ABelly[Period==1]~TCgonad[Period==1], na.action=na.exclude)),na.rm=T),col=”blue”) segments( min(TCgonad[Period==2],na.rm=T), min(fitted(lm(ABelly[Period==2]~TCgonad[Period==2], na.action=na.exclude)),na.rm=T), max(TCgonad[Period==2],na.rm=T), max(fitted(lm(ABelly[Period==2]~TCgonad[Period==2], na.action=na.exclude)),na.rm=T),col=”red”) I just think it's strange that abline has as a nonadjustable default to extrapolate the line to outside the data - a mortal sin in my field. Cheers Andreas Andreas, What is likely happening is that your x values are not sorted in increasing order as they were in the dummy data set, thus there is probably some movement of the lines back and forth from one x value to the next in the order that they appear in the data set. Here is a more generic approach that should work. It is based upon the examples in ?predict.lm. We create the two models and then use range(x?) for the new min,max x values to be used for the prediction of the fitted y values. Thus: # Create model1 using random data set.seed(1) x1 - rnorm(15) y1 - x1 + rnorm(15) model1 - lm(y1 ~ x1) # Create model2 using random data set.seed(2) x2 - rnorm(15) y2 - x2 + rnorm(15) model2 - lm(y2 ~ x2) # Plot the first set of points # set the axis ranges based upon the common ranges of the # two sets of data plot(x1, y1, col = red, xlim = range(x1, x2), ylim = range(y1, y2)) # Add the second set of points points(x2, y2, col = blue) # Create the first fitted line # Create two x values for the prediction based upon the # range of x1 lines(range(x1), predict(model1, data.frame(x1 = range(x1))), col = red) # Create the second fitted line # Same here for the x2 values lines(range(x2), predict(model2, data.frame(x2 = range(x2))), col = blue) Note that in both cases for the fitted lines, the lines will be constrained by the range of the actual x? data values. This should be easier to apply in general. HTH, Marc Schwartz __ 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] conditional replacement
On Tue, 2006-05-23 at 11:40 -0700, Sachin J wrote: Hi How can do this in R. df 48 1 35 32 80 If df 30 then replace it with 30 and else if df 60 replace it with 60. I have a large dataset so I cant afford to identify indexes and then replace. Desired o/p: 48 30 35 32 60 Thanx in advance. Sachin One approach is to combine the use of two ifelse() statements: ifelse(df 30, 30, ifelse(df 60, 60, df)) [1] 48 30 35 32 60 Recall that if the condition (1st argument) is TRUE, then the second argument is evaluated and returned. If the condition is FALSE, then the third argument is evaluated, which in this case is another ifelse(). The same logic follows within that function. See ?ifelse HTH, Marc Schwartz __ 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] writing 100 files
On Mon, 2006-05-22 at 16:24 +0100, Federico Calboli wrote: Hi All, I need to write as text files 1000 ish variation of the same data frame, once I permute a row. I would like to use the function write.table() to write the files, and use a loop to do it: for (i in 1:1000){ bb8[2,] = sample(bb8[2,]) write.table(bb8, quote = F, sep = '\t', row.names = F, col.names = F, file = 'whatever?.txt') } so all the files are called whatever1: whatever1000 Any idea? Cheers, Federico The same process used in R FAQ 7.34: How can I save the result of each iteration in a loop into a separate file? can be used here. Instead of using save(), use write.table(), adjusting the arguments accordingly. HTH, Marc Schwartz __ 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] Subset a list
On Mon, 2006-05-22 at 17:55 -0400, Doran, Harold wrote: I have a data frame of ~200 columns and ~20,000 rows where each column consists of binary responses (0,1) and a 9 for missing data. I am interested in finding the columns for which there are fewer than 100 individuals with responses of 0. I can use an apply function to generate a table for each column, but I'm not certain whether I can subset a list based on some criterion as subset() is designed for vectors, matrices or dataframes. For example, I can use the following: tt - apply(data, 2, table) Which returns an object of class list. Here is some sample output from tt $R0235940b 0 1 9 2004 1076 15361 $R710a 0 9 2 18439 $R710b 0 1 9 3941 11167 tt$R710a meets my criteria and I would want to be able to easily find this instead of rolling through the entire output. Is there a way to subset this list to identify the columns which meet the criteria I note above? Thanks, Harold Harold, How about this: DF V1 V2 V3 V4 V5 1 0 1 0 1 0 2 0 0 1 0 1 3 0 0 1 1 0 4 1 1 0 0 1 5 1 1 1 1 0 6 0 1 0 1 1 7 0 1 1 1 0 8 0 1 0 0 0 9 0 0 1 1 0 10 1 0 0 1 1 # Find the columns with 5 0's which(sapply(DF, function(x) sum(x == 0)) 5) V2 V4 2 4 So in your case, just replace the DF with your data frame name and the 5 with 100. HTH, Marc Schwartz __ 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] make error for R-2.3.0
On Fri, 2006-05-19 at 15:37 -0400, Randall C Johnson [Contr.] wrote: Hello, I'm trying to install R on a linux machine running Red Hat 8. I ran ./configure make and get the following error. I've installed several versions of R (2.2.1 most recently) on this machine and haven't had any problems until now. I wondered if the outdated compiler (gcc version 3.2) was the problem and attempted to install my own, more recent version. I tried gcc versions 4.1.0 and 3.4.6, but still have problems. The output below is using gcc 3.4.6 (my best attempt, but still ending with the same error as gcc 3.2). Any pointers would be appreciated. Best, Randy SNIP of sock.h related errors Randy, This looks like the same issue that was reported on r-devel back at the end of April for RH 9. Download the latest r-patched tarball and you should be OK. Prof. Ripley made some changes to sock.h that should get around these issues. Unfortunately, they were not reported until after the release of 2.3.0. Download from here: ftp://ftp.stat.math.ethz.ch/Software/R/R-patched.tar.gz It might be time to consider updating your system, since RH 8.0 is not even supported by the Fedora Legacy folks any longer. That means no functional or security updates. Best regards, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How can you buy R?
On Fri, 2006-05-19 at 17:59 -0300, Rogerio Porto wrote: While reading the various answers, I've remembered that the juridic part can't be that so simple. If I'm not fogeting something, there are some packages in R that has a more restrictive licence than GPL. HTH, Rogerio. Any CRAN packages (or other R packages not on CRAN) that have non-commercial use restrictions, likely would not be able to be used by the OP anyway, even prior to this new policy. So I suspect that this would be a non-issue. If Damien's employer is willing to accept the GPL license (probably the most significant issue) and feels the need to pay for something, they could make an appropriate donation to the R Foundation. Perhaps even secure a little PR benefit for having done so. Is Damien's employer allowing the use of Firefox instead of IE? If so, the precedent within the confines of the policy has been set already. Firefox is GPL, free and no CD. There is an awful lot of commercial software out there than can be purchased online, properly licensed and downloaded, without the need for a physical CD. Anti-virus software perhaps being the most notable example. So: License: GPL CD: Don't need one Purchase:Donation to the R Foundation Being able to use R: Priceless :-) HTH, Marc Schwartz __ 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