Re: [R] mapply instead for loop
Thanks for help. But, I am surprised, that mapply is slower than for loop? OV From: Uwe Ligges lig...@statistik.tu-dortmund.de Cc: r-help@r-project.org r-help@r-project.org Sent: Saturday, November 3, 2012 4:32 PM Subject: Re: [R] mapply instead for loop On 30.10.2012 20:01, Omphalodes Verna wrote: Hi all! My question in about using mapply instead for loop. Below is a example with for loop: Is it posible to give same results with mapply function? Thanks for help! OV x - 1:10 y - 1:10 xyz - data.frame(expand.grid(x,y)[1], expand.grid(x,y)[2], z = rnorm(100)) names(xyz) - c(x, y, z) head(xyz) size - 2 output - NULL ### for loop for(i in 1:dim(xyz)[1]){ x0 - xyz[i, x] y0 - xyz[i, y] xyzSel - xyz[xyz$x = (x0 - size) xyz$x (x0 + size) xyz$y = (y0 - size) xyz$y (y0 + size), ] output[i] - min(xyzSel$z) } output Yes: output - mapply(function(x0, y0) min(xyz[(xyz$x = (x0 - size) xyz$x (x0 + size)) (xyz$y = (y0 - size) xyz$y (y0 + size)), z]), xyz$x, xyz$y) Uwe Ligges [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Replacing a string
Hi, I have what I hope is a simple text processing question in R. I want to replace every instance of http:\\XXX.com with WEBSITE When I try sub('(^http://)(.com$)', 'WEBSITE', filename);, it only substitutes http:// and .com so it looks like WEBSITEXXXWEBSITE How do I get it to match the pattern http:// . . . . .com and substitute the whole phrase? Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Replacing-a-string-tp4648368.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.
Re: [R] Can you turn a string into a (working) symbol?
HI, get(DD[2]) #[1] 5 If you wanted to change the one without quotes: new1-function(x,y) {eval.parent(substitute(x-y))} new1(DD[2],7) DD[2] #[1] 7 DD #[1] 1 7 3 A.K. - Original Message - From: andrewH ahoer...@rprogress.org To: r-help@r-project.org Cc: Sent: Saturday, November 3, 2012 11:10 PM Subject: Re: [R] Can you turn a string into a (working) symbol? Yes, the assign command goes a little way toward what what I was hoping for. But it requires a different syntax, and it does not in general let you use quoted expressions that you could use with other assignment operators. For instance, DD - 1:3 assign(DD[2], 5) DD [1] 1 2 3 So I am still looking for a function that produces an output that is fully equivalent to the string without quotation marks. Or for a definite statement that no such function can exist. Thanks so much for your attention to this problem. andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343p4648366.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Saving R Graph to a file
Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.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.
Re: [R] Saving R Graph to a file
On Sun, Nov 4, 2012 at 4:16 AM, frespider frespi...@hotmail.com wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() Did you try running your own code? It should have thrown an error here since there's no device.off() function. You just want dev.off() Michael __ 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] Replacing a string
You want to read the ?regexp page and try gsub(^http://.*\\.com$;, WEBSITE, filename) Michael On Sun, Nov 4, 2012 at 4:09 AM, Allie818 alice...@gmail.com wrote: Hi, I have what I hope is a simple text processing question in R. I want to replace every instance of http:\\XXX.com with WEBSITE When I try sub('(^http://)(.com$)', 'WEBSITE', filename);, it only substitutes http:// and .com so it looks like WEBSITEXXXWEBSITE How do I get it to match the pattern http:// . . . . .com and substitute the whole phrase? Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Replacing-a-string-tp4648368.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am attempting to use optim under the default Nelder-Mead algorithm for model fitting, minimizing a Chi^2 statistic whose value is determined by a .C call to an external shared library compiled from C C++ code. My problem has been that the R session will immediately crash upon starting the simplex run, without it taking a single step. This is strange, as the .C call itself works, is error-free (as far as I can tell!) does not return NAN or Inf under any initial starting parameters that I have tested it with in R. It only ever crashes the R session when the Chi^2 function to be minimized is called from optim, not under any other circumstances. In the interests of reproducibility, I attach R code that reads attached data files attempts a N-M optim run. The required shared library containing the external code (compiled in Ubuntu 12.04 x64 with g++ 4.6.3) is also attached. Calculating an initial Chi^2 value for a starting set of model parameters works, then the R session crashes when the optim call is made. Is there something I'm perhaps doing wrong in the specification of the optim run? Is it inadvisable to use external code with optim? There doesn't seem to be a problem with the external code itself, so I'm very stumped as to the source of the crashes. __ 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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] Saving R Graph to a file
On Sat, 3 Nov 2012 21:16:49 -0700 (PDT) frespider frespi...@hotmail.com wrote: You're leaving out some critical information like the error messages, if any, you receive. That said, your example won't work properly because it is not properly coded. FI'm sure it gets tiresome being told to read the manual, but that is the way you learn R. For poscript output you need to read ?postscript() and follow the information there. There are important differences in detail between png(), pdf(), and postscript() that are critical. CAREFULLY read the help for postscript(), and also check into dev.off() use too. Your example doesn't restore things properly. If you do the above, you'll discover what is wrong with your example. JWDougherty __ 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] Saving R Graph to a file
Some hints: For pdf(), height and width are in inches, not pixels. dev.off() is necessary after drawing the image for pdf(). The name for the file argument (file=c:/figure.xxx) is file not filename hist(CO2[,5]) is more interesting And yes, ?pdf ?postscript ?ping On 11/3/2012 11:16 PM, frespider wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.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. -- __ Robert W Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 US __ 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] Saving R Graph to a file
On 11/4/2012 4:32 AM, Robert Baer wrote: Some hints: For pdf(), height and width are in inches, not pixels. dev.off() is necessary after drawing the image for pdf(). The name for the file argument (file=c:/figure.xxx) is file not filename hist(CO2[,5]) is more interesting And yes, ?pdf ?postscript ?png On 11/3/2012 11:16 PM, frespider wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.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. -- __ Robert W Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 US __ 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] sample equal number of cases per class
Dear community I have a dataframe and want to split it into a learn and a test partition. However the learnset should be balanced, i.e. each class should have the same number of cases. I tried and searched a lot, without success so far. Maybe you can help? Some example code *# generate example data df - data.frame(class = as.factor(sample(1:3, 20, replace = T)), var1 = rnorm(20,3), var2 = rnorm(20,6)) summary(df) # split into learn and test sets using the caret package require(caret) ind - createDataPartition(df$class, p=.8, list = F, times = 1) # The problem is here: class sizes are not equal) learnset - df[ind,] summary(learnset)* Version info: / R.Version() $platform [1] x86_64-pc-mingw32 $arch [1] x86_64 $os [1] mingw32 $system [1] x86_64, mingw32 $major [1] 2 $minor [1] 15.1/ -- View this message in context: http://r.789695.n4.nabble.com/sample-equal-number-of-cases-per-class-tp4648381.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.
Re: [R] optim .C / Crashing on run
It looks like my attached files didn't go through, so I'll put them in a public Dropbox folder instead; optim_rhelp.tar.gzhttp://dl.dropbox.com/u/1113102/optim_rhelp.tar.gz Thanks, I'll run a compiled binary of the C++ code through Valgrind see what it reports, then perhaps I'll try an Rscript execution of the R code calling the C++ in optim (not sure if Valgrind can process that though!). It does seem to be a memory error of some kind, since occasionally the OS pops up a crash report referencing a segmentation fault after optim crashes the R session. Though it is strange that the code has never crashed from a straight .C call in R, or when run from a compiled C++ binary. - Paul On 4 November 2012 09:35, Patrick Burns pbu...@pburns.seanet.com wrote: That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am attempting to use optim under the default Nelder-Mead algorithm for model fitting, minimizing a Chi^2 statistic whose value is determined by a .C call to an external shared library compiled from C C++ code. My problem has been that the R session will immediately crash upon starting the simplex run, without it taking a single step. This is strange, as the .C call itself works, is error-free (as far as I can tell!) does not return NAN or Inf under any initial starting parameters that I have tested it with in R. It only ever crashes the R session when the Chi^2 function to be minimized is called from optim, not under any other circumstances. In the interests of reproducibility, I attach R code that reads attached data files attempts a N-M optim run. The required shared library containing the external code (compiled in Ubuntu 12.04 x64 with g++ 4.6.3) is also attached. Calculating an initial Chi^2 value for a starting set of model parameters works, then the R session crashes when the optim call is made. Is there something I'm perhaps doing wrong in the specification of the optim run? Is it inadvisable to use external code with optim? There doesn't seem to be a problem with the external code itself, so I'm very stumped as to the source of the crashes. __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
When invoking R, you can add -d valgrind to run it under valgrind. On 04/11/2012 11:35, Paul Browne wrote: It looks like my attached files didn't go through, so I'll put them in a public Dropbox folder instead; optim_rhelp.tar.gz http://dl.dropbox.com/u/1113102/optim_rhelp.tar.gz Thanks, I'll run a compiled binary of the C++ code through Valgrind see what it reports, then perhaps I'll try an Rscript execution of the R code calling the C++ in optim (not sure if Valgrind can process that though!). It does seem to be a memory error of some kind, since occasionally the OS pops up a crash report referencing a segmentation fault after optim crashes the R session. Though it is strange that the code has never crashed from a straight .C call in R, or when run from a compiled C++ binary. - Paul On 4 November 2012 09:35, Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com wrote: That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am attempting to use optim under the default Nelder-Mead algorithm for model fitting, minimizing a Chi^2 statistic whose value is determined by a .C call to an external shared library compiled from C C++ code. My problem has been that the R session will immediately crash upon starting the simplex run, without it taking a single step. This is strange, as the .C call itself works, is error-free (as far as I can tell!) does not return NAN or Inf under any initial starting parameters that I have tested it with in R. It only ever crashes the R session when the Chi^2 function to be minimized is called from optim, not under any other circumstances. In the interests of reproducibility, I attach R code that reads attached data files attempts a N-M optim run. The required shared library containing the external code (compiled in Ubuntu 12.04 x64 with g++ 4.6.3) is also attached. Calculating an initial Chi^2 value for a starting set of model parameters works, then the R session crashes when the optim call is made. Is there something I'm perhaps doing wrong in the specification of the optim run? Is it inadvisable to use external code with optim? There doesn't seem to be a problem with the external code itself, so I'm very stumped as to the source of the crashes. R-help@r-project.org mailto:R-help@r-project.org mailing list https://stat.ethz.ch/mailman/__listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/__posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/__blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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] Saving R Graph to a file
Hello, #Example 1: The following code to save svyboxplots works for me pdf(boxplots_dthage.pdf, width = 1020) # 4 boxplots in 2 columns and 2 rows par(mfrow=c(2,2), oma=c(0,0,0,0)) # svyboxplot commands not shown dev.off() #Example 2: The following code to save a ggplot graph works for me: # ggolot () not shown print (p) ggsave(file='Xfacet_abodill_age3.pdf', width=12, height=8) Thanks, Pradip Muhuri From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf Of Robert Baer [rb...@atsu.edu] Sent: Sunday, November 04, 2012 5:32 AM To: frespider Cc: r-help@r-project.org Subject: Re: [R] Saving R Graph to a file Some hints: For pdf(), height and width are in inches, not pixels. dev.off() is necessary after drawing the image for pdf(). The name for the file argument (file=c:/figure.xxx) is file not filename hist(CO2[,5]) is more interesting And yes, ?pdf ?postscript ?ping On 11/3/2012 11:16 PM, frespider wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.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. -- __ Robert W Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 US __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loop...
On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote: Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A BC R 1 10 1 11 2 30 1 32 3 50 1 53 1000 100012000 1 10 221 2 30 262 3 50 2103 10001000 23000 How can I do it using for loop? You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named dat dat[R] - dat[A] + dat[B]*dat[C] dat[,R] - dat[,A] + dat[,B]*dat[,C] Read the R intro manual. Berend Thanks Shailly [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sample equal number of cases per class
Hello, Function caret::createDatapartition preserves the proportions of classes, like its documentation says, so you should expected the result to be balanced only if the original data.frame is also balanced. A solution is to write a small function that chooses a balanced set of indices. Note that ths function below does _not_ use the same arguments as caret::createDataPartition, its arguments are: x - the original vector, matrix or data.frame. y - a vector, what to balance. p - proportion of x to choose. createSets - function(x, y, p){ nr - NROW(x) size - (nr * p) %/% length(unique(y)) idx - lapply(split(seq_len(nr), y), function(.x) sample(.x, size)) unlist(idx) } ind - createSets(df, df$class, 0.8) lrn - df[ind,] summary(lrn) Also, 'df' is a bad name for a variable, it allready is an R function. Use, for instance, 'dat'. Hope this helps, Rui Barradas Em 04-11-2012 10:47, ollestrat escreveu: Dear community I have a dataframe and want to split it into a learn and a test partition. However the learnset should be balanced, i.e. each class should have the same number of cases. I tried and searched a lot, without success so far. Maybe you can help? Some example code *# generate example data df - data.frame(class = as.factor(sample(1:3, 20, replace = T)), var1 = rnorm(20,3), var2 = rnorm(20,6)) summary(df) # split into learn and test sets using the caret package require(caret) ind - createDataPartition(df$class, p=.8, list = F, times = 1) # The problem is here: class sizes are not equal) learnset - df[ind,] summary(learnset)* Version info: / R.Version() $platform [1] x86_64-pc-mingw32 $arch [1] x86_64 $os [1] mingw32 $system [1] x86_64, mingw32 $major [1] 2 $minor [1] 15.1/ -- View this message in context: http://r.789695.n4.nabble.com/sample-equal-number-of-cases-per-class-tp4648381.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] what is the function naming convention?
Dear R people, In typing names of functions (built in or from a package) I often guess wrong, and have to look the name up. In other words, I don't understand the logic in naming functions (if there is any): - most names are plain, lower case: cos, plot, sapply, t, toupper, unserialize, (etc) - some are capitalized: Filter, Machine, Map, NCOL, RNGversion, T (etc) - many are dotted: as.complex, as.data.frame.array, merge.data.frame, write.dcf (etc) The manual Creating R Packages states that it depends on the classes and instances. I couldn't find more hints. And there's more: - using underscore characters: check_tzones, Cstack_info, R_system_version (etc) - using interCapping: closeAllConnections, rawToChar, rowSums, toString, tryCatch, writeLines (etc) - using dots and intercapping: as.Date, julian.Date, toString.default (etc) So, an entire zoo of function names. Did I miss a system, or is it arbitrary (within the set of accepted characters) ? What is the best way to name one's own functions? Thanks in advance, Franklin Bretschneider Utrecht University Dept Biology Kruytgebouw W711 Padualaan 8 3584 CH Utrecht The Netherlands __ 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] what is the function naming convention?
On 12-11-04 8:16 AM, Bretschneider SIG-R wrote: Dear R people, In typing names of functions (built in or from a package) I often guess wrong, and have to look the name up. In other words, I don't understand the logic in naming functions (if there is any): R and its packages have been written by hundreds (thousands?) of people, and they do not use consistent naming conventions. Perhaps a convention should have been enforced from the beginning, but it's too late now. Duncan Murdoch - most names are plain, lower case: cos, plot, sapply, t, toupper, unserialize, (etc) - some are capitalized: Filter, Machine, Map, NCOL, RNGversion, T (etc) - many are dotted: as.complex, as.data.frame.array, merge.data.frame, write.dcf (etc) The manual Creating R Packages states that it depends on the classes and instances. I couldn't find more hints. And there's more: - using underscore characters: check_tzones, Cstack_info, R_system_version (etc) - using interCapping: closeAllConnections, rawToChar, rowSums, toString, tryCatch, writeLines (etc) - using dots and intercapping: as.Date, julian.Date, toString.default (etc) So, an entire zoo of function names. Did I miss a system, or is it arbitrary (within the set of accepted characters) ? What is the best way to name one's own functions? Thanks in advance, Franklin Bretschneider Utrecht University Dept Biology Kruytgebouw W711 Padualaan 8 3584 CH Utrecht The Netherlands __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sqldf Date problem
Zitat von jim holtman jholt...@gmail.com: Most likely your Date is either a character or a factor (you need to provide an 'str' of the dataframe). You are therefore most likely doing a character compare and that is the reason for your problem. You need to convert to a character string of the format -MM-DD to do the correct character comparison. ## x - data.frame(Date = paste0('1/', 1:31, '/2011')) str(x) 'data.frame': 31 obs. of 1 variable: $ Date: Factor w/ 31 levels 1/1/2011,1/10/2011,..: 1 12 23 26 27 28 29 30 31 2 ... x Date 1 1/1/2011 2 1/2/2011 3 1/3/2011 4 1/4/2011 5 1/5/2011 6 1/6/2011 7 1/7/2011 8 1/8/2011 9 1/9/2011 10 1/10/2011 11 1/11/2011 12 1/12/2011 13 1/13/2011 14 1/14/2011 15 1/15/2011 16 1/16/2011 17 1/17/2011 18 1/18/2011 19 1/19/2011 20 1/20/2011 21 1/21/2011 22 1/22/2011 23 1/23/2011 24 1/24/2011 25 1/25/2011 26 1/26/2011 27 1/27/2011 28 1/28/2011 29 1/29/2011 30 1/30/2011 31 1/31/2011 require(sqldf) # not correct because of character compares sqldf('select * from x where Date 1/13/2011 and Date 1/25/2011') Date 1 1/2/2011 2 1/14/2011 3 1/15/2011 4 1/16/2011 5 1/17/2011 6 1/18/2011 7 1/19/2011 8 1/20/2011 9 1/21/2011 10 1/22/2011 11 1/23/2011 12 1/24/2011 # convert the date to /MM/DD for character compares x$newDate - as.character(as.Date(as.character(x$Date), format = %m/%d/%Y)) # now do the select sqldf('select * from x where newDate between 2011-01-13 and 2011-01-25') DatenewDate 1 1/13/2011 2011-01-13 2 1/14/2011 2011-01-14 3 1/15/2011 2011-01-15 4 1/16/2011 2011-01-16 5 1/17/2011 2011-01-17 6 1/18/2011 2011-01-18 7 1/19/2011 2011-01-19 8 1/20/2011 2011-01-20 9 1/21/2011 2011-01-21 10 1/22/2011 2011-01-22 11 1/23/2011 2011-01-23 12 1/24/2011 2011-01-24 13 1/25/2011 2011-01-25 On Sat, Nov 3, 2012 at 4:22 PM, Andreas Recktenwald a.recktenw...@mx.uni-saarland.de wrote: Dear R-help readers, i've created a database for quotes data (for 4 years; 2007 -- 2010) with the sqldf package. This database contains a column Date in the format mm/dd/. The table in the database is called main.data and the database itself Honda. I tried to get the Data just for certain period, say from 01/01/2007 until 01/10/2007 with the following code: sqldf(select * from main.data where Date='01/10/2007' and Date='01/01/2007'), dbname=Honda) I get the data for this period for every year(2007,2008,2009,2010) not only for 2007. It seems that the year is overlooked and just looked for the fitting days and months. Because I haven't really much experience with sql I decide to send my problem to the list. Many thanks in advance. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. Thanks for your quick response Jim, you are right the entries in my Date column are characters (my fault not to mention this in my first post). Now i know the reasons for my problem and can solve it. __ 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] some help
HI everybody, Thanks for your answers, I can't provide data here, but I can explain more: It's satellite images as a text file (ascii). Two different images with two different variables, land surface temperature (LST -lstascii =filename) and an vegetation index (NDVI - ndviascii = filename). The aim was to do a scatter plot of NDVI against LST, whereas the NDVI should be on the x-axis. - The scatter plot itself is not needed as you will see. As I cannot compare each value (pixel value) in one image to the respective value in the other image, i had the idea to sort all columns one under each other. In the end I would have one column of one variable. I wanted to put the two variables LST and NDVI together in one table, so that I have two columns with two variables, and the value of one variable (NDVI) in each row can be compared directly to the value of the other variable (LST) in the same row. Why that? I wanted to sort the columns by the NDVI variable and make 0,01 NDVI intervals. From each of these intervals I wanted to know the max and min LST. These values I need for the regression - see the graph in the file attached. I this more clear now? Attached you can see the regression line I need to find. For this I developed this process (because I cannot compare pixel value by pixel value in a multi column and row table). http://r.789695.n4.nabble.com/file/n4648382/scatterplot.jpg Best Regards -- View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316p4648382.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.
Re: [R] some help
Hello, Why answer to just me? You should keep it in the R-Help list, the odds of getting more (and hopefully better) answers is bigger. Anyway, with data examples it's much easier to do something. I hope the code is self explanatory, except maybe for the use of scan(), not read.table. scan() reads in vectors, so there's no need to stack the columns anymore, they're just one big vector. Another thing is that around half of the values are zeros, and those don't show up in the graph you've posted, so I've filtered them out. fdir - Asciis # change if needed curr_dir - setwd(fdir) # save current directory and set working dir fls - list.files() lst - scan(fls[1], dec=,) ndv - scan(fls[2], dec=,) str(lst) str(ndv) i0 - ndv 0 lst 0 ndv0 - ndv[i0] lst0 - lst[i0] brks - seq(0, 1, by = 0.01) group - cut(ndv0, breaks = brks) mins - tapply(lst0, group, FUN = min) # one min per group maxs - tapply(lst0, group, FUN = max) # one max per group fit.min - lm(mins~brks[-1L]) fit.max - lm(maxs~brks[-1L]) dev.new() plot(ndv0, lst0, pch=.) abline(fit.min, col = blue) abline(fit.max, col = red) # Now with ave(). It returns one min/max per element in lst0, # so you'll have a vector as long as the original lst0, with the min/max # corresponding values. mins2 - ave(lst0, group, FUN = min) # one min per element of lst0 maxs2 - ave(lst0, group, FUN = max) # one max per element of lst0 fit.min2 - lm(mins2~ndv0) fit.max2 - lm(maxs2~ndv0) dev.new() plot(ndv0, lst0, pch=.) abline(fit.min2, col = blue) abline(fit.max2, col = red) Hope this helps, Rui Barradas Em 04-11-2012 10:36, Stefan Mühlbauer escreveu: Hello Rui, Thanks a lot for your answer. I can also provide you some data: It's satellite images as a text file (ascii). Two different images with two different variables, land surface temperature (LST -lstascii =filename) and an vegetation index (NDVI - ndviascii = filename). The aim was to do a scatter plot of NDVI against LST, whereas the NDVI should be on the x-axis. - The scatter plot itself is not needed as you will see. As I cannot compare each value (pixel value) in one image to the respective value in the other image, i had the idea to sort all columns one under each other. In the end I would have one column of one variable. I wanted to put the two variables LST and NDVI together in one table, so that I have two columns with two variables, and the value of one variable (NDVI) in each row can be compared directly to the value of the other variable (LST) in the same row. Why that? I wanted to sort the columns by the NDVI variable and make 0,01 NDVI intervals. From each of these intervals I wanted to know the max and min LST. These values I need for the regression - see the graph in the file attached. Can you understand this? Aattached you can see the ascii files of LST and NDVI. Again thanks a lot for help! Best Regards Stefan Dipl.-Ing. Stefan Mühlbauer Kaiser Strasse 85/2/15 A - 1070 Wien E-Mail: stefan.mue...@yahoo.de dattel_pa...@yahoo.de Von: Rui Barradas ruipbarra...@sapo.pt An: dattel_palme dattel_pa...@yahoo.de CC: r-help@r-project.org Gesendet: 19:34 Samstag, 3.November 2012 Betreff: Re: [R] some help Hello, Without data it's not easy to answer to your questions, but 1. Use ?unlist. If the data is in a file, read it with ?read.table and the unlist the result. All columns will be stacked. dat - read.table(filename, ...) unlist(dat) 2. At best confusing. But to divide a vector into groups use ?cut or ?findInterval and then, to find the maximum and minimum of each group, ?tapply or ?ave. 3. Regress what on what? Provide a data example using dput for better answers: dput( head(mydata, 30) ) # paste the output of this in a post Hope this helps, Rui Barradas Em 03-11-2012 16:07, dattel_palme escreveu: Hi People! I have following concern consisting of some steps to do in R: I have an ascii file (table) consisting of many columns and rows. 1. I would like to order all values of the columns one under each other. It will begin with column 1, then column 2 under column 1, column 3 under column 2 etc. until at the end there is only 1 column. How do I do it? 2. Second problem is to make a scatterplot of two variables (I think after further explanation scatter plot itself will not be needed). I have two columns of two different variables (that I produces before), column 1 with variable 1 and column 2 with variable 2. I would like to order them by one variable and 0,01 interval (the varibale values will range between 0 and 1). From each 0,01 interval (100 intervals) i want to pick the maximum and minimum value of variable 2. 3. From the obtained max and min of values of each interval i would like to make a linear least square regression. I hope someone can help me out! Thanks Stefan -- View this message in context:
[R] blackboost (mboost package) function leads to non-reclaimable memory usage
Dear all, I am puzzled by R's memory usage when calling the blackboost function from package mboost to estimate a Gradient boosting model on a simulated dataset with 20 correlated variables and 100,000 obs. The blackboost object created by the function is only 15.3Mb, but R's memory usage increases by about 3.9Gb during the estimation of the model and the memory is not released even after calling the garbage collection with gc() or saving and reloading the workspace to a new R session. I wonder what is causing this behavior and if there is a way to free up the extra memory? I appreciate any thoughts since I would really like to use this function. I already posted a similar question on stackoverflow http://stackoverflow.com/questions/13195733/how-can-i-remove-invisible-objects-form-an-r-workspace-that-are-not-removed-by-g , however haven't gotten any solutions yet. The following code and output illustrates my quesion: Thanks Nima -- View this message in context: http://r.789695.n4.nabble.com/blackboost-mboost-package-function-leads-to-non-reclaimable-memory-usage-tp4648391.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.
Re: [R] Saving R Graph to a file
Hi guys, I really appreciated all your responds, I made mistake on my example below hwere I wrote device.off() where it should be dev.off(). I read all the help documnet for odf png and postscript before I posted my question but it didn't help. Can some one please code an example? Thanks Date: Sun, 4 Nov 2012 02:39:13 -0800 From: ml-node+s789695n4648380...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Saving R Graph to a file On 11/4/2012 4:32 AM, Robert Baer wrote: Some hints: For pdf(), height and width are in inches, not pixels. dev.off() is necessary after drawing the image for pdf(). The name for the file argument (file=c:/figure.xxx) is file not filename hist(CO2[,5]) is more interesting And yes, ?pdf ?postscript ?png On 11/3/2012 11:16 PM, frespider wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] 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. -- __ Robert W Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 US __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below:http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369p4648380.html To unsubscribe from Saving R Graph to a file, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369p4648393.html Sent from the R help mailing list archive at Nabble.com. [[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] lines with colored segments
Hello all I'm trying to create a plot similar to a plot.default(..., type='b') with points plotted connected by lines that leave small gaps between the end of the line and the point symbol, BUT, with each line segment's color controlled by a category. plot... draws the line color uniformly according to the first color in a color sequence, ignoring the remainder. I can use segments() to give the proper colors using the x,y data, but those segments don't have the small gaps around the symbols. Somewhere, somehow, plot... either only draws the shortened segments, or draws the full segment, blanks out the space around the the symbol then adds the symbol (or, maybe something more sophisticated). Obviously I'm not the first to want to do this. Has anyone addressed this? Regards David -- David K Stevens, P.E., Ph.D., Professor Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT 84322-8200 435 797 3229 - voice 435 797 1363 - fax david.stev...@usu.edu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing NAs in long format
Thanks these different examples work perfectly. Chris On Sat, Nov 3, 2012 at 8:32 PM, arun smartpink...@yahoo.com wrote: HI Bill, It is much simpler. # with aggregate() and merge() res1-with(dat2,aggregate(seq_len(nrow(dat2)),by=list(idr=idr),FUN=function(i) with(dat2[i,], any(schyear=5 year ==0 res2-merge(dat2,res1,by=idr) colnames(res2)[4]-flag within(res2,{flag-as.integer(flag)}) #idr schyear year flag #1 1 4 -11 #2 1 501 #3 1 611 #4 1 721 #5 2 900 #6 2 1010 #7 2 1120 A.K. - Original Message - From: William Dunlap wdun...@tibco.com To: arun smartpink...@yahoo.com; Christopher Desjardins cddesjard...@gmail.com Cc: R help r-help@r-project.org Sent: Saturday, November 3, 2012 9:21 PM Subject: RE: [R] Replacing NAs in long format Or, even simpler, flag - with(dat2, ave(schyear=5 year==0, idr, FUN=any)) data.frame(dat2, flag) idr schyear year flag 1 1 4 -1 TRUE 2 1 50 TRUE 3 1 61 TRUE 4 1 72 TRUE 5 2 90 FALSE 6 2 101 FALSE 7 2 112 FALSE Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of William Dunlap Sent: Saturday, November 03, 2012 5:38 PM To: arun; Christopher Desjardins Cc: R help Subject: Re: [R] Replacing NAs in long format ave() or split-() can make that easier to write, although it may take some time to internalize the idiom. E.g., flag - rep(NA, nrow(dat2)) # add as.integer if you prefer 1,0 over TRUE,FALSE split(flag, dat2$idr) - lapply(split(dat2, dat2$idr), function(d)with(d, any(schyear=5 year==0))) data.frame(dat2, flag) idr schyear year flag 1 1 4 -1 TRUE 2 1 50 TRUE 3 1 61 TRUE 4 1 72 TRUE 5 2 90 FALSE 6 2 101 FALSE 7 2 112 FALSE or ave(seq_len(nrow(dat2)), dat2$idr, FUN=function(i)with(dat2[i,], any(schyear=5 year==0))) [1] 1 1 1 1 0 0 0 flag - ave(seq_len(nrow(dat2)), dat2$idr, FUN=function(i)with(dat2[i,], any(schyear=5 year==0))) data.frame(dat2, flag) idr schyear year flag 1 1 4 -11 2 1 501 3 1 611 4 1 721 5 2 900 6 2 1010 7 2 1120 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto: r-help-boun...@r-project.org] On Behalf Of arun Sent: Saturday, November 03, 2012 5:01 PM To: Christopher Desjardins Cc: R help Subject: Re: [R] Replacing NAs in long format Hi, May be this helps: dat2-read.table(text= idr schyear year 14 -1 150 161 172 290 2101 211 2 ,sep=,header=TRUE) dat2$flag-unlist(lapply(split(dat2,dat2$idr),function(x) rep(ifelse(any(apply(x,1,function(x) x[2]=5 x[3]==0)),1,0),nrow(x))),use.names=FALSE) dat2 # idr schyear year flag #1 1 4 -11 #2 1 501 #3 1 611 #4 1 721 #5 2 900 #6 2 1010 #7 2 1120 A.K. - Original Message - From: Christopher Desjardins cddesjard...@gmail.com To: jim holtman jholt...@gmail.com Cc: r-help@r-project.org Sent: Saturday, November 3, 2012 7:09 PM Subject: Re: [R] Replacing NAs in long format I have a similar sort of follow up and I bet I could reuse some of this code but I'm not sure how. Let's say I want to create a flag that will be equal to 1 if schyear = 5 and year = 0 for a given idr. For example dat idr schyear year 1 4 -1 1 50 1 61 1 72 2 90 2101 211 2 How could I make the data look like this? idr schyear year flag 1 4 -1 1 1 50 1 1 61 1 1 72 1 2 90 0 21010 211 2 0 I am not sure how to end up not getting both 0s and 1s for the 'flag' variable for an idr. For example, dat$flag = ifelse(schyear = 5 year ==0, 1, 0) Does not work because it will create: idr schyear year flag 1 4 -1 0 1 50 1 1 6
Re: [R] rgl package and animation
On 12-11-03 11:40 AM, Robert Baer wrote: On 11/3/2012 6:47 AM, Duncan Murdoch wrote: On 12-11-02 7:47 PM, Robert Baer wrote: I am trying to figure out how to use rgl package for animation. It appears that this is done using the play3d() function. Below I have some sample code that plots a 3D path and puts a sphere at the point farthest from the origin (which in this case also appears to be at the end of the path). What I would like to do is animate the movement of another sphere along the length of the path while simultaneously rotating the viewport. Duncan Murdock's (wonderful) Braided Knot YouTube video: (http://www.youtube.com/watch?v=prdZWQD7L5c) makes it clear that such things can be done, but I am having trouble understanding how to construct the f(time) function that gets passed to play3d(). The demo(flag) example is a little helpful, but I still can't quite translate it to my problem. Can anyone point to some some simple f(time) function examples that I could use for reference or give me a little hint as to how to construct f(time) for movement along the path while simultaneously rotating the viewport? Thanks, Rob library(rgl) # Generate a 3D path dat - structure(list(X = c(0, 0.06181308, 0.002235635, -0.03080658, -0.1728054, -0.372467, -0.5877065, -0.8814848, -1.103668, -1.366157, -1.625862, -1.948066, -2.265388, -2.689826, -3.095001, -3.49749, -3.946068, -4.395653, -4.772034, -5.111259, -5.410515, -5.649475, -5.73439, -5.662201, -5.567145, -5.390334, -5.081581, -4.796631, -4.496559, -4.457024, -4.459564, -4.641746, -4.849105, -5.0899430001, -5.43129, -5.763724, -6.199448, -6.517578, -6.864234, -6.907439), Y = c(0, -0.100724, -0.1694719, 0.036505999886, -0.09299519, -0.222977, -0.3557596, -0.3658229, -0.3299489, -0.2095574, -0.08041446, 0.02013388, 0.295372, 0.1388314, 0.2811047, 0.2237614, 0.1419052, 0.06029464, -0.09330875, -0.2075969, -0.3286296, -0.4385684, -0.4691093, -0.6235059, -0.5254676, -0.568444, -0.6388859, -0.727356, -1.073769, -1.0321350001, -1.203461, -1.438637, -1.6502310001, -1.861351, -2.169083, -2.4314730001, -2.6991430001, -2.961258, -3.239381, -3.466103), Z = c(0, 0.1355290002, 0.40106200024, 1.216374, 1.5539550003, 1.7308050003, 1.8116760003, 2.185124, 2.5260320004, 3.034794, 3.4265440004, 3.822512, 4.7449040002, 4.644837, 5.4184880002, 5.8586730001, 6.378356, 6.8339540001, 7.216339, 7.5941160004, 7.9559020004, 8.352936, 8.709319, 9.0166930003, 9.4855350003, 9.9000550001, 10.397003, 10.932068, 11.025726, 12.334595, 13.177887, 13.741852, 14.61142, 15.351013, 16.161255, 16.932831, 17.897186, 18.826691, 19.776001, 20.735596), time = c(0, 0.0116, 0.0196, 0.0311, 0.0391, 0.0507, 0.0623, 0.0703, 0.0818, 0.0899, 0.101, 0.109, 0.121, 0.129, 0.141, 0.152, 0.16, 0.172, 0.18, 0.191, 0.199, 0.211, 0.222, 0.23, 0.242, 0.25, 0.262, 0.27, 0.281, 0.289, 0.301, 0.312, 0.32, 0.332, 0.34, 0.351, 0.359, 0.371, 0.379, 0.391)), .Names = c(X, Y, Z, time), row.names = c(1844, 1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883), class = data.frame) # Plot 3d path with(dat, plot3d(X,Y,Z, type = 'l', col = 'blue', lty = 1)) # get absolute distance from origin dat$r = sqrt(dat$X ^ 2 + dat$Y ^ 2 + dat$Z ^ 2) mr = max(dat$r) # yes sorry, didn't get copied to original email code mxpnt = dat[dat$r == mr,] # Coordinates of furthest point # Plot a blue sphere at max distance plot3d(mxpnt$X, mxpnt$Y, mxpnt$Z, type = 's', radius = 1, col = 'blue', add = TRUE) Your code didn't include the mr variable, but I assume it's just max(dat$r). With that assumption, I'd do the animation function as follows: First, draw the new sphere at the first point and save the object id: sphereid - sphere3d(dat[1,c(X, Y, Z)], col=red, radius=1) # Also save the spinner that you like: spin - spin3d( ) #maybe with different parms # Now, the animation function: f - function(time) { par3d(skipRedraw = TRUE) # stops intermediate redraws on.exit(par3d(skipRedraw=FALSE)) # redraw at the end rgl.pop(id=sphereid) #
[R] Rd2pdf freeze
Hi everyone, From the currently available version of the package VBmix, I would like to retrieve the intermediate .tex file that generates the VBmix-manual.pdf file. Formerly, running R CMD check with --no-clean allowed to get this tex source from a hidden directory : this feature was removed, but can apparently still be accessed through R CMD Rd2pdf --no-clean. Surprisingly, while the manual generation runs with no warnings when issuing R CMD check --as-cran VBmix_0.2.9.tar.gz, R CMD Rd2pdf VBmix_0.2.9.tar.gz collapses under warnings, until it simply freezes... A sample : VBmix_0.2.9.tar.gz:30918: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30921: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30929: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30932: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : Rd files are encoded as regular ASCII text, I cannot get what is going wrong... Any idea ? Thanks by advance for your help. Pierrick Bruneau Research Associate CRP Gabriel Lippmann (Luxemburg) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lines with colored segments
On 12-11-04 8:29 AM, David Stevens wrote: Hello all I'm trying to create a plot similar to a plot.default(..., type='b') with points plotted connected by lines that leave small gaps between the end of the line and the point symbol, BUT, with each line segment's color controlled by a category. plot... draws the line color uniformly according to the first color in a color sequence, ignoring the remainder. I can use segments() to give the proper colors using the x,y data, but those segments don't have the small gaps around the symbols. Somewhere, somehow, plot... either only draws the shortened segments, or draws the full segment, blanks out the space around the the symbol then adds the symbol (or, maybe something more sophisticated). Obviously I'm not the first to want to do this. Has anyone addressed this? You might be the first to want to do this. It's a fairly strange plot: you have a category determined by the pair x[i] and x[i+1]. I think the only way to do this would be to draw the segments in a loop. For example, x - 1:10 y - 1:10 plot(x, y) # draw the points, in black col - rainbow(9) for (i in 1:9) # draw the segments in colour lines(x[i:(i+1)], y[i:(i+1)], type='c', col=col[i]) Duncan Murdoch __ 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] Rd2pdf freeze
On 12-11-04 8:50 AM, Pierrick Bruneau wrote: Hi everyone, From the currently available version of the package VBmix, I would like to retrieve the intermediate .tex file that generates the VBmix-manual.pdf file. Formerly, running R CMD check with --no-clean allowed to get this tex source from a hidden directory : this feature was removed, but can apparently still be accessed through R CMD Rd2pdf --no-clean. Surprisingly, while the manual generation runs with no warnings when issuing R CMD check --as-cran VBmix_0.2.9.tar.gz, R CMD Rd2pdf VBmix_0.2.9.tar.gz collapses under warnings, until it simply A tar.gz file is not an Rd file. The help for Rd2pdf says it accepts the files in these ways: Generate PDF output from the Rd sources specified by files, by either giving the paths to the files, or the path to a directory with the sources of a package, or an installed package. So you need to untar the file and specify the directory for it, you can't use a tar.gz file here. Duncan Murdoch freezes... A sample : VBmix_0.2.9.tar.gz:30918: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30921: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30929: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30932: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : Rd files are encoded as regular ASCII text, I cannot get what is going wrong... Any idea ? Thanks by advance for your help. Pierrick Bruneau Research Associate CRP Gabriel Lippmann (Luxemburg) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Rd2pdf freeze
I naively thought it would be able to examine the compressed file, Thanks for your answer :) On Sun, Nov 4, 2012 at 2:55 PM, Duncan Murdoch murdoch.dun...@gmail.comwrote: On 12-11-04 8:50 AM, Pierrick Bruneau wrote: Hi everyone, From the currently available version of the package VBmix, I would like to retrieve the intermediate .tex file that generates the VBmix-manual.pdf file. Formerly, running R CMD check with --no-clean allowed to get this tex source from a hidden directory : this feature was removed, but can apparently still be accessed through R CMD Rd2pdf --no-clean. Surprisingly, while the manual generation runs with no warnings when issuing R CMD check --as-cran VBmix_0.2.9.tar.gz, R CMD Rd2pdf VBmix_0.2.9.tar.gz collapses under warnings, until it simply A tar.gz file is not an Rd file. The help for Rd2pdf says it accepts the files in these ways: Generate PDF output from the Rd sources specified by files, by either giving the paths to the files, or the path to a directory with the sources of a package, or an installed package. So you need to untar the file and specify the directory for it, you can't use a tar.gz file here. Duncan Murdoch freezes... A sample : VBmix_0.2.9.tar.gz:30918: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30921: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30929: unexpected '{' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : VBmix_0.2.9.tar.gz:30932: unexpected '}' Warning in parse_Rd(VBmix_0.2.9.tar.gz, encoding = unknown, fragment = FALSE, : Rd files are encoded as regular ASCII text, I cannot get what is going wrong... Any idea ? Thanks by advance for your help. Pierrick Bruneau Research Associate CRP Gabriel Lippmann (Luxemburg) [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
Hi, Thanks for your help. Invoking valgrind under R for the test script I attached produces the following crash report; Rscript optim_rhelp.R -d valgrind Nelder-Mead direct search function minimizer function value for initial parameters = 1267.562555 Scaled convergence tolerance is 1.2e-05 Stepsize computed as 433.499000 *** caught segfault *** address 0x40, cause 'memory not mapped' Traceback: 1: .C(a_fsbl_wrapper, as.integer(length(t)), as.double(model_par[6]), as.double(model_par[7]), as.double(model_par[1]), as.double(model_par[2]), as.double(t), as.double(model_par[3]), as.double(model_par[4]), as.double(model_par[5]), as.double(prec), as.double(vector(double, length(t 2: fsbl_mag(subset(data$hjd, data$site_n == i), model_par) 3: fn(par, ...) 4: function (par) fn(par, ...)(c(4334.99, 53, 4.57, 0.277, 433.50033, 2.158, 0.288)) 5: optim(par = model_par, fn = fsbl_chi2, method = c(Nelder-Mead), control = list(trace = 6, maxit = 2000)) aborting ... Segmentation fault (core dumped) So definitely a memory problem then, but the traceback doesn't seem very informative as to its cause. Running a valgrind memcheck leak check just on a test of the C++ code, without it being called from R, reports no issues; ==6670== Memcheck, a memory error detector ==6670== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==6670== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==6670== Command: ./fsbl_y_test ==6670== Parent PID: 2614 ==6670== ==6670== ==6670== HEAP SUMMARY: ==6670== in use at exit: 0 bytes in 0 blocks ==6670== total heap usage: 6,022,561 allocs, 6,022,561 frees, 408,670,648 bytes allocated ==6670== ==6670== All heap blocks were freed -- no leaks are possible ==6670== ==6670== For counts of detected and suppressed errors, rerun with: -v ==6670== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) Perhaps it has something to do with how I've written two wrapping functions in the C/C++ code that pass input results back forth from R the rest of the external code? These are the two functions; //* //a_fsbl_wrapper - R wrapper function for FSBL magnification //* extern C { void a_fsbl_wrapper(int *k, double *a, double *q, double *t0, double *tE, double *t, double *alpha, double *u0, double *Rs, double *prec, double *result) { int i; for(i=0;i*k;i++){ result[i] = a_fsbl(*a,*q,*t0,*tE,t[i],*alpha,*u0,*Rs,*prec); } } } //* //a_fsbl - FSBL magnification, model parameters, no parallax //* double a_fsbl(double a, double q, double t0, double tE, double t, double alpha, double u0, double Rs, double prec) { double y1,y2; y1 = (-1)*u0*sin(alpha) + ((t-t0)/tE)*cos(alpha); y2 = y2 = u0*cos(alpha) + ((t-t0)/tE)*sin(alpha); return(BinaryLightCurve(a,q,y2,0.0,y1,Rs,prec)); } a_fsbl_wrapper takes input model parameters an input vector of times t, then returns an output vector result. The elements of result are calculated in a_fsbl, from a call to the rest of the external C++ code for each element. As I mentioned, this works amazingly well from a straight .C call in R, it only crashes when invoked by optim. - Paul On 4 November 2012 11:55, Patrick Burns pbu...@pburns.seanet.com wrote: When invoking R, you can add -d valgrind to run it under valgrind. On 04/11/2012 11:35, Paul Browne wrote: It looks like my attached files didn't go through, so I'll put them in a public Dropbox folder instead; optim_rhelp.tar.gz http://dl.dropbox.com/u/**1113102/optim_rhelp.tar.gzhttp://dl.dropbox.com/u/1113102/optim_rhelp.tar.gz Thanks, I'll run a compiled binary of the C++ code through Valgrind see what it reports, then perhaps I'll try an Rscript execution of the R code calling the C++ in optim (not sure if Valgrind can process that though!). It does seem to be a memory error of some kind, since occasionally the OS pops up a crash report referencing a segmentation fault after optim crashes the R session. Though it is strange that the code has never crashed from a straight .C call in R, or when run from a compiled C++ binary. - Paul On 4 November 2012 09:35, Patrick Burns pbu...@pburns.seanet.com mailto:pburns@pburns.seanet.**com pbu...@pburns.seanet.com wrote: That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am
Re: [R] For loop...
But ... On Sun, Nov 4, 2012 at 4:24 AM, Berend Hasselman b...@xs4all.nl wrote: On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote: Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A BC R 1 10 1 11 2 30 1 32 3 50 1 53 1000 100012000 1 10 221 2 30 262 3 50 2103 10001000 23000 How can I do it using for loop? You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named dat dat[R] - dat[A] + dat[B]*dat[C] dat[[R]] - dat[[A]] + dat[[B]]*dat[[C]] would be better, I believe. And the OP would be well-advised to read the Intro to R tutorial before posting further on this list. To quote Rolf Turner on this list: Learn something about R; don't just hammer and hope. Read the introductory manuals and scan the FAQ.. -- Bert dat[,R] - dat[,A] + dat[,B]*dat[,C] Read the R intro manual. Berend Thanks Shailly [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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] what is the function naming convention?
... but see the R Language Definition manual or ?UseMethod for S3 generic and method naming (overloading functions) procedures, which explains some of as.data.frame.array, merge.data.frame, etc. -- Bert On Sun, Nov 4, 2012 at 5:28 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 12-11-04 8:16 AM, Bretschneider SIG-R wrote: Dear R people, In typing names of functions (built in or from a package) I often guess wrong, and have to look the name up. In other words, I don't understand the logic in naming functions (if there is any): R and its packages have been written by hundreds (thousands?) of people, and they do not use consistent naming conventions. Perhaps a convention should have been enforced from the beginning, but it's too late now. Duncan Murdoch - most names are plain, lower case: cos, plot, sapply, t, toupper, unserialize, (etc) - some are capitalized: Filter, Machine, Map, NCOL, RNGversion, T (etc) - many are dotted: as.complex, as.data.frame.array, merge.data.frame, write.dcf (etc) The manual Creating R Packages states that it depends on the classes and instances. I couldn't find more hints. And there's more: - using underscore characters: check_tzones, Cstack_info, R_system_version (etc) - using interCapping: closeAllConnections, rawToChar, rowSums, toString, tryCatch, writeLines (etc) - using dots and intercapping: as.Date, julian.Date, toString.default (etc) So, an entire zoo of function names. Did I miss a system, or is it arbitrary (within the set of accepted characters) ? What is the best way to name one's own functions? Thanks in advance, Franklin Bretschneider Utrecht University Dept Biology Kruytgebouw W711 Padualaan 8 3584 CH Utrecht The Netherlands __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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] R 2.15.2 is released
Cool. I have some packages installed using install.packages(). Do I need to reinstall them? https://r-forge.r-project.org/tracker/?func=detailatid=294aid=2224group_id=61 Not a bug: This only happens under the circumstance of a Matrix package installation *not* matching your R installation. In other words: One way to fix your problem is to re install the Matrix package in the version of R you are using. So, will the bug reappear now? -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://openvotingconsortium.org http://mideasttruth.com http://www.memritv.org Lisp: Serious empowerment. __ 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] For loop...
On 04-11-2012, at 15:40, Bert Gunter wrote: But ... On Sun, Nov 4, 2012 at 4:24 AM, Berend Hasselman b...@xs4all.nl wrote: On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote: Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A BC R 1 10 1 11 2 30 1 32 3 50 1 53 1000 100012000 1 10 221 2 30 262 3 50 2103 10001000 23000 How can I do it using for loop? You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named dat dat[R] - dat[A] + dat[B]*dat[C] dat[[R]] - dat[[A]] + dat[[B]]*dat[[C]] would be better, I believe. Right. I insert the reply which was sent to me and not to the R-help list: I think you misunderstood my question. In my dataset, I have only two columns.( A and B with 1000 values each). I generated a variable C using seq(1,4200,20). Now for each value of C, I want to calculate 1000 R values (using 1000 - A and 1000 - B values) Finally, I want to have a dataset in which for each C value , I have 1000 R values. So the final dataset should have 210*1000 rows. Hope it is clear now. Well try this then: dat - read.table(text= A B 1 10 2 30 3 50 1000 1000 1 10 2 30 3 50 1000 1000, header=TRUE) dat[,C] - rep(1:2,each=nrow(dat)/2,length.out=nrow(dat)) dat[,R] - unlist(lapply(split(dat,dat$C), FUN=function(x) x$A+x$B*x$C)) dat Berend And the OP would be well-advised to read the Intro to R tutorial before posting further on this list. To quote Rolf Turner on this list: Learn something about R; don't just hammer and hope. Read the introductory manuals and scan the FAQ.. -- Bert dat[,R] - dat[,A] + dat[,B]*dat[,C] Read the R intro manual. Berend __ 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] optim .C / Crashing on run
Running this valgrind command on the test optim_rhelp.R script R -d valgrind --tool=memcheck --leak-check=full --log-file=optim_rhelp.valgrind.log --vanilla optim_rhelp.R yields this report: optim_rhelp.valgrind.loghttp://dl.dropbox.com/u/1113102/optim_rhelp.valgrind.log Ignoring everything in there to do with R other libraries, it seems like the problem in my external code is occuring here; ==8176== Invalid read of size 8 ==8176== at 0xCD8F0D3: _curve::~_curve() (VBBinaryLensing.cpp:257) ==8176== by 0xCD8F806: _sols::~_sols() (VBBinaryLensing.cpp:494) ==8176== by 0xCD95F20: BinaryMag(double, double, double, double, double, double) (VBBinaryLensing.cpp:816) ==8176== by 0xCD9659C: BinaryLightCurve(double, double, double, double, double, double, double) (VBBinaryLensing.cpp:636) ==8176== by 0xCD8D47C: a_fsbl_wrapper (fsbl.c:24) ==8176== by 0x4EEDCF2: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F25B1C: Rf_eval (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F2B092: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x5009A01: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F258FE: Rf_eval (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F276AF: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F258FE: Rf_eval (in /usr/lib/R/lib/libR.so) ==8176== Address 0x40 is not stack'd, malloc'd or (recently) free'd ==8176== ==8176== ==8176== Process terminating with default action of signal 11 (SIGSEGV) ==8176== General Protection Fault ==8176== at 0x571BC60: __snprintf_chk (snprintf_chk.c:31) ==8176== by 0x4FCEA81: Rf_EncodeReal (in /usr/lib/R/lib/libR.so) ==8176== by 0x4FCFEC7: Rf_EncodeElement (in /usr/lib/R/lib/libR.so) ==8176== by 0x4ED895D: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4EDAB4A: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4ED976D: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4EDAB4A: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4ED945B: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4EDAB4A: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4ED945B: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4EDA48E: ??? (in /usr/lib/R/lib/libR.so) ==8176== by 0x4F0ED54: R_GetTraceback (in /usr/lib/R/lib/libR.so) in the function ~_curve(void) or ~_sols(void) of the external C++ library. Unfortunately I didn't write this code library, nor do I have very much experience with C++ so this problem might well be unsolvable for me. If anyone could see anything wrong in the C++ code fragments comprising the problem functions, I'd be extremely grateful! _curve::~_curve(void){ _point *scan1,*scan2; scan1=first; for(int i=0;ilength;i++){ scan2=scan1-next; delete scan1; scan1=scan2; } } _sols::~_sols(void){ _curve *scan1,*scan2; scan1=first; while(scan1){ scan2=scan1-next; delete scan1; scan1=scan2; } } - Paul On 4 November 2012 14:20, Paul Browne paulfj.bro...@gmail.com wrote: Hi, Thanks for your help. Invoking valgrind under R for the test script I attached produces the following crash report; Rscript optim_rhelp.R -d valgrind Nelder-Mead direct search function minimizer function value for initial parameters = 1267.562555 Scaled convergence tolerance is 1.2e-05 Stepsize computed as 433.499000 *** caught segfault *** address 0x40, cause 'memory not mapped' Traceback: 1: .C(a_fsbl_wrapper, as.integer(length(t)), as.double(model_par[6]), as.double(model_par[7]), as.double(model_par[1]), as.double(model_par[2]), as.double(t), as.double(model_par[3]), as.double(model_par[4]), as.double(model_par[5]), as.double(prec), as.double(vector(double, length(t 2: fsbl_mag(subset(data$hjd, data$site_n == i), model_par) 3: fn(par, ...) 4: function (par) fn(par, ...)(c(4334.99, 53, 4.57, 0.277, 433.50033, 2.158, 0.288)) 5: optim(par = model_par, fn = fsbl_chi2, method = c(Nelder-Mead), control = list(trace = 6, maxit = 2000)) aborting ... Segmentation fault (core dumped) So definitely a memory problem then, but the traceback doesn't seem very informative as to its cause. Running a valgrind memcheck leak check just on a test of the C++ code, without it being called from R, reports no issues; ==6670== Memcheck, a memory error detector ==6670== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==6670== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==6670== Command: ./fsbl_y_test ==6670== Parent PID: 2614 ==6670== ==6670== ==6670== HEAP SUMMARY: ==6670== in use at exit: 0 bytes in 0 blocks ==6670== total heap usage: 6,022,561 allocs, 6,022,561 frees, 408,670,648 bytes allocated ==6670== ==6670== All heap blocks were freed -- no leaks are possible ==6670== ==6670== For counts of detected and suppressed errors, rerun with: -v ==6670== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) Perhaps it has something to do with how I've written two wrapping functions in the C/C++ code that pass input results back forth from R the rest of the external
Re: [R] For loop...
Hi Bert, I think you missed my clarification on the question, which I by mistake posted to the person who replied. Sorry about that. I think by reading my clarifiaction you will understand that I am not just hammering and hoping. I have a valid question. Thanks Shailly On Sun, Nov 4, 2012 at 10:16 AM, Berend Hasselman b...@xs4all.nl wrote: On 04-11-2012, at 15:40, Bert Gunter wrote: But ... On Sun, Nov 4, 2012 at 4:24 AM, Berend Hasselman b...@xs4all.nl wrote: On 04-11-2012, at 13:07, SHAILLY MEHROTRA wrote: Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A BC R 1 10 1 11 2 30 1 32 3 50 1 53 1000 100012000 1 10 221 2 30 262 3 50 2103 10001000 23000 How can I do it using for loop? You don't do that with a for loop. You can do it like this, assuming your dataset is a data.frame and is named dat dat[R] - dat[A] + dat[B]*dat[C] dat[[R]] - dat[[A]] + dat[[B]]*dat[[C]] would be better, I believe. Right. I insert the reply which was sent to me and not to the R-help list: I think you misunderstood my question. In my dataset, I have only two columns.( A and B with 1000 values each). I generated a variable C using seq(1,4200,20). Now for each value of C, I want to calculate 1000 R values (using 1000 - A and 1000 - B values) Finally, I want to have a dataset in which for each C value , I have 1000 R values. So the final dataset should have 210*1000 rows. Hope it is clear now. Well try this then: dat - read.table(text= A B 1 10 2 30 3 50 1000 1000 1 10 2 30 3 50 1000 1000, header=TRUE) dat[,C] - rep(1:2,each=nrow(dat)/2,length.out=nrow(dat)) dat[,R] - unlist(lapply(split(dat,dat$C), FUN=function(x) x$A+x$B*x$C)) dat Berend And the OP would be well-advised to read the Intro to R tutorial before posting further on this list. To quote Rolf Turner on this list: Learn something about R; don't just hammer and hope. Read the introductory manuals and scan the FAQ.. -- Bert dat[,R] - dat[,A] + dat[,B]*dat[,C] Read the R intro manual. Berend [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loop...
HI, Try this: dat1-read.table(text= A B C 1 10 1 2 30 1 3 50 1 1000 1000 1 1 10 2 2 30 2 3 50 2 1000 1000 2 ,sep=,header=TRUE) dat1$R-apply(dat1,1,function(x) x[1]+x[2]*x[3]) dat1 # A B C R #1 1 10 1 11 #2 2 30 1 32 #3 3 50 1 53 #4 1000 1000 1 2000 #5 1 10 2 21 #6 2 30 2 62 #7 3 50 2 103 #8 1000 1000 2 3000 A.K. - Original Message - From: SHAILLY MEHROTRA shaillymehro...@gmail.com To: r-help@r-project.org Cc: Sent: Sunday, November 4, 2012 7:07 AM Subject: [R] For loop... Dear R-users, I have dataset with column A and B with1000 values, For each of column C value (C = seq(1,1000,1), I want to repeat A and B values and calculate R = A+B*C for each row. I want to get output as A B C R 1 10 1 11 2 30 1 32 3 50 1 53 1000 1000 1 2000 1 10 2 21 2 30 2 62 3 50 2 103 1000 1000 2 3000 How can I do it using for loop? Thanks Shailly [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Saving R Graph to a file
On Sun, Nov 4, 2012 at 1:18 PM, frespider frespi...@hotmail.com wrote: Hi guys, I really appreciated all your responds, I made mistake on my example below hwere I wrote device.off() where it should be dev.off(). I read all the help documnet for odf png and postscript before I posted my question but it didn't help. Can some one please code an example? Thanks Beyond the ones in the relevant help files, png() plot(1:5) dev.off() should make a plot. Michael __ 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] Excluding fixed number of rows from calculation while summarizing using ddply() function.
Hello All, I have a .csv file( contents shown) below, where I need to calculate mean(for example) for only the rows highlighted in bold. (i.e. in this example case I need to exclude the first row and last row(N=1) for each *StepNo* column) Unique,StepNo,Data1,Data2#In actual file I have 100 columns and nearly millions of rows. A,1,4,5 #Exclude this 1st row for this StepNo and Unique combination. *A,1,5,6 * A,1,7,8 #Exclude this last row for this StepNo and Unique combination. A,2,9,10 #Exclude this row because this 1st row for this StepNo and Unique combination. *A,2,45,25* A,2,10,11 #Exclude this last row for this StepNo and Unique combination. B,2,34,12 #Exclude this 1st row for this StepNo and Unique combination. *B,2,5,6 B,2,7,8* B,2,6,7 #Exclude this last row for this StepNo and Unique combination. B,3,1,2 #Exclude this 1st row for this StepNo and Unique combination. *B,3,3,4* B,3,4,5 #Exclude this last row for this StepNo and Unique combination. My existing code to calculate mean* for all rows* is dat - read.csv(aboveinput.csv, header=T) #Loading Input file library(plyr) *result - ddply(dat, .(Unique,StepNo), numcolwise(mean))* # Calculating mean for each Unique and StepNo combination and summarizing the results. *I need to modify the above script to exclude some N number of rows at the start as well as at the end of each StepNo* Something like result - ddply(dat, .(Unique,StepNo),numcolwise(mean(head n rows excluded, tail n rows excluded in each StepNo))) #Just a skeleton script. Please revert to me if my question is not clear. - Sidda Business Analyst Lead Applied Materials Inc. -- View this message in context: http://r.789695.n4.nabble.com/Excluding-fixed-number-of-rows-from-calculation-while-summarizing-using-ddply-function-tp4648406.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.
Re: [R] Replacing a string
Thanks so much Arun! It's the second case. Being able to extract is really powerful too. Thank you for sharing that as well! Sent from my iPad On Nov 4, 2012, at 12:00 AM, arun kirshna [via R] ml-node+s789695n4648372...@n4.nabble.com wrote: HI, I am not sure how you want your output. Is it something like: WEBSITEWEBSITEWEBSITE #or just WEBSITE replacing the whole url I guess it is the former. url1-http:\\XXX.com gsub(.*,WEBSITEWEBSITEWEBSITE,url1) #[1] WEBSITEWEBSITEWEBSITE #2nd case gsub(.*,WEBSITE,url1) #[1] WEBSITE But, if you wanted to extract the 1st (http), 2nd (XXX), and 3rd (com) components: gsub(,,gsub((.*)\\:(.*)\\.(.*),\\1 \\2 \\3,url1)) #[1] http XXX com #or gsub((.*)\\:(.*)\\.(.*),\\1 \\2 \\3,url1) #[1] http XXX com #just the first component gsub((.*)\\:.*,\\1,url1) #[1] http #second alone gsub(.*\\:(.*)\\..*,\\1,url1) #[1] XXX A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Replacing-a-string-tp4648368p4648372.html To unsubscribe from Replacing a string, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Replacing-a-string-tp4648368p4648415.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] some help
HI David, Thanks for your answers, I can't provide data here, but I can explain more: It's satellite images as a text file (ascii). Two different images with two different variables, land surface temperature (LST -lstascii =filename) and an vegetation index (NDVI - ndviascii = filename). The aim was to do a scatter plot of NDVI against LST, whereas the NDVI should be on the x-axis. - The scatter plot itself is not needed as you will see. As I cannot compare each value (pixel value) in one image to the respective value in the other image, i had the idea to sort all columns one under each other. In the end I would have one column of one variable. I wanted to put the two variables LST and NDVI together in one table, so that I have two columns with two variables, and the value of one variable (NDVI) in each row can be compared directly to the value of the other variable (LST) in the same row. Why that? I wanted to sort the columns by the NDVI variable and make 0,01 NDVI intervals. From each of these intervals I wanted to know the max and min LST. These values I need for the regression - see the graph in the file attached. I this more clear now? Attached you can see the regression line I need to find. For this I developed this process (because I cannot compare pixel value by pixel value in a multi column and row table). http://r.789695.n4.nabble.com/file/n4648410/scatterplot.jpg Best Regards Stefan -- View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316p4648410.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Struggeling with nlminb...
Hallo together, I am trying to estimate parameters by means of QMLE using the nlminb optimizer for a tree-structured GARCH model. I face two problems. First, the optimizer returns error[8] false convergence if I estimate the functions below. I have estimated the model at first with nlm without any problems, but then I needed to add some constraints so i choose nlminb. my.loglike.normal-function(theta){ x.start- 1/n * sum(returns) sigmasq.start- 1/(n-1) * sum((returns-x.start)^2) data-c(x.start,returns) my.sigmasq- rep(0,n+1) my.sigmasq[1]-sigmasq.start for (i in 2:(n+1)) { my.sigmasq[i]-(theta[1] + theta[2]*data[i-1]^2 + theta[3]*my.sigmasq[i-1])*(data[i-1]=d1)*(my.sigmasq[i-1]=d2) + (theta[4] + theta[5]*data[i-1]^2 + theta[6]*my.sigmasq[i-1])*(my.sigmasq[i-1]d2)*(data[i-1]=d1)+ (theta[7] + theta[8]*data[i-1]^2 + theta[9]*my.sigmasq[i-1])*(data[i-1]d1)*(my.sigmasq[i-1]=d3)+(theta[10] + theta[11]*data[i-1]^2 + theta[12]*my.sigmasq[i-1])*(data[i-1]d1)*(my.sigmasq[i-1]d3) } my.mean-rep(0,n+1) for(j in 2:(n+1)) { my.mean[j]-theta[13]*data[j-1] } 1/2*sum(log(my.sigmasq[2:(n+1)])) + n/2*log(2*pi) + 1/2*sum((data[2:(n+1)]-my.mean[2:(n+1)])^2/(my.sigmasq[2:(n+1)])) } constLow=c(rep(0,(length(par.start)-1)),-2) my.optpar3- nlminb(par.start,my.loglike.normal,lower=constLow,control=list(eval.max=500,iter.max=100) ) Second, I estimate a similar function but with only 7 instead of 13 parameters, I fix theta[1]-theta[6] to some constant, but vary d3 in a loop. It seems like that the optimizer faces some NA/Inf issues for some d3. for(j in (my.d1j+1):7){ cat(j,\n) d3 - emp.quant[j] constLo=c(rep(0.1, (length(par.start)-1)), -99) my.optpar3 - nlminb(par.start, my.loglike.normal, lower=constLo, control=list(eval.max=60,iter.max=30)) value - valore.normal(my.optpar3$par) } Thank you for your help! Best, Marcial -- View this message in context: http://r.789695.n4.nabble.com/Struggeling-with-nlminb-tp4648413.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Apply same linear model to subset of dataframe
I have applied the same linear model to several different subsets of a dataset. I recently read that in R, code should never be repeated. I feel my code as it currently stands has a lot of repetition, which could be condensed into fewer lines. I will use the mtcars dataset to replicate what I have done. My question is: how can I use fewer lines of code (for example using a for loop, a function or plyr) to achieve the same output as below? data(mtcars) # Apply the same model to the dataset but choosing different combinations of dependent (DV) and independent (IV) variables in each case: lm.mpg= lm(mpg~cyl+disp+hp, data=mtcars) lm.drat = lm(drat~wt+qsec, data=mtcars) lm.gear = lm(gear~carb+hp, data=mtcars) # Plot residuals against fitted values for each model plot(lm.mpg$fitted,lm.mpg$residuals, main = lm.mpg) plot(lm.drat$fitted,lm.drat$residuals, main = lm.drat) plot(lm.gear$fitted,lm.gear$residuals, main = lm.gear) # Plot residuals against IVs for each model plotResIV - function (IV,lmResiduals) { lapply(IV, function (x) plot(x,lmResiduals)) } plotResIV(lm.mpg$model[,-1],lm.mpg$residuals) plotResIV(lm.drat$model[,-1],lm.drat$residuals) plotResIV(lm.gear$model[,-1],lm.gear$residuals) Many thanks Ross Ahmed [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R 2.15.2 is released
?update.packages -- Bert On Sun, Nov 4, 2012 at 7:01 AM, Sam Steingold s...@gnu.org wrote: Cool. I have some packages installed using install.packages(). Do I need to reinstall them? https://r-forge.r-project.org/tracker/?func=detailatid=294aid=2224group_id=61 Not a bug: This only happens under the circumstance of a Matrix package installation *not* matching your R installation. In other words: One way to fix your problem is to re install the Matrix package in the version of R you are using. So, will the bug reappear now? -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://openvotingconsortium.org http://mideasttruth.com http://www.memritv.org Lisp: Serious empowerment. __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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] R 2.15.2 is released
* Bert Gunter thagre.ore...@trar.pbz [2012-11-04 09:48:58 -0800]: ?update.packages It is not obvious to me that this is the answer to my question. Specifically, I have package X version 1.2.3 installed and built against R version 2.15.1. If 1.2.3 is the current latest version of X, then update.packages() will _not_ try to update it, but, apparently, at least for some packages, I do need to rebuild them against the new R version 2.15.2. Thanks. On Sun, Nov 4, 2012 at 7:01 AM, Sam Steingold s...@gnu.org wrote: I have some packages installed using install.packages(). Do I need to reinstall them? https://r-forge.r-project.org/tracker/?func=detailatid=294aid=2224group_id=61 Not a bug: This only happens under the circumstance of a Matrix package installation *not* matching your R installation. In other words: One way to fix your problem is to re install the Matrix package in the version of R you are using. So, will the bug reappear now? -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://americancensorship.org http://palestinefacts.org http://www.PetitionOnline.com/tap12009/ http://www.memritv.org http://memri.org If a woman is listening to a you without interrupting, do not wake her up! __ 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] R 2.15.2 is released
On Sun, Nov 4, 2012 at 6:22 PM, Sam Steingold s...@gnu.org wrote: * Bert Gunter thagre.ore...@trar.pbz [2012-11-04 09:48:58 -0800]: ?update.packages It is not obvious to me that this is the answer to my question. Specifically, I have package X version 1.2.3 installed and built against R version 2.15.1. If 1.2.3 is the current latest version of X, then update.packages() will _not_ try to update it, but, apparently, at least for some packages, I do need to rebuild them against the new R version 2.15.2. If I remember correctly (and I may well not) it's somewhat OS dependent, with OS X being rather strict about package updates (2.15.1 not being so compatible with 2.15.2) while linuxen are nicer about it. Certainly packages with significant compiled code (like Matrix) are more prone to versioning mis-matches. Michael __ 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] 95% Q-Q Plot error message
Can someone please help with the error message below? Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] Thanks! __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R 2.15.2 is released
On Nov 4, 2012, at 12:22 PM, Sam Steingold s...@gnu.org wrote: * Bert Gunter thagre.ore...@trar.pbz [2012-11-04 09:48:58 -0800]: ?update.packages It is not obvious to me that this is the answer to my question. Specifically, I have package X version 1.2.3 installed and built against R version 2.15.1. If 1.2.3 is the current latest version of X, then update.packages() will _not_ try to update it, but, apparently, at least for some packages, I do need to rebuild them against the new R version 2.15.2. Thanks. Take note of the 'checkBuilt' argument, which defaults to FALSE... Regards, Marc Schwartz On Sun, Nov 4, 2012 at 7:01 AM, Sam Steingold s...@gnu.org wrote: I have some packages installed using install.packages(). Do I need to reinstall them? https://r-forge.r-project.org/tracker/?func=detailatid=294aid=2224group_id=61 Not a bug: This only happens under the circumstance of a Matrix package installation *not* matching your R installation. In other words: One way to fix your problem is to re install the Matrix package in the version of R you are using. So, will the bug reappear now? __ 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] Q-Q Plot error message
Can someone please help with the error message below -- thanks! Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[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] Fw: 95% Q-Q Plot error message
Can someone please help with the below - thanks! Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] add1() alternative
Hi Bruno, probably not exactly what you are looking for, but maybe all subset regression as in library leaps might be an alternative for variable selection? But I am definitely not sure if this is faster than drop1() (calculates more models), nor have I ever tested it with a hierarchical logistic regression model. Another option might be stepAIC() in MASS library, which is capable of forward, backward and stepwise model selection. So it is add() and drop() in one function. Hope this might help a little. Greetings from Munich, Felix Am 02.11.12 10:49, schrieb brunosm: Hi, I'm trying to build a hierarchical logistic regression model with lme4 package, but I have a problem on selecting the variables to include in this model. In a simple logistic regression, using Forward selection, i use a likelihood ratio test to check which variables i should include in the model, using the function add1(). The problem is that this function doesn't work with the hierarchical model that i'm trying to achieve. Example: model- glmer(y ~ (1 | group)+x+sex+age, family = binomial(logit), data = db) add1(model, db, test=Chisq) Error: $ operator not defined for this S4 class I know that the drop1() function works fine to do a backward selection, but the problem is that i have 40 variables, and which time i use drop1() i have to wait a loong time to get a result... Is there any alternative to add1()? I mean, i don't want to use anova(model1,model2) because i would have to do all the models by hand... Thanks a lot guys, Bruno -- View this message in context: http://r.789695.n4.nabble.com/add1-alternative-tp4648215.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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
Playing around with alternate optimzers, I've found that both nlminb the nls.lm Levenberg-Marquadt optimizer in minpack.lm both work with my objective function without crashing, and minimize the function as I'd expect them to. Using optim for amoeba sampling would be nice, but I think I'll just have to chalk up its crashing with my external code library as a problem I won't be able to solve for now. I'll use nlminb or nls.lm for optimization a hand-coded MCMC algorithm for characterization of local minima. -- View this message in context: http://r.789695.n4.nabble.com/optim-C-Crashing-on-run-tp4648325p4648419.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.
Re: [R] 95% Q-Q Plot error message
Dear liang.che, I'm guessing that this is the qqPlot() function in the car package. This looks to me to be the combination of two problems: (1) You have at least one observation in your model for which the leverage (hat-value) is 1. That could happen, for example, if you have a factor in the model with only one observation at a particular level. (2) qqPlot() isn't handling that degenerate situation properly. Not only did I have to guess that you're using qqPlot() in the car package, but I had to guess what the problem is. If you read the text from r-help at the bottom of your message, you'll see that it says, provide commented, minimal, self-contained, reproducible code. If you'd like help beyond my remarks above, you're more likely to get it if you provide the commands and data for your problem. Of course, we'll take a look at qqPlot() to see whether it's doing something unreasonable, and fix it if we find a problem. Best, John --- John Fox Senator McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of liang@us.pwc.com Sent: Sunday, November 04, 2012 1:31 PM To: r-help@r-project.org Subject: [R] 95% Q-Q Plot error message Can someone please help with the error message below? Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] Thanks! __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Excluding fixed number of rows from calculation while summarizing using ddply() function.
dat1-read.table(text= Unique,StepNo,Data1,Data2 A,1,4,5 A,1,5,6 A,1,7,8 A,2,9,10 A,2,45,25 A,2,10,11 B,2,34,12 B,2,5,6 B,2,7,8 B,2,6,7 B,3,1,2 B,3,3,4 B,3,4,5 ,sep=,,header=TRUE,stringsAsFactors=FALSE) dat2-ddply(dat1,.(Unique,StepNo),function(x) x[c(1,nrow(x)),]) dat1$newcoldat1-TRUE dat2$newcoldat2-TRUE dat3-merge(dat1,dat2,all=TRUE) dat4-dat3[is.na(dat3$newcoldat2),1:4] dat4 # Unique StepNo Data1 Data2 #2 A 1 5 6 #6 A 2 45 25 #7 B 2 5 6 #9 B 2 7 8 #12 B 3 3 4 ddply(dat4,.(Unique,StepNo),numcolwise(mean)) # Unique StepNo Data1 Data2 #1 A 1 5 6 #2 A 2 45 25 #3 B 2 6 7 #4 B 3 3 4 A.K. - Original Message - From: siddu479 onlyfordigitalst...@gmail.com To: r-help@r-project.org Cc: Sent: Sunday, November 4, 2012 9:40 AM Subject: [R] Excluding fixed number of rows from calculation while summarizing using ddply() function. Hello All, I have a .csv file( contents shown) below, where I need to calculate mean(for example) for only the rows highlighted in bold. (i.e. in this example case I need to exclude the first row and last row(N=1) for each *StepNo* column) Unique,StepNo,Data1,Data2 #In actual file I have 100 columns and nearly millions of rows. A,1,4,5 #Exclude this 1st row for this StepNo and Unique combination. *A,1,5,6 * A,1,7,8 #Exclude this last row for this StepNo and Unique combination. A,2,9,10 #Exclude this row because this 1st row for this StepNo and Unique combination. *A,2,45,25* A,2,10,11 #Exclude this last row for this StepNo and Unique combination. B,2,34,12 #Exclude this 1st row for this StepNo and Unique combination. *B,2,5,6 B,2,7,8* B,2,6,7 #Exclude this last row for this StepNo and Unique combination. B,3,1,2 #Exclude this 1st row for this StepNo and Unique combination. *B,3,3,4* B,3,4,5 #Exclude this last row for this StepNo and Unique combination. My existing code to calculate mean* for all rows* is dat - read.csv(aboveinput.csv, header=T) #Loading Input file library(plyr) *result - ddply(dat, .(Unique,StepNo), numcolwise(mean))* # Calculating mean for each Unique and StepNo combination and summarizing the results. *I need to modify the above script to exclude some N number of rows at the start as well as at the end of each StepNo* Something like result - ddply(dat, .(Unique,StepNo),numcolwise(mean(head n rows excluded, tail n rows excluded in each StepNo))) #Just a skeleton script. Please revert to me if my question is not clear. - Sidda Business Analyst Lead Applied Materials Inc. -- View this message in context: http://r.789695.n4.nabble.com/Excluding-fixed-number-of-rows-from-calculation-while-summarizing-using-ddply-function-tp4648406.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Excluding fixed number of rows from calculation while summarizing using ddply() function.
Hi, One more way: dat1-read.table(text= Unique,StepNo,Data1,Data2 A,1,4,5 A,1,5,6 A,1,7,8 A,2,9,10 A,2,45,25 A,2,10,11 B,2,34,12 B,2,5,6 B,2,7,8 B,2,6,7 B,3,1,2 B,3,3,4 B,3,4,5 ,sep=,,header=TRUE,stringsAsFactors=FALSE) dat2-dat1[!(!duplicated(dat1[,1:2])|!duplicated(dat1[,1:2],fromLast=TRUE)),] library(plyr) ddply(dat2,.(Unique,StepNo),numcolwise(mean)) # Unique StepNo Data1 Data2 #1 A 1 5 6 #2 A 2 45 25 #3 B 2 6 7 #4 B 3 3 4 A.K. - Original Message - From: siddu479 onlyfordigitalst...@gmail.com To: r-help@r-project.org Cc: Sent: Sunday, November 4, 2012 9:40 AM Subject: [R] Excluding fixed number of rows from calculation while summarizing using ddply() function. Hello All, I have a .csv file( contents shown) below, where I need to calculate mean(for example) for only the rows highlighted in bold. (i.e. in this example case I need to exclude the first row and last row(N=1) for each *StepNo* column) Unique,StepNo,Data1,Data2 #In actual file I have 100 columns and nearly millions of rows. A,1,4,5 #Exclude this 1st row for this StepNo and Unique combination. *A,1,5,6 * A,1,7,8 #Exclude this last row for this StepNo and Unique combination. A,2,9,10 #Exclude this row because this 1st row for this StepNo and Unique combination. *A,2,45,25* A,2,10,11 #Exclude this last row for this StepNo and Unique combination. B,2,34,12 #Exclude this 1st row for this StepNo and Unique combination. *B,2,5,6 B,2,7,8* B,2,6,7 #Exclude this last row for this StepNo and Unique combination. B,3,1,2 #Exclude this 1st row for this StepNo and Unique combination. *B,3,3,4* B,3,4,5 #Exclude this last row for this StepNo and Unique combination. My existing code to calculate mean* for all rows* is dat - read.csv(aboveinput.csv, header=T) #Loading Input file library(plyr) *result - ddply(dat, .(Unique,StepNo), numcolwise(mean))* # Calculating mean for each Unique and StepNo combination and summarizing the results. *I need to modify the above script to exclude some N number of rows at the start as well as at the end of each StepNo* Something like result - ddply(dat, .(Unique,StepNo),numcolwise(mean(head n rows excluded, tail n rows excluded in each StepNo))) #Just a skeleton script. Please revert to me if my question is not clear. - Sidda Business Analyst Lead Applied Materials Inc. -- View this message in context: http://r.789695.n4.nabble.com/Excluding-fixed-number-of-rows-from-calculation-while-summarizing-using-ddply-function-tp4648406.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R 2.15.2 is released
* Marc Schwartz znep_fpujn...@zr.pbz [2012-11-04 12:33:20 -0600]: On Nov 4, 2012, at 12:22 PM, Sam Steingold s...@gnu.org wrote: * Bert Gunter thagre.ore...@trar.pbz [2012-11-04 09:48:58 -0800]: ?update.packages It is not obvious to me that this is the answer to my question. Take note of the 'checkBuilt' argument, which defaults to FALSE... Thanks a lot! So, what I need to do is: update.packages(checkBuilt=TRUE, ask=FALSE, lib.loc=.libPaths()[grep(^/home/,.libPaths())]) -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://americancensorship.org http://pmw.org.il http://iris.org.il http://camera.org http://jihadwatch.org http://dhimmi.com Kleptomania: the ability to find stuff even before its owner loses it. __ 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] Fw: 95% Q-Q Plot error message
Please don't double post. And we'll need a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Michael On Sun, Nov 4, 2012 at 6:50 PM, liang@us.pwc.com wrote: Can someone please help with the below - thanks! Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 95% Q-Q Plot error message
Thanks John. I have one observation data point with a value that's exactly equal to the predicted value, therefore the residual is 0. Would this be the reason you mentioned below? From: John Fox j...@mcmaster.ca To: Liang Che/US/TLS/PwC@Americas-US Cc: 'Sanford Weisberg' sa...@umn.edu, r-help@r-project.org Date: 11/04/2012 02:03 PM Subject:RE: [R] 95% Q-Q Plot error message Dear liang.che, I'm guessing that this is the qqPlot() function in the car package. This looks to me to be the combination of two problems: (1) You have at least one observation in your model for which the leverage (hat-value) is 1. That could happen, for example, if you have a factor in the model with only one observation at a particular level. (2) qqPlot() isn't handling that degenerate situation properly. Not only did I have to guess that you're using qqPlot() in the car package, but I had to guess what the problem is. If you read the text from r-help at the bottom of your message, you'll see that it says, provide commented, minimal, self-contained, reproducible code. If you'd like help beyond my remarks above, you're more likely to get it if you provide the commands and data for your problem. Of course, we'll take a look at qqPlot() to see whether it's doing something unreasonable, and fix it if we find a problem. Best, John --- John Fox Senator McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of liang@us.pwc.com Sent: Sunday, November 04, 2012 1:31 PM To: r-help@r-project.org Subject: [R] 95% Q-Q Plot error message Can someone please help with the error message below? Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] Thanks! __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[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. __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[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] select duplicate identifier with higher mean across sample columns
Hi Group: I searched R groups before posting this question. I could not find the appropriate answer and I do not have clear understanding how to do this in R. I have a data frame with duplicated row identifiers but with different values across columns. I want to select the identifier with higher inter-quartile range or mean. id - c(A, A, C, D, E, F) year - c(2000, 2001, 2001, 2002, 2003, 2004) samp1 - c(100, 120, 101, 110, 132,123) samp2 - c(110, 130, 131, 150, 122,143) mdf - data.frame(id,samp1,samp2,samp2a) mdf id samp1 samp2 samp2a 1 A 100 110110 2 A 120 130150 3 C 101 131151 4 D 110 150130 5 E 132 122122 6 F 123 143143 There are two A ids in this df. I want to select the row with higher mean. How can I do this. Thanks Adrian __ 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] select duplicate identifier with higher mean across sample columns
Is this what you want: mdf - read.table(text = id samp1 samp2 samp2a + 1 A 100 110110 + 2 A 120 130150 + 3 C 101 131151 + 4 D 110 150130 + 5 E 132 122122 + 6 F 123 143143, header = TRUE) result - do.call(rbind, lapply(split(mdf, mdf$id), function(.id){ + maxIndx - which.max(rowMeans(.id[, -1L])) + .id[maxIndx, ] + })) result id samp1 samp2 samp2a A A 120 130150 C C 101 131151 D D 110 150130 E E 132 122122 F F 123 143143 On Sun, Nov 4, 2012 at 2:25 PM, Adrian Johnson oriolebaltim...@gmail.com wrote: Hi Group: I searched R groups before posting this question. I could not find the appropriate answer and I do not have clear understanding how to do this in R. I have a data frame with duplicated row identifiers but with different values across columns. I want to select the identifier with higher inter-quartile range or mean. id - c(A, A, C, D, E, F) year - c(2000, 2001, 2001, 2002, 2003, 2004) samp1 - c(100, 120, 101, 110, 132,123) samp2 - c(110, 130, 131, 150, 122,143) mdf - data.frame(id,samp1,samp2,samp2a) mdf id samp1 samp2 samp2a 1 A 100 110110 2 A 120 130150 3 C 101 131151 4 D 110 150130 5 E 132 122122 6 F 123 143143 There are two A ids in this df. I want to select the row with higher mean. How can I do this. Thanks Adrian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ 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] select duplicate identifier with higher mean across sample columns
Hello, Thanks for the data example. (You forgot samp2a). Try the following. mdf - read.table(text= id samp1 samp2 samp2a 1 A 100 110110 2 A 120 130150 3 C 101 131151 4 D 110 150130 5 E 132 122122 6 F 123 143143 , header=TRUE) idx - ave(rowMeans(mdf[,-1]), mdf$id, FUN = function(x) x == max(x)) mdf[as.logical(idx), ] Hope this helps, Rui Barradas Em 04-11-2012 19:25, Adrian Johnson escreveu: Hi Group: I searched R groups before posting this question. I could not find the appropriate answer and I do not have clear understanding how to do this in R. I have a data frame with duplicated row identifiers but with different values across columns. I want to select the identifier with higher inter-quartile range or mean. id - c(A, A, C, D, E, F) year - c(2000, 2001, 2001, 2002, 2003, 2004) samp1 - c(100, 120, 101, 110, 132,123) samp2 - c(110, 130, 131, 150, 122,143) mdf - data.frame(id,samp1,samp2,samp2a) mdf id samp1 samp2 samp2a 1 A 100 110110 2 A 120 130150 3 C 101 131151 4 D 110 150130 5 E 132 122122 6 F 123 143143 There are two A ids in this df. I want to select the row with higher mean. How can I do this. Thanks Adrian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 95% Q-Q Plot error message
Dear Liang Che, -Original Message- From: liang@us.pwc.com [mailto:liang@us.pwc.com] Sent: Sunday, November 04, 2012 2:12 PM To: j...@mcmaster.ca Cc: r-help@r-project.org; 'Sanford Weisberg' Subject: RE: [R] 95% Q-Q Plot error message Thanks John. I have one observation data point with a value that's exactly equal to the predicted value, therefore the residual is 0. Would this be the reason you mentioned below? No. An observation with hat-value (leverage) equal to 1 will necessarily have a 0 residual, but the converse isn't true. Are your data a secret? If so, maybe you can make up a suitable example that you can share and that demonstrates the problem. As I said, we'll do what we can in the absence of a reproducible example. John From:John Fox j...@mcmaster.ca To:Liang Che/US/TLS/PwC@Americas-US Cc:'Sanford Weisberg' sa...@umn.edu, r-help@r-project.org Date:11/04/2012 02:03 PM Subject:RE: [R] 95% Q-Q Plot error message Dear liang.che, I'm guessing that this is the qqPlot() function in the car package. This looks to me to be the combination of two problems: (1) You have at least one observation in your model for which the leverage (hat-value) is 1. That could happen, for example, if you have a factor in the model with only one observation at a particular level. (2) qqPlot() isn't handling that degenerate situation properly. Not only did I have to guess that you're using qqPlot() in the car package, but I had to guess what the problem is. If you read the text from r-help at the bottom of your message, you'll see that it says, provide commented, minimal, self-contained, reproducible code. If you'd like help beyond my remarks above, you're more likely to get it if you provide the commands and data for your problem. Of course, we'll take a look at qqPlot() to see whether it's doing something unreasonable, and fix it if we find a problem. Best, John --- John Fox Senator McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org mailto:r-help-boun...@r-project.org ] On Behalf Of liang@us.pwc.com Sent: Sunday, November 04, 2012 1:31 PM To: r-help@r-project.org Subject: [R] 95% Q-Q Plot error message Can someone please help with the error message below? Warning messages: 1: Not plotting observations with leverage one: 7 2: Not plotting observations with leverage one: 7 print(qqPlot(fit),envelop=.95); Error in model.frame.default(formula = Y ~ X - 1, drop.unused.levels = TRUE) : variable lengths differ (found for 'X') In addition: Warning message: In matrix(yhat, n, reps) : data length [9] is not a sub-multiple or multiple of the number of rows [8] Thanks! __ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- http://www.r-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries.
Re: [R] select duplicate identifier with higher mean across sample columns
Hi, Try this: mdf[unlist(tapply(rowMeans(mdf[,-1]),mdf$id,FUN=function(x) x%in%max(x))),] # id samp1 samp2 samp2a #2 A 120 130 150 #3 C 101 131 151 #4 D 110 150 130 #5 E 132 122 122 #6 F 123 143 143 A.K. - Original Message - From: Adrian Johnson oriolebaltim...@gmail.com To: r-help r-help@r-project.org Cc: Sent: Sunday, November 4, 2012 2:25 PM Subject: [R] select duplicate identifier with higher mean across sample columns Hi Group: I searched R groups before posting this question. I could not find the appropriate answer and I do not have clear understanding how to do this in R. I have a data frame with duplicated row identifiers but with different values across columns. I want to select the identifier with higher inter-quartile range or mean. id - c(A, A, C, D, E, F) year - c(2000, 2001, 2001, 2002, 2003, 2004) samp1 - c(100, 120, 101, 110, 132,123) samp2 - c(110, 130, 131, 150, 122,143) mdf - data.frame(id,samp1,samp2,samp2a) mdf id samp1 samp2 samp2a 1 A 100 110 110 2 A 120 130 150 3 C 101 131 151 4 D 110 150 130 5 E 132 122 122 6 F 123 143 143 There are two A ids in this df. I want to select the row with higher mean. How can I do this. Thanks Adrian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Problem with polygon vertices
I am building a mask with area.urb.sp.W - as(area.urb.sp, owin) and got the message Erro em owin(poly = opls) : Polygon data contain duplicated vertices, self-intersection and overlaps between polygons how can I solve this problem in R? many thanks IVAN [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Problem with polygon vertices
On 05/11/12 13:13, Luis Iván Ortiz Valencia wrote: I am building a mask with area.urb.sp.W - as(area.urb.sp, owin) and got the message Erro em owin(poly = opls) : Polygon data contain duplicated vertices, self-intersection and overlaps between polygons how can I solve this problem in R? many thanks Basically you need to examine area.urb.sp carefully and amend it so that it doesn't have duplicated vertices, self-intersection, and overlaps. This can be difficult, especially if the boundaries of the polygons involved have a large number of edges. You *could* set spatstat.options(checkpolygons=FALSE) but this is not advised. The owin object you wind up with will in general be nonsensical. Doing this, and plotting the result might give you some insight as to what needs to be fixed. But *do* fix it. Don't just go with the nonsensical window. This would cause any further analyses to yield nonsense. Garbage in, garbage out. cheers, Rolf Turner __ 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] Replace array with percentile values
Hi: I have an array of measurements, that I've been doing linear regression model and AI models on. Because there are many errors and the values are ill-formed I would like to copy the array, but replace each value with the PERCENTILE of that value, in the original array. i.e. mesments$V1: 9, 77, -1 would become: mesmentsCopy$V1: 50, 100, 0 The actual array has many more rows and columns, of course. Cheers, Greg Allen Freelance Techno-Slave SLC, Utah __ 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] Replace array with percentile values
On Nov 4, 2012, at 7:22 PM, greggal...@gmail.com wrote: Hi: I have an array of measurements, that I've been doing linear regression model and AI models on. Because there are many errors and the values are ill-formed I would like to copy the array, but replace each value with the PERCENTILE of that value, in the original array. i.e. mesments$V1: 9, 77, -1 If you are using the $ function, you do not have an array but rather a dataframe. The distinction in R is not at all trivial. If this is a dataframe and you are only woring with one column then this might work (assuming that thememetsCopy alreadyexists) : mesmentsCopy$V1 - 100*quantile(mesments$V1, (1:100)/100) All untested. You were asked in the Posting Guide to present a means of creatine data that has the same structure. Using dput is a good way of presneting htat structure in ascii form. would become: mesmentsCopy$V1: 50, 100, 0 The actual array has many more rows and columns, of course. Cheers, Greg Allen Freelance Techno-Slave SLC, Utah __ 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. David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replace array with percentile values
rank() would do it: x - c(1,2,2,2,2,3,4,4,5,5) rank(x, ties=max)/length(x) * 100 [1] 10 50 50 50 50 60 80 80 100 100 as would ecdf() ecdf(x)(x)*100 [1] 10 50 50 50 50 60 80 80 100 100 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of greggal...@gmail.com Sent: Sunday, November 04, 2012 7:22 PM To: r-help@r-project.org Subject: [R] Replace array with percentile values Hi: I have an array of measurements, that I've been doing linear regression model and AI models on. Because there are many errors and the values are ill-formed I would like to copy the array, but replace each value with the PERCENTILE of that value, in the original array. i.e. mesments$V1: 9, 77, -1 would become: mesmentsCopy$V1: 50, 100, 0 The actual array has many more rows and columns, of course. Cheers, Greg Allen Freelance Techno-Slave SLC, Utah __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Having some Trouble Data Structures
Please keep mail threads on the mailing list. Please follow the posting guidelines and provide a sample of data and desired outcome. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Benjamin Ward (ENV) b.w...@uea.ac.uk wrote: Hi, Thank you very much for your reply - how you prefer, is how my supervisor implemented the layout in Minitab, however I was unsure of how to get R to do this repeating ID behaviour and how to know that in a for loop going through individual 1 to say 10, I want it to: Randomly sample a number from a distribution for the number of effectors (I can do this but with runif), Then put one value in a cell of the Effector column and repeat the ID for each effector row. I'm also then left wondering when I do for loops then that use ID, will it go and apply operations row by row, or ID by ID - for example in the immunology part I would need a loop to check individual by individual if any of the effectors it has means death in the host, in which case all instances of - say ID 1 would need to be deleted. Would you be able to provide an example chunk of how you accomplish this with your preferred approach, if you have the time? Thanks, Ben W. From: Jeff Newmiller [jdnew...@dcn.davis.ca.us] Sent: 28 October 2012 15:27 To: Benjamin Ward (ENV); r-help@r-project.org Subject: Re: [R] Having some Trouble Data Structures Search on ragged array. My preferred approach is to use a data frame with one row per effector that repeats the per-ID information. If that occupies too much memory, you can setup another data frame with one row per ID and refer to that information as using lapply and subset the effectors data as needed. The plyr package is also useful for such processing. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Benjamin Ward (ENV) b.w...@uea.ac.uk wrote: Hi All, I'm trying to run a simulation of host-pathogen evolution based around individuals. What I need to have is a dataframe or table of some description - describing all the individuals of a pathogen population (so far I've implemented this as a matrix): ID No_of_Effectors Effectors (Sequences) [1,] 0001 3 ## 3 Random Numbers ## There will be many such rows for many individuals. They have something called effectors, the number of which is randomly generated, so say you get 3 in the No_of_Effectors column. Then I make R generate 3 numbers from between 1 and 10,000, this gives me three numerical representations of genes. These numbers will be compared to a similar data structure of the host individuals who have their immune genes with similar numbers. My problem is that obviously I can't stick 3 numbers in one cell of the matrix (I've tried) : Pathogen_Individuals[1,3] - c(2,3,4) Error in Pathogen_Individuals[1, 3] - c(345, 567, 678) : number of items to replace is not a multiple of replacement length In future I'm also going to have more variables such as whether a gene is expressed. Such information may require a matrix in itself - something like: Effector ID Sequence Expressed? [1,] 0001 345,567,678 1 (or 0). Is there a way then I can put more than one value in the cell like a list of values, or a way to put objects in a cell of a data frame, matrix or table etc. Almost an inception deal - data structures nested in a data structure? If I search for things like insert list into matrix I get results like how to turn one into another, which is not what I think I need to be doing. I have been considering having several data structures not nested in each other, something like for every individual create a new matrix object with the name Effectors_[Individual_ID] and some how get my simulation loops operating on those objects but I find it hard to see how to tell R all of those matrices are to be included in an
Re: [R] Strange behaviour of setwd/getwd
Thanks David, that's exactly what I needed. 2012/11/2 David L Carlson dcarl...@tamu.edu Aren't you just looking for this? default.wd - setwd(tmp.wd - choose.dir()) -- David L Carlson Associate Professor of Anthropology Texas AM University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Berend Hasselman Sent: Friday, November 02, 2012 3:36 PM To: Markus Holotta Cc: r-help@r-project.org Subject: Re: [R] Strange behaviour of setwd/getwd On 02-11-2012, at 21:02, Markus Holotta wrote: Maybe my question was not clear enough: default.wd is the working dir defined in the RStudio options. In my script I want to change it temporarily to another directory by setwd(choose.dir()) and set it back to the default.wd before calling another script. But after choosing the new directory tmp.wd shows the same path in the workspace as default.wd. Calling getwd() shows the correct path. Hope its clearer now. No. Read the help for setwd(). setwd returns the current directory before the change, invisibly and with the same conventions as getwd. It will give an error if it does not succeed (including if it is not implemented). The essential bit is before the change. Berend Am 02.11.2012, 19:14 Uhr, schrieb Duncan Murdoch murdoch.dun...@gmail.com: On 02/11/2012 12:57 PM, Markus Holotta wrote: I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem? It's not clear what the problem is from your post. As the help page says, both default.wd and tmp.wd should be the same after executing those two lines. If you want to store both the old and new directories, you should do it like this: old.wd - setwd(choose.dir()) new.wd - getwd() Duncan Murdoch __ 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. [[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] slider control questions
DeaR UseRs, I have the following code (see below). It is working as expected although I have two problems/questions: - how can I set the size of the graph? It may sound silly, but I couldnt figure that out - is it possible to export this small interactive aplication to html or a latex document, or maybe pdf? Thank you for your attentions. Best wishes: Daniel the code: library(tkrplot) library(relax) dirfelulet - function (a1 = 1, a2 = 1, a3 = 1){ x1 - x2 - seq(0, 1, by=.01) dirf - function(x1, x2){ term1 - gamma(a1+a2+a3)/(gamma(a1)*gamma(a2)*gamma(a3)) term2 - x1^(a1-1)*x2^(a2-1)*(1-x1-x2)^(a3-1) term3 - (x1 + x2 1) term1 * term2 * term3 } f - outer(x1, x2, dirf) f[f=0] - NA f[is.infinite(f)] - NA persp(x1, x2, f, zlim = c(0, max(f, na.rm = TRUE)+1), main = bquote(paste(Dirichlet eloszlás, , alpha ,=(,.(a1),,,.(a2),,,.(a3),))), col = lightblue, theta = 50, phi = 20, r = 50, d = 0.1, expand = 0.5, ltheta = 90, lphi = 180, shade = 0.75, ticktype = detailed, nticks = 5) } plot.dirichlet-function(){ refresh.code-function(...){ a1-slider(no=1); a2-slider(no=2); a3-slider(no=3) type= slider(obj.name=type) dirfelulet(a1,a2,a3) } slider(obj.name=type,obj.value=l) gslider(refresh.code,sl.names=c(a1,a2,a3), sl.mins=c(1,1,1),sl.maxs=c(10,10,10),sl.deltas=c(.1,.1,.1),sl.defaults=c(1,1,1)) } plot.dirichlet() [[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] Hypothesis RayosRayos.2 = (Intercept) is not well formed: contains bad coefficient/variable names.
Hi R-listers, I am trying to do this function amongst others for a Chi-sq test in require(multcomp) for some reason it is not working but all of the other functions have work. What does this error mean and how do I fix it? Jean linearHypothesis(model.rayos.C, RayosRayos.2 = (Intercept)) Error in constants(lhs, cnames_symb) : The hypothesis RayosRayos.2 = (Intercept) is not well formed: contains bad coefficient/variable names. In addition: Warning message: In constants(lhs, cnames_symb) : NAs introduced by coercion __ 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] Dates as POSIXt
When I try to do linear interpolation between financial contracts with maturities on different dates in different months I have come across some behavior I haven't seen before. I have a data frame in R which is loaded from an access database so I can't provide a working example. It was loaded using this code: dbPath - H:/pathToDB/DB.mdb channel - odbcConnectAccess(dbPath) DF = sqlFetch(channel,'nameOfTable') When I look at the Date column I get this result str(DF$Date) POSIXt[1:25311], format: 2003-09-03 06:00:00 2003-09-03 06:00:00 ... I have newer seen data as POSIXt, only as POSIXct or POSIXlt. It is the behavior of this class is that I would like more information about. Online searching have only told me that it is a virtual class. When I do some calculations to get the dates of maturity into the data frame I find this behavior. (For simplicity assume that the only month is March.) DF[,DateOfMaturity] = NA DF[,DateOfMaturityPrevious] = NA DF[,DateOfMaturityNext] = NA maturityFeb = 14 maturityMar = 16 maturityApr = 15 yearTmp = as.POSIXlt(DF$Date)$year+1900 DF$DateOfMaturity = as.POSIXct(strptime(paste(yearTmp,03,maturityMar ), %Y %m %d)) DF$DateOfMaturityPrevious = as.POSIXct(strptime(paste(yearTmp,02,maturityFeb ), %Y %m %d) DF$DateOfMaturityNext = as.POSIXct(strptime(paste(yearTmp,04,maturityApr ), %Y %m %d)) which works fine and gives me the dates I want but it is not readable with human eyes. When I try DF$DateOfMaturity = as.POSIXlt(strptime(paste(yearTmp,03,maturityMar ), %Y %m %d)) DF$DateOfMaturityPrevious = as.POSIXlt(strptime(paste(yearTmp,02,maturityFeb ), %Y %m %d) DF$DateOfMaturityNext = as.POSIXlt(strptime(paste(yearTmp,04,maturityApr ), %Y %m %d)) it breaks my DF str(DF$DateOfMaturity) List of 2015 $ : num [1:2015] 0 0 0 0 0 0 0 0 0 0 ... $ : int [1:2015] 0 0 0 0 0 0 0 0 0 0 ... $ : int [1:2015] 0 0 0 0 0 0 0 0 0 0 ... $ : int [1:2015] 16 16 16 16 16 16 16 16 16 16 ... $ : int [1:2015] 0 0 0 0 0 0 0 0 0 0 ... $ : int [1:2015] 104 104 104 104 104 104 104 104 104 104 ... $ : int [1:2015] 5 5 5 5 5 5 5 5 5 5 ... $ : int [1:2015] 15 15 15 15 15 15 15 15 15 15 . . . [list output truncated] Now I wonder why I can't use POSIXlt in my data frame (I know I shouldn't but that is not the question) and if I can use POSIXt like the original data? It is human readable but also suited for calculation (e.g. DF$Date as.POSIXct(2005-12-01) works nicely. Best regards Daniel Haugstvedt Ph.D. student NTNU, Trondheim, Norway __ 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.