Re: [R] Counting the loop-round of a "for"-loop
Consider using sapply instead of a for loop, if the code in the sapply call returns a vector, and every vector is the same length, then sapply will automatically form it into a matrix for you. On Sun, Feb 12, 2012 at 12:30 PM, jolo999 wrote: > It seems to work. Simple and effective! > > Thanks! > > -- > View this message in context: > http://r.789695.n4.nabble.com/Counting-the-loop-round-of-a-for-loop-tp4381319p4381780.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply pairs function to multiple columns in a data frame
You might find the pairs2 function in the TeachingDemos package useful. On Fri, Feb 10, 2012 at 1:13 PM, jarvisma wrote: > I am very new to R and programming and thank you in advance for your patience > and help with a complete novice! > > I am working with a large multivariate data set that has 10 explanatory > environmental variables (e.g. temp, depth) and over 60 response variables > (each is a separate species). My data frame is set up like the simplified > version below: > > JulianDay Temperature Salinity Depth Copepod Barnacle Gastropod > Bivalve > 222 12.1 33 0.3 500 > 756 0 178 > 222 12.3 33.2 1.1 145 > 111 0 0 > 223 11.1 33.1 7 752 > 234 12 0 > > Where JulianDay, Temperature, Salinity, Depth, Copepod, Barnacle, Gastropod, > and Bivalve are the column headers. > > I am using the pairs function in R to explore my data. My data frame is > named Sunset. > Using this code: > > Z=cbind(Sunset$Copepod,Sunset[,c(1:10)]) #the first 10 columns of my data > frame are explanatory variables such as temp > Pairs.Copepod=pairs(Z,main="Copepods vs. explanatory variables", > panel=panel.smooth) > > I get a great pair plot of Copepods vs. all of the 10 explanatory > environmental variables. I would like to do this for each of my 60+ > species. I can't just make one big pair plot of all of my explanatory > variables vs. all of my species because I have too many. So instead I would > like a separate pair plot for each species (each column of data after the > first 10 columns of explanatory variables) > > I would like to be able to write a loop that creates all of these plots at > once, but haven't been able to do so. Ideally, each pair plot would have > the main title be the column header (e.g. Copepod) for that plot. I would > love to include a way to save each pair plot as separate jpeg to a folder > on my desktop with a file name that includes the species name (e.g. > Copepod). > > Again, I am very new to R and to programing so I would GREATLY appreciate > anyone patient and kind enough to respond with lots of detail so I can > hopefully follow your answer and suggestions. I have searched but haven't > been able to find a way to write a loop that uses each column of data, so I > would love some help! > > Thank you! > Marley > > -- > View this message in context: > http://r.789695.n4.nabble.com/apply-pairs-function-to-multiple-columns-in-a-data-frame-tp4377425p4377425.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Split dataframe into new dataframes
I think one of your problems (the others have been addressed by others) is that you want the expression x$y to represent a column of x whose name is stored in y (not the name y itself). The problem here is that the $ notation is a magical shortcut and like any other magic if used incorrectly is likely to do the programmatic equivalent of turning yourself into a toad. It is better to use '[[' instead of the shortcut, try replacing x$y with x[[y]] and see if that fixes that problem. On Wed, Feb 8, 2012 at 2:11 PM, Johannes Radinger wrote: > Hi, > > I want to split a dataframe based on a grouping variable (in one column). The > resulting new > dataframes should be stored in a new variable. I tried to split the dataframe > using split() and > to store it using a FOR loop, but thats not working so far: > > df <- data.frame(A=c("A1","A1","A2","A2"),B=seq(1:4)) > > Fsplit <- function(x,y){ > ls <- split(x,f=x$y) > for (i in names(ls)){ > i <- ls$i > } > } > > Fsplit(df,A) #1st argument is dataframe to split, 2nd argument grouping > variable > > > Any suggestions how to get that done? > > Best regards > Johannes > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How indices calculated in package "boot"
In your second example the boot function will still generate its random indicies before your internal function calls sample, so the seed will be different when you call sample from what you originally set it to. If you really want to know what the boot function does, look at its code (it does some things to try and be more efficient which might give different results than a simpler version). On Wed, Feb 8, 2012 at 1:45 PM, Deng Nina wrote: > Hi,there, > > I am using R package "boot" to bootstrap. I have one question here: does > anybody possibly know how the boot package generates the "indices" which is > used in the statistic function? > > I thought "indices = sample(data, replace=TRUE)", but when I replaced > "indices" with this command and used "boot", I got different results. > > Specifically, below are the codes for illustration. > > (1) The typical way by generating indices in the package: > boot1 <- function (data, indices) > { > d <- data[indices] > return(d) > } > AA <- c(1:10) > require(boot) > set.seed(123) > results1 <- boot(data= AA, statistic=boot1, R=100) > > (2) The alternative way by calculating "indices" myself: > boot2 <- function (data,indices) > { > indices <- sample(data, replace=TRUE) > d <- data[indices] > return(d) > } > AA <- c(1:10) > set.seed(123) > results2 <- boot(data= AA, statistic=boot2, R=100) > > When I looked up using results1$t and results2$t, I had totoally different > bootstrap samples. I found this even had great impacts on the results in my > study. Does the second approach have any problem? Anyone could provide any > inputs on this? Thank you very much in advance! > Regards > Nina > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to stop a loop for?
If you want to pause for the person to look at a plot before going on to the next plot then just do: > par(ask=TRUE) This will actually allow your loop to continue with calculations while the user looks at the plot but will pause before drawing the next plot (hitting enter in the command window or clicking on the plot window will allow the code to continue). If you want to pause for something other than a plot then use readline like Michael suggests. On Wed, Feb 8, 2012 at 12:45 PM, Juan Andres Hernandez wrote: > Hi all, I have some time trying to find a way to stop a loop for( ) until the > user presses the enter key or any other one and the loop can continue. > This could > be an example: > > library(MASS) > data <- data.frame(mvrnorm(1000,rep(0,5),Sigma=diag(1,5))) > for(i in 1:dim(data)[2]){ > plot(density(data[,i]), main=paste('histogram',i)) > #here something like waituntil command > } > > Thank's in advance > Juan A. Hernandez > Spain. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to have columns lined up?
A little more on topic is that there are tools for R to nicely format output. The simplest is probably the R2wd package which will transfer objects and text to word, you can send a matrix or data frame to word as a nicely formatted table or wdVerbatim will send output to word but make sure that the font is the correct type to make things line up. If you are going to be doing a lot of this then you should investigate the variations on Sweave (Sword, knitr, odfWeave, etc.) which allow you to create a template file with R code and report text, then process it to replace the R code with its output. On Tue, Feb 7, 2012 at 9:07 PM, hithit168 wrote: > Hi there, > > Everytime when I paste My R output in WORD, the columns couldn't line up > nicely like they appear in R console. Is there a way to fix this problem? > Thanks for any help! > > > time n.risk n.event survival std.err lower 95% CI upper 95% CI > 6 21 3 0.857 0.0764 0.707 1.000 > 7 17 1 0.807 0.0869 0.636 0.977 > 10 15 1 0.753 0.0963 0.564 0.942 > 13 12 1 0.690 0.1068 0.481 0.900 > 16 11 1 0.627 0.1141 0.404 0.851 > 22 7 1 0.538 0.1282 0.286 0.789 > 23 6 1 0.448 0.1346 0.184 0.712 > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-have-columns-lined-up-tp4367928p4367928.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fitting polynomial (power greater than 2)
This looks like homework, which is generally discouraged here. You should at the least admit that it is homework and tell what resources your teacher has OK'd you to use. Since you did show what you have done so far and are not just asking for a handout, here is a hint: ?poly On Tue, Feb 7, 2012 at 9:34 PM, dkendall44 wrote: > Hey all, first time poster here. I'm new to R and working on my first real > programming and forecasting asignment. I'm using unemployment data from > 1948-2012. I successfully completed part a and the linear fit for part b, > but i am really struggling fitting a polynomial with a power greater than 2 > to my forecast. I'll upload my R code at the bottom. Any help is very much > appreciated! Thank You! > > (a) Produce a time-series plot of your data. > (b) Fit linear, polynomial (use a power greater than 2), and R’s best fit > models to your series. > > rm(list=ls(all=TRUE)) > z=read.table("longtermunempdata.txt") > names(z)= c("Unemployment") > attach(z) > TIME=seq(1,769,by=1) > > Unemployment_ts<-ts(Unemployment,start=1948,freq=12) > #quartz() > #plot(stl(Unemployment_ts,s.window="periodic")) > > #1A > quartz() > plot(TIME, Unemployment, xlab="Year", ylab="Long Term > Unemployment",type='l') > > #1B Linear > quartz() > plot(TIME,Unemployment,type="l", xlab="Year",ylab="Long Term Unemployment") > modelu=lm(Unemployment~TIME) > summary(modelu) > abline(modelu,col="red3") > > modelu=lm(Unemployment~TIME) > #1B Linear vs TS > quartz() > par(mfrow=c(2,1)) > plot(TIME, Unemployment, xlab="Year", ylab="Long Term > Unemployment",type='l') > abline(modelu,col="red3",lwd=2) > plot(TIME,modelu$res,xlab="Year",ylab="Long Term Unemployment",type='l') > > -- > View this message in context: > http://r.789695.n4.nabble.com/Fitting-polynomial-power-greater-than-2-tp4368026p4368026.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Positioning text in top left corner of plot
You might want to look at the grconvertX and grconvertY functions. You can use them to convert a coordinate relative to the plotting device (screen, paper) or plotting region to user coordinates to use with text or mtext. On Tue, Feb 7, 2012 at 4:06 AM, Manta wrote: > Dear all, > > another questions related to zoo plotting. I would like to do as in the > subject. Here a reproducible code: > > library(zoo) > par(mfrow=c(2,1) > plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1) > mtext("EUR billions",adj=0,cex=0.7) > plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1) > par(xpd=T) > text(par("usr")[1],par("usr")[3]+10.5,"EUR billions",cex=0.7) > > In the first graph I use the mtext function, which does the trick but it > places the text too close too the y-axis. > A second possibility is to use the text function, prior specification of the > option parameter par(xpd=T). However, this does not look consistent as, > depending on the graph, I would need to add different numbers in the y > specification of the axis (par("usr")[3]+XXX). > > Any idea? > > -- > View this message in context: > http://r.789695.n4.nabble.com/Positioning-text-in-top-left-corner-of-plot-tp831723p4364355.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] text command - how to get a white background to cover grid lines
An alternative is the shadowtext function in the TeachingDemos package. On Mon, Feb 6, 2012 at 1:19 AM, Jim Lemon wrote: > On 02/06/2012 08:23 AM, Henry wrote: >> >> New to R - rookie question. >> I'm a mechanical engineer and enjoying using R to make high quality >> graphs. >> >> I've searched. >> >> I want to put text notation on graph plot areas and have the text >> background >> "box" white to cover over the grid lines. >> >> my command so far >> text(15,5200,"Air Flow",cex=.8,col="blue", background="white") # this >> doesn't work... >> >> I've tried bg="white", background color="white" and a number of other >> attempts. >> >> The text is getting placed on the chart where I want it. >> > Hi Henry, > have a look at the boxed.labels function in the plotrix package. > > Jim > > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Clear last x entries of R console
You may want to look at functions like: txtProgressBar, winProgressBar (windows onnly), or tkProgressBar (tcltk package), rather than reinventing the wheel. On Fri, Feb 3, 2012 at 7:00 AM, angliski_jigit wrote: > Hi All, > > I am trying to build in a progress-tracker into my loops that let me have a > sense of their progress. I'd like to be able to output to screen a series of > periods "" etc. for each completion of the loop, but I want to > build a pyramid, e.g. > . > .. > ... > > etc. So I need to be able to delete of the console entry to > accomplish this. There are commands to erase the whole console, but that's > not what I want either; ideally, the command would allow me to erase the > last line or the last x lines. > > Thanks > Angliski > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Clear-last-x-entries-of-R-console-tp4354669p4354669.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] an unusual use for R
Nice, Last year I found that my office needed some decoration and my wife has some fancy sewing machines that can be programmed to do embroidery and cross-stitch. So I designed some cross stitches (using R and the program for the machines) that show distribution functions and equations for the Central Limit Theorem, Bayes Theorem, and the Mean Value Theorem of Integration and my wife stitched them out for me. I certainly get varied comments when people see them. Nice to know there are others who mix R, Statistics, and Textile crafts. On Thu, Feb 2, 2012 at 3:54 PM, Sarah Goslee wrote: > I thought some of you might be amused by this. > > In my non-work time, I'm an avid weaver and teacher of weaving. I'm > working on a project involving creating many detailed weaving > patterns, so I wrote R code to automate it. > > Details here: > http://stringpage.com/blog/?p=822 > > If the overlap between R users and avid tablet weavers turns out to be >>> 1, I'll polish it up and turn it into a package. > > Sarah > > -- > Sarah Goslee > http://www.functionaldiversity.org > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Vertical string with horizontal letters
There are only a few graphics devices that honor the 'crt' setting to rotate characters differently from the string rotation (postscript is the only one I know of, and then not always). For your specific case you could do something like: > text(1,1, paste( unlist(strsplit('output','')), collapse='\n'), adj=c(0,1)) You could use gsub instead of the paste and strsplit, but it adds an extra line feed at the beginning and end, or use: gsub('(?<=.)(?=.)','\n','output', perl=TRUE) You may also want to play around a little with the adj=c(0,1) to get the positioning that you want. On Thu, Feb 2, 2012 at 10:20 AM, Israel Byrd wrote: > > I'm trying to format text on a plot such that the string is vertical but the > letters are horizonal. I tried > text(1,1,label="output", srt=270) > This gives the string rotation I want, but that rotates the entire "output" > so the letters are also rotated. I've also tried > text(1,1,label="output", srt=270, crt=270) > to no avail. par()$crt doesn't seem to affect text? The format I want is > demonstrated below: > > o > u > t > p > u > t > > Thanks. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.