[R] From Java to R OOP
Hi, I'm new to OOP in R so please forgive the naiveness of some of the questions. Here are a couple of them. It would be great if you can contrast to OOP in Java. 1. R's S4 appears to centered around a dispatch mechanism which in my understanding is just a way to implement polymorphism. Now, here's the snag, I thought polymorphism was an aspect of OOP not by itself the definition of OOP. What am I missing here? Is any language that implements polymorphism automatically OO? 2. Can someone provide a simple example of how NextMethod() works? I read some things about but I can't make any sense out of it. It's supposed to facilitate inheritance but how? Why is it needed, what happens if it's ignored? An example would be useful. Is there a Java equivalent of NextMethod()? Many Thanks! __ 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] Columns perfectly aligned with the column names
Dear R users, I need to export a data frame, and I would like to have all the columns perfectly aligned with the column names. At the time being, the output is not satisfactory. Here below there is a real example, with the R commands that I am using. Is it possible? How can I do? place MDI MPI MOI mDO mPO mOO MDO MPO mDD mPD MDD MPD mDDDmPDDMDDDMPDD Urbino 9 9 10 4 5 6 3 4 1 3 8 8 3 4 9 9 Jesi 12 13 14 7 7 9 4 7 3 4 7 9 5 5 10 11 Pesaro 10 13 14 7 6 9 5 9 3 2 6 10 7 6 8 12 Camerino 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 Ascoli 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 SBenedetto 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 place - c(Urbino, Jesi, Pesaro, Camerino, Ascoli, SBenedetto, MteMonaco, PSElpidio, Ancona, Osimo, Montefano, Fabriano, Senigallia, Macerata) MAIL - data.frame(place=place, MDI=max_dmo_ieri, MPI=max_prev_ieri, MOI=max_oss_ieri, mDO=min_dmo_oggi, mPO=min_prev_oggi, mOO=min_oss_oggi, MDO=max_dmo_oggi, MPO=max_prev_oggi, mDD=min_dmo_domani, mPD=min_prev_domani, MDD=max_dmo_domani, MPD=max_prev_domani, mDDD=min_dmo_dopodomani, mPDD=min_prev_dopodomani, MDDD=max_dmo_dopodomani, MPDD=max_prev_dopodomani) write.table(MAIL, file=/home/meteo/KALMAN_DEV2/output/MAIL.txt, sep = \t, row.names=FALSE, col.names = TRUE, quote=FALSE, qmethod=double) Thank you for your help Stefano Sofia AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario. IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. __ 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] From Java to R OOP
On Mon, Mar 25, 2013 at 6:51 AM, Francisco J. Bido b...@mac.com wrote: Hi, I'm new to OOP in R so please forgive the naiveness of some of the questions. Here are a couple of them. It would be great if you can contrast to OOP in Java. Java is not the end-all of OOP (in fact S is a good bit older than Java) and you might find that the Lisp or Dylan object systems are a better analogy. (I'm only going by hearsay on Dylan; never used it myself) You might also quickly breeze through: https://github.com/hadley/devtools/wiki/S3 1. R's S4 appears to centered around a dispatch mechanism which in my understanding is just a way to implement polymorphism. Now, here's the snag, I thought polymorphism was an aspect of OOP not by itself the definition of OOP. What am I missing here? Is any language that implements polymorphism automatically OO? If you accept the immutability of objects, then arguably yes, I suppose polymorphism gives you a great deal of it. The remaining weaknesses are generally addressed by the S4 object system. (Not immutability of bindings like Haskell, but the fact that x - y - 1:5; y[3] - 10 won't change x. In theory this is done by creating a new y with the modified 3rd element and binding the name y to that; not entirely thus in practice for performance reasons ) 2. Can someone provide a simple example of how NextMethod() works? I read some things about but I can't make any sense out of it. It's supposed to facilitate inheritance but how? Why is it needed, what happens if it's ignored? An example would be useful. Is there a Java equivalent of NextMethod()? Grepping through R's source, it seems that the print system uses a fair amount of NextMethod for the AsIs and noquote print methods. You might take a look at those: also, section 7 of http://cran.r-project.org/doc/manuals/r-devel/R-exts.html MW Many Thanks! __ 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] Automatic script for updating packages in R
Hello all,Good day,Internet access have been a problem , and learning R forced to download packages manual.I have google for script for automatic R update didnt getPlease any one with help?am using ubuntu 12.04R-2.5.3Thank you [[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] Columns perfectly aligned with the column names
Hi Your code is not reproducible but it probably does not matter. You use tab as separator which means the file is readable directly by spreadsheet programmes (Excel). Reading them as text shall be OK unless some values are not so long that they exceed predefined tabs or unless they have some leading spaces which are unseen but which cause unaligning. You can either read the file in Excel directly or to use some text formating tools (sprintf, formatC, ...) If you read it to Word, you also can change text to table whoch results in aligned values. Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Stefano Sofia Sent: Monday, March 25, 2013 10:24 AM To: r-help@r-project.org Subject: [R] Columns perfectly aligned with the column names Dear R users, I need to export a data frame, and I would like to have all the columns perfectly aligned with the column names. At the time being, the output is not satisfactory. Here below there is a real example, with the R commands that I am using. Is it possible? How can I do? place MDI MPI MOI mDO mPO mOO MDO MPO mDD mPD MDD MPD mDDDmPDDMDDDMPDD Urbino 9 9 10 4 5 6 3 4 1 3 8 8 3 4 9 9 Jesi 12 13 14 7 7 9 4 7 3 4 7 9 5 5 10 11 Pesaro 10 13 14 7 6 9 5 9 3 2 6 10 7 6 8 12 Camerino 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 Ascoli 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 SBenedetto 12 12 11 5 6 8 4 3 1 2 10 10 2 3 8 8 place - c(Urbino, Jesi, Pesaro, Camerino, Ascoli, SBenedetto, MteMonaco, PSElpidio, Ancona, Osimo, Montefano, Fabriano, Senigallia, Macerata) MAIL - data.frame(place=place, MDI=max_dmo_ieri, MPI=max_prev_ieri, MOI=max_oss_ieri, mDO=min_dmo_oggi, mPO=min_prev_oggi, mOO=min_oss_oggi, MDO=max_dmo_oggi, MPO=max_prev_oggi, mDD=min_dmo_domani, mPD=min_prev_domani, MDD=max_dmo_domani, MPD=max_prev_domani, mDDD=min_dmo_dopodomani, mPDD=min_prev_dopodomani, MDDD=max_dmo_dopodomani, MPDD=max_prev_dopodomani) write.table(MAIL, file=/home/meteo/KALMAN_DEV2/output/MAIL.txt, sep = \t, row.names=FALSE, col.names = TRUE, quote=FALSE, qmethod=double) Thank you for your help Stefano Sofia AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario. IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. __ 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] Automatic script for updating packages in R
Hi Strange, I thought that only Windows users are sending Html mail and providing sparse info describing their problems. I presume, you use 2.15.3 R version. There is some readme in Ubuntu CRAN repository and if you followed that and fail it would be necessary to provide at least what you did (some code) and what was the error messages. http://cran.r-project.org/bin/linux/ubuntu/README Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Twaha Mlwilo Sent: Monday, March 25, 2013 11:21 AM To: r-h...@stat.math.ethz.ch Subject: [R] Automatic script for updating packages in R Hello all,Good day,Internet access have been a problem , and learning R forced to download packages manual.I have google for script for automatic R update didnt getPlease any one with help?am using ubuntu 12.04R-2.5.3Thank you [[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.
[R] How to use RWeka with Multilayer Perceptron?
Hello, I have a serialized Multilayer Perceptron trained using weka. I would like to use R to re-evaluate the model. How can I do this? I could not find an example of RWeca that applies to Multilayer Perceptron. Thanks, Rui __ 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] plot Raster
Dear all, I am trying to plot an image so I am trying this through raster layer. You can copy paste the following require('raster') Data-matrix(data=rnorm(900,80,20),nrow=30,ncol=30) rasterData-raster(Data) lengthOut-5 xAxisFrequencies-seq(800,900,length.out=lengthOut) plot(rasterData, ylab=,xaxt=n,yaxt=n) axis(1, at=seq(0,1,length.out=lengthOut), xAxisFrequencies, col.axis = blue) What I want is to add a customized x label, but the last line I gave above axis(1,at.) does not work. It would be also nice to add a bit of legend also under the color bar to explain there what are the values. Could you please help me with those two ? Regards Alex [[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] How to copy current line in Tinn-R
Hello All, A very simple question about Tinn-R. I am able to use the send line shortcut, but I want to be able to just copy the current line to the clipboard and then paste in the current document or somewhere else. It's so tedious to select the whole line and then copy it. Thanks __ 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] Weighted Kaplan-Meier estimates with R (with confidence intervals)?
As part of a research paper, I would like to draw both weighted and unweighted Kaplan-Meier estimates, the weight being the ’importance’ of the each project to the mass of projects whose survival I’m trying to estimate. I know that the function survfit in the package survival accepts weights and produces confidence intervals. However, I suspect that the confidence intervals may not be correct. The reason why I suspect this is that depending on how I define the weights, I get very different confidence intervals, e.g. require(survival) s - Surv(c(50,100),c(1,1)) sf - survfit(s~1,weights=c(1,2)) plot(sf) vs. require(survival) s - Surv(c(50,100),c(1,1)) sf - survfit(s~1,weights=c(100,200)) plot(sf) Any suggestions would be more than welcome! -- View this message in context: http://r.789695.n4.nabble.com/Weighted-Kaplan-Meier-estimates-with-R-with-confidence-intervals-tp4662360.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] cursor after txtProgressBar
Hi there, I am using R 2.15.2 under Win7 64. I hope to display progress bar when I am running a loop. After the loop exit, the cursor is at the right margin of the window. How can I make it to the left margin? Here is the minimal example code. combn.bar - function(n, m) { n - choose(n, m) pb - txtProgressBar(1,n,style = 3) for (i in 1:n) { Sys.sleep(0.1) setTxtProgressBar(pb, i) } close(pb) } combn.bar(6,3) I try to move the cursor to the left margin using cat(\r). However, after that, I can not move the cursor to left if I try to edit a history command. It seems a problem that only related with Rgui. I try the above example in Rterm, it works well. Any help will be really appreciated. Regards, Jinsong __ 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] Need help to plot clara results [clustering]
capricy gao capri...@yahoo.com on Thu, 21 Mar 2013 06:07:12 -0700 writes: I am going to use clara for� gene expression analysis, so tried to play around with the examples from R document: http://127.0.0.1:10699/library/cluster/html/clara.html Everything looked fine until I tried to plot the results: it says: waiting to confirm page change... I waited for more than 10 min and NO plot came out... :-) :-) I really had a great chuckle!! *Who* do you think is waiting when R says waiting to confirm page change... ?? Of course, R is waiting for *you* to confirm the page change... Should I wait longer? Anything wrong like this? :-) ;-) ... I can hardly stop chuckling away... sorry ... Thanks for any input:) You're welcome. Martin __ 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 does this mean
Hi I was working with a script and I came across this %*%. What does this mean? Thanks -- Shane [[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] %*% what does this mean
Matrix Multiplication ?%*% -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Shane Carey Sent: Montag, 25. März 2013 13:31 To: r-help@r-project.org Subject: [R] %*% what does this mean Hi I was working with a script and I came across this %*%. What does this mean? Thanks -- Shane [[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] %*% what does this mean
On 13-03-25 8:31 AM, Shane Carey wrote: Hi I was working with a script and I came across this %*%. What does this mean? Thanks help(%*%) will tell you. 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] Weighted Kaplan-Meier estimates with R (with confidenceintervals)?
The two confidence intervals should be different. In the first model you have 3 failures and the second one you have 300. More failures results in narrower confidence intervals. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of rm Sent: Montag, 25. März 2013 10:47 To: r-help@r-project.org Subject: [R] Weighted Kaplan-Meier estimates with R (with confidenceintervals)? As part of a research paper, I would like to draw both weighted and unweighted Kaplan-Meier estimates, the weight being the ’importance’ of the each project to the mass of projects whose survival I’m trying to estimate. I know that the function survfit in the package survival accepts weights and produces confidence intervals. However, I suspect that the confidence intervals may not be correct. The reason why I suspect this is that depending on how I define the weights, I get very different confidence intervals, e.g. require(survival) s - Surv(c(50,100),c(1,1)) sf - survfit(s~1,weights=c(1,2)) plot(sf) vs. require(survival) s - Surv(c(50,100),c(1,1)) sf - survfit(s~1,weights=c(100,200)) plot(sf) Any suggestions would be more than welcome! -- View this message in context: http://r.789695.n4.nabble.com/Weighted-Kaplan-Meier-estimates-with-R-with-confidence-intervals-tp4662360.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] Automatic script for updating packages in R
Hi, you might also want to check out some nice helpers of the opencpu and opencpu.tools packages made by Jeroen Ooms: https://raw.github.com/jeroenooms/opencpu.tools/master/R/install.all.packages.R Best, Gergely On 25 March 2013 11:21, Twaha Mlwilo uddessy2...@hotmail.com wrote: Hello all,Good day,Internet access have been a problem , and learning R forced to download packages manual.I have google for script for automatic R update didnt getPlease any one with help?am using ubuntu 12.04 R-2.5.3Thank you [[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] Help on dendrogram reorder
On Fri, Mar 22, 2013 at 1:48 PM, capricy gao capri...@yahoo.com wrote: as.dendrogram ?reorder.dendrogram Jean [[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] Automatic script for updating packages in R
try, update.packages(ask='graphics', checkBuilt=TRUE) ?update.packages ?download.file ?url # file:// URLs On 3/25/2013 6:09 AM, PIKAL Petr wrote: Hi Strange, I thought that only Windows users are sending Html mail and providing sparse info describing their problems. I presume, you use 2.15.3 R version. There is some readme in Ubuntu CRAN repository and if you followed that and fail it would be necessary to provide at least what you did (some code) and what was the error messages. http://cran.r-project.org/bin/linux/ubuntu/README Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Twaha Mlwilo Sent: Monday, March 25, 2013 11:21 AM To: r-h...@stat.math.ethz.ch Subject: [R] Automatic script for updating packages in R Hello all,Good day,Internet access have been a problem , and learning R forced to download packages manual.I have google for script for automatic R update didnt getPlease any one with help?am using ubuntu 12.04R-2.5.3Thank you [[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. -- Robert W. Baer, Ph.D. Professor of Physiology Kirksille College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 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] Double condition
Hi, To Mason: I like your idea with reshaping the dataframe. I've read the paper and checked the help for the cast function, but I wasn't able to reshape it to wanted form, which as you mentioned would be (column names): jul timedtime ddawn.noon ddawn.midnight ddusk.noon ddusk.midnight And maybe it would be easier to do with this dataframe (the basic one which later I use to create other dataframes, the one mentioned above as well), I'm not sure: LOC[ 21:30,] jul fix dawn dusk lat long 21 14664 midnight 07:49 20:00 15.92 -25.30 22 14664 noon 07:49 19:50 24.04 -24.07 23 14665 midnight NA NANA NA 24 14665 noon NA NANA NA 25 14666 midnight NA NANA NA 26 14666 noon 07:48 19:55 21.19 -24.65 27 14667 midnight 07:51 19:55 24.32 -25.05 28 14667 noon 07:51 20:00 20.43 -25.69 29 14668 midnight 07:49 20:00 19.08 -25.47 30 14668 noon 07:49 19:53 26.24 -24.61 dput(LOC[ 21:30,]) structure(list(jul = c(14664, 14664, 14665, 14665, 14666, 14666, 14667, 14667, 14668, 14668), fix = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c(midnight, noon ), class = factor), dawn = structure(c(38L, 38L, NA, NA, NA, 37L, 40L, 40L, 38L, 38L), .Label = c(07:12, 07:13, 07:14, 07:15, 07:16, 07:17, 07:18, 07:19, 07:20, 07:21, 07:22, 07:23, 07:24, 07:25, 07:26, 07:27, 07:28, 07:29, 07:30, 07:31, 07:32, 07:33, 07:34, 07:35, 07:36, 07:37, 07:38, 07:39, 07:40, 07:41, 07:42, 07:43, 07:44, 07:45, 07:46, 07:47, 07:48, 07:49, 07:50, 07:51, 07:52, 07:53, 07:54, 07:55, 07:56, 07:57, 07:58, 07:59, 08:00, 08:01, 08:02, 08:03, 08:04, 08:05, 08:06, 08:07, 08:08, 08:11, 08:12, 08:13, 08:15, 08:16, 08:17, 08:18, 08:19, 08:20, 08:21, 08:22, 08:23, 08:25, 08:27, 08:28, 08:29, 08:30, 08:31, 08:32, 08:35, 08:41), class = factor), dusk = structure(c(55L, 45L, NA, NA, NA, 50L, 50L, 55L, 55L, 48L), .Label = c(18:53, 19:00, 19:01, 19:03, 19:05, 19:07, 19:08, 19:09, 19:10, 19:12, 19:13, 19:14, 19:15, 19:16, 19:17, 19:19, 19:20, 19:21, 19:22, 19:23, 19:24, 19:25, 19:26, 19:28, 19:29, 19:30, 19:32, 19:33, 19:34, 19:35, 19:36, 19:37, 19:38, 19:39, 19:40, 19:41, 19:42, 19:43, 19:44, 19:45, 19:46, 19:47, 19:48, 19:49, 19:50, 19:51, 19:52, 19:53, 19:54, 19:55, 19:56, 19:57, 19:58, 19:59, 20:00, 20:01, 20:02, 20:03, 20:04, 20:05, 20:06, 20:07, 20:08, 20:09, 20:10, 20:11, 20:13, 20:15, 20:20, 20:25, 20:26, 20:29, 20:30, 20:31, 20:35, 20:36, 20:38, 20:40, 20:41, 20:43, 20:44, 20:45, 20:46, 20:47, 20:48, 20:49, 20:50, 20:51, 20:52, 20:54, 20:55, 20:56, 20:58, 21:00, 21:01, 21:02, 21:03, 21:04, 21:05, 21:06, 21:07, 21:08, 21:09, 21:10, 21:12, 21:13, 21:14, 21:15, 21:16, 21:17, 21:18, 21:19, 21:20, 21:21, 21:22, 21:23, 21:24, 21:25, 21:26, 21:27, 21:28, 21:30, 21:31, 21:34 ), class = factor), lat = c(15.92, 24.04, NA, NA, NA, 21.19, 24.32, 20.43, 19.08, 26.24), long = c(-25.3, -24.07, NA, NA, NA, -24.65, -25.05, -25.69, -25.47, -24.61)), .Names = c(jul, fix, dawn, dusk, lat, long), row.names = 21:30, class = data.frame) Could you, please, help me with that? The wanted is dataframe with these columns: jul ddawn.noon ddawn.midnight ddusk.noon ddusk.midnight lat.noon lat.midnight long.noon long.midnight Thanks, Zuzana On 22 March 2013 19:44, Mason ma...@verbasoftware.com wrote: It sounds like, although your noon and midnight data are separate rows, they are not fully independent. If I understand correctly, the operation you want to perform would be simple if you had (at least temporarily) a single row with columns ddawn.midnight, ddusk.midnight, ddawn.noon, ddusk.noon, rather than two separate rows. I recommend you check out the reshape package http://had.co.nz/reshape/, and read the paper Hadley wrote about it for a conceptual understanding of wide vs. long data. On Fri, Mar 22, 2013 at 11:18 AM, zuzana zajkova zuzu...@gmail.comwrote: Hi, I would appreciate if somebody could help me with this small issue... I have a dataframe like this (originaly has more than 100 000 rows): subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 0 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 0 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 0 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 dput(subz)
Re: [R] Weighted Kaplan-Meier estimates with R (with confidenceintervals)?
Say, that I have two observations, one from time 0 to time 50, and a second from time 0 to time 100, both of which are known to have failed, i.e. no censoring. I would like to give double the weight to the second observation. This is what I’ve tried to implement in the both pieces of code. Both pieces of code give the same survival curve but different confidence intervals. Why? How should I fix the code to get the “correct” confidence intervals? -- View this message in context: http://r.789695.n4.nabble.com/Weighted-Kaplan-Meier-estimates-with-R-with-confidence-intervals-tp4662360p4662384.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] Double condition
Hi Rui, thank you for your code, but unfortunately it doesn't work correctly. What I got is this: subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 1 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 1 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 1 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 Where day for time 19:54:49 for midnight is 1 and for noon is 0. There are supposed to be 0 both (as dtime 19.91361 ddusk for noon 19.88333) Probably the problem would be adding 1 to he index in subz$day[idx + 1] - subz$day[idx] So far, I haven't found solution... Zuzana On 22 March 2013 20:01, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. idx - which(subz$fix == noon) if(idx[length(idx)] == nrow(subz)) idx - idx[-length(idx)] subz$day[idx + 1] - subz$day[idx] Hope this helps, Rui Barradas Em 22-03-2013 18:18, zuzana zajkova escreveu: Hi, I would appreciate if somebody could help me with this small issue... I have a dataframe like this (originaly has more than 100 000 rows): subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 0 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 0 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 0 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 dput(subz) structure(list(jul = c(15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006), time = structure(c(1296587689, 1296588289, 1296588289, 129659, 129659, 1296589489, 1296589489, 1296590089, 1296590089, 1296590689, 1296590689), class = c(POSIXct, POSIXt), tzone = GMT), dtime = c(19.24694, 19.41361, 19.41361, 19.58028, 19.58028, 19.74694, 19.74694, 19.91361, 19.91361, 20.08028, 20.08028), fix = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c(midnight, noon), class = factor), ddawn = c(7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667 ), ddusk = c(19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333 ), day = c(1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0)), .Names = c(jul, time, dtime, fix, ddawn, ddusk, day), row.names = 101608:101618, class = data.frame) where day is calculated as subz$day - ifelse( subz$dtime subz$ddusk | subz$dtime subz$ddawn, 0, 1 ) The way I would like to calculate day is this - for the same time, the day is calculated for noon as mentioned above but for midnight is just copying the same value as for noon. So for the same time the day value should be the same for noon and midnight. Something like this: jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 1 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 1 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 0 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0
Re: [R] Double condition
Hello, I believe the following solves your problem. It's a bit more complicated but with the sample dataset you've provided the result is as wished. tmp - lapply(split(subz, subz$time), function(x) { i1 - which(as.character(x$fix) == noon)[1] i2 - which(as.character(x$fix) == midnight) x$day[i2] - x$day[i1] x}) names(tmp) - NULL result - do.call(rbind, tmp) Hope this helps, Rui Barradas Em 25-03-2013 14:15, zuzana zajkova escreveu: Hi Rui, thank you for your code, but unfortunately it doesn't work correctly. What I got is this: subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 1 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 1 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 1 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 Where day for time 19:54:49 for midnight is 1 and for noon is 0. There are supposed to be 0 both (as dtime 19.91361 ddusk for noon 19.88333) Probably the problem would be adding 1 to he index in subz$day[idx + 1] - subz$day[idx] So far, I haven't found solution... Zuzana On 22 March 2013 20:01, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. idx - which(subz$fix == noon) if(idx[length(idx)] == nrow(subz)) idx - idx[-length(idx)] subz$day[idx + 1] - subz$day[idx] Hope this helps, Rui Barradas Em 22-03-2013 18:18, zuzana zajkova escreveu: Hi, I would appreciate if somebody could help me with this small issue... I have a dataframe like this (originaly has more than 100 000 rows): subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 0 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 0 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 0 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 dput(subz) structure(list(jul = c(15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006), time = structure(c(1296587689, 1296588289, 1296588289, 129659, 129659, 1296589489, 1296589489, 1296590089, 1296590089, 1296590689, 1296590689), class = c(POSIXct, POSIXt), tzone = GMT), dtime = c(19.24694, 19.41361, 19.41361, 19.58028, 19.58028, 19.74694, 19.74694, 19.91361, 19.91361, 20.08028, 20.08028), fix = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c(midnight, noon), class = factor), ddawn = c(7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667 ), ddusk = c(19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333 ), day = c(1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0)), .Names = c(jul, time, dtime, fix, ddawn, ddusk, day), row.names = 101608:101618, class = data.frame) where day is calculated as subz$day - ifelse( subz$dtime subz$ddusk | subz$dtime subz$ddawn, 0, 1 ) The way I would like to calculate day is this - for the same time, the day is calculated for noon as mentioned above but for midnight is just copying the same value as for noon. So for the same time the day value should be the same for noon and midnight. Something like this: jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611
Re: [R] Weighted Kaplan-Meier estimates with R (with confidenceintervals)?
Le lundi 25 mars 2013 à 05:55 -0700, rm a écrit : Say, that I have two observations, one from time 0 to time 50, and a second from time 0 to time 100, both of which are known to have failed, i.e. no censoring. I would like to give double the weight to the second observation. This is what I’ve tried to implement in the both pieces of code. Both pieces of code give the same survival curve but different confidence intervals. Why? How should I fix the code to get the “correct” confidence intervals? If the weights you cant to use are sampling weights (as I suspect), use the function survfitkm() from the survey package. Regards -- View this message in context: http://r.789695.n4.nabble.com/Weighted-Kaplan-Meier-estimates-with-R-with-confidence-intervals-tp4662360p4662384.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] Obtaining the internal integer codes of a factor XXXX
Hi everyone, I understand the process of obtaining the internal integer codes for the raw values of a factor (using as.numeric() as below), but what is the best way to obtain these codes for the levels() of a factor (since the as.numeric() results don't really make clear which code maps to which level)? fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) fdata levels(fdata) as.numeric(fdata) I thought something like this would make sense and work, but it throws an error: as.numeric(levels(fdata)) Thanks! Dan __ 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] count NAs with aggregate
Dear members of this list, I'd like to count missing values using the aggregate function. Something like this: count_nas - function(arg1) { return(sum(is.na(arg1))) } aggregate(cbind(var1, var2, var3) ~ subject + time, data = mydataset, count_nas) It's not working: I end up with a matrix containing zeros, although there are missings in the data frame. I'd highly appreciate a hint! Thanks a lot, Timo Stolz __ 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] Obtaining the internal integer codes of a factor XXXX
Why do you think you need to do this? (Feel free to ignore, however). -- Bert On Mon, Mar 25, 2013 at 8:37 AM, Dan Abner dan.abne...@gmail.com wrote: Hi everyone, I understand the process of obtaining the internal integer codes for the raw values of a factor (using as.numeric() as below), but what is the best way to obtain these codes for the levels() of a factor (since the as.numeric() results don't really make clear which code maps to which level)? fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) fdata levels(fdata) as.numeric(fdata) I thought something like this would make sense and work, but it throws an error: as.numeric(levels(fdata)) Thanks! Dan __ 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 [[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] Double condition
Hi, although it takes some 10 minutes to do the calculation (the original dataframe has more than 100 000 rows..), it seems to work ok. Thanks for your time and help, Zuzana On 25 March 2013 15:37, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, I believe the following solves your problem. It's a bit more complicated but with the sample dataset you've provided the result is as wished. tmp - lapply(split(subz, subz$time), function(x) { i1 - which(as.character(x$fix) == noon)[1] i2 - which(as.character(x$fix) == midnight) x$day[i2] - x$day[i1] x}) names(tmp) - NULL result - do.call(rbind, tmp) Hope this helps, Rui Barradas Em 25-03-2013 14:15, zuzana zajkova escreveu: Hi Rui, thank you for your code, but unfortunately it doesn't work correctly. What I got is this: subz jultimedtime fixddawnddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 1 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 1 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 1 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 Where day for time 19:54:49 for midnight is 1 and for noon is 0. There are supposed to be 0 both (as dtime 19.91361 ddusk for noon 19.88333) Probably the problem would be adding 1 to he index in subz$day[idx + 1] - subz$day[idx] So far, I haven't found solution... Zuzana On 22 March 2013 20:01, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. idx - which(subz$fix == noon) if(idx[length(idx)] == nrow(subz)) idx - idx[-length(idx)] subz$day[idx + 1] - subz$day[idx] Hope this helps, Rui Barradas Em 22-03-2013 18:18, zuzana zajkova escreveu: Hi, I would appreciate if somebody could help me with this small issue... I have a dataframe like this (originaly has more than 100 000 rows): subz jultimedtime fixddawn ddusk day 101608 15006 2011-02-01 19:14:49 19.24694 noon 7.916667 19.88333 1 101609 15006 2011-02-01 19:24:49 19.41361 midnight 7.916667 19.56667 1 101610 15006 2011-02-01 19:24:49 19.41361 noon 7.916667 19.88333 1 101611 15006 2011-02-01 19:34:49 19.58028 midnight 7.916667 19.56667 0 101612 15006 2011-02-01 19:34:49 19.58028 noon 7.916667 19.88333 1 101613 15006 2011-02-01 19:44:49 19.74694 midnight 7.916667 19.56667 0 101614 15006 2011-02-01 19:44:49 19.74694 noon 7.916667 19.88333 1 101615 15006 2011-02-01 19:54:49 19.91361 midnight 7.916667 19.56667 0 101616 15006 2011-02-01 19:54:49 19.91361 noon 7.916667 19.88333 0 101617 15006 2011-02-01 20:04:49 20.08028 midnight 7.916667 19.56667 0 101618 15006 2011-02-01 20:04:49 20.08028 noon 7.916667 19.88333 0 dput(subz) structure(list(jul = c(15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006, 15006), time = structure(c(1296587689, 1296588289, 1296588289, 129659, 129659, 1296589489, 1296589489, 1296590089, 1296590089, 1296590689, 1296590689), class = c(POSIXct, POSIXt), tzone = GMT), dtime = c(19.24694, 19.41361, 19.41361, 19.58028, 19.58028, 19.74694, 19.74694, 19.91361, 19.91361, 20.08028, 20.08028), fix = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c(midnight, noon), class = factor), ddawn = c(7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667, 7.916667 ), ddusk = c(19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333, 19.56667, 19.88333 ), day = c(1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0)), .Names = c(jul, time, dtime, fix, ddawn, ddusk, day), row.names = 101608:101618, class = data.frame) where day is calculated as subz$day - ifelse( subz$dtime subz$ddusk | subz$dtime subz$ddawn, 0, 1 ) The way I would like to calculate day is this - for the same time, the day is calculated for noon as mentioned above but for midnight is just copying the same value as for noon. So for the same time the day value should be the same for noon
Re: [R] Read text file in R
Hi, Try this: con-file(Routallnew.txt) Lines1- readLines(con) close(con) indx-rep(rep(c(TRUE,FALSE),each=2),24) Lines2-Lines1[!grepl([A-Za-z],Lines1)] res-read.table(text=paste(gsub(^\\s+,,Lines2[indx]),gsub(^\\s+,,Lines2[!indx])),sep=,header=FALSE) nm1-unlist(strsplit(gsub(^ +,,paste(Lines1[grepl([A-Za-z],Lines1)][1:2],collapse= )), )) colnames(res)- nm1[nm1!=] head(res) # m1 n1 m n cterm1_P0L cterm1_P0H c11 c12 c1 c2 alpha beta T_error N #1 13 5 17 9 0.7851203 0.6689925 0.03 0.03 0.15 0.15 0.1 0.2 0.3669373 26 #2 13 5 17 9 0.7851203 0.6689925 0.03 0.03 0.15 0.15 0.1 0.2 0.3669373 26 #3 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.20 0.1 0.2 0.4137296 24 #4 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.20 0.1 0.2 0.4137296 24 #5 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.25 0.1 0.2 0.4782406 24 #6 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.25 0.1 0.2 0.4782406 24 # EN BH BL AH AL #1 20.18355 0.07718537 0.1865207 0.08079875 0.02243240 #2 20.18355 0.07718537 0.1865207 0.08079875 0.02243240 #3 18.55295 0.08482219 0.1996013 0.09569044 0.03361565 #4 18.55295 0.08482219 0.1996013 0.09569044 0.03361565 #5 18.55295 0.19596330 0.1996013 0.04906038 0.03361565 #6 18.55295 0.19596330 0.1996013 0.04906038 0.03361565 A.K. From: Joanna Zhang zjoanna2...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 25, 2013 11:19 AM Subject: Read text file in R Hi Arun, I just sent you a text file via R, but I think it is being held for moderator approval. I attached the final output file here, could you help me read it in R when you have time? __ 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] error installing RcppClassic
Anyone knows what does this error means? library(RcppClassic) Error in gzfile(file, rb) : cannot open the connection I thought I installed the package successfully: install.packages('RcppClassic') Installing package(s) into C:/Program Files/R/library (as lib is unspecified) also installing the dependency Rcpp trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/Rcpp_0.10.2.zip'Content type 'application/zip' length 3882722 bytes (3.7 Mb) opened URL downloaded 3.7 Mb trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/RcppClassic_0.9.3.zip'Content type 'application/zip' length 854312 bytes (834 Kb) opened URL downloaded 834 Kb package Rcpp successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package Rcpp package RcppClassic successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\\AppData\Local\Temp\Rtmp2t8Lm6\downloaded_packages Thanks,Lin [[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] Obtaining the internal integer codes of a factor XXXX
On 25/03/2013 11:37 AM, Dan Abner wrote: Hi everyone, I understand the process of obtaining the internal integer codes for the raw values of a factor (using as.numeric() as below), but what is the best way to obtain these codes for the levels() of a factor (since the as.numeric() results don't really make clear which code maps to which level)? fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) fdata levels(fdata) as.numeric(fdata) I thought something like this would make sense and work, but it throws an error: as.numeric(levels(fdata)) seq_len(length(levels(fdata))) will give you the numeric codes for the levels. They are simply 1:3 in your example above. (Or perhaps I misunderstood your question?) 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] count NAs with aggregate
Hello, The problem seems to be with the formula interface. # Make up some data var1 - rnorm(100) var2 - rnorm(100) var3 - rnorm(100) subject - sample(4, 100, replace = TRUE) time - sample(10, 100, replace = TRUE) var1[sample(100, 10)] - NA var2[sample(100, 10)] - NA var3[sample(100, 10)] - NA mydataset - data.frame(var1, var2, var3, subject, time) count_nas - function(arg1) { return(sum(is.na(arg1))) } aggregate(mydataset[, 1:3], list(mydataset$subject, mydataset$time), FUN = count_nas) Hope this helps, Rui Barradas Em 25-03-2013 15:40, Timo Stolz escreveu: Dear members of this list, I'd like to count missing values using the aggregate function. Something like this: count_nas - function(arg1) { return(sum(is.na(arg1))) } aggregate(cbind(var1, var2, var3) ~ subject + time, data = mydataset, count_nas) It's not working: I end up with a matrix containing zeros, although there are missings in the data frame. I'd highly appreciate a hint! Thanks a lot, Timo Stolz __ 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] Obtaining the internal integer codes of a factor XXXX
Hello, Though I have the same doubt as Bert, the following seems to make more sense. seq_along(levels(fdata)) Hope this helps, Rui Barradas Em 25-03-2013 15:37, Dan Abner escreveu: Hi everyone, I understand the process of obtaining the internal integer codes for the raw values of a factor (using as.numeric() as below), but what is the best way to obtain these codes for the levels() of a factor (since the as.numeric() results don't really make clear which code maps to which level)? fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) fdata levels(fdata) as.numeric(fdata) I thought something like this would make sense and work, but it throws an error: as.numeric(levels(fdata)) Thanks! Dan __ 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] Obtaining the internal integer codes of a factor XXXX
I would like to add that using the labels argument without a levels argument in factor(), fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) is a dangerous way to make your factor. Consider what would happen if the 4th input value were not a but was d or if you were in a locale where the sort order is not a, b, c (this is unlikely in this example, but if you had some capitalized and some not the sort order would be different in the C locale and almost any other locale). Avoid the problem by stating what the input levels are expected to be: fdata - factor( c(b,b,c,a,b,c), levels=c(a,b,c), labels=c(I,II,III)) 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 Rui Barradas Sent: Monday, March 25, 2013 9:38 AM To: Dan Abner Cc: r-help@r-project.org Subject: Re: [R] Obtaining the internal integer codes of a factor Hello, Though I have the same doubt as Bert, the following seems to make more sense. seq_along(levels(fdata)) Hope this helps, Rui Barradas Em 25-03-2013 15:37, Dan Abner escreveu: Hi everyone, I understand the process of obtaining the internal integer codes for the raw values of a factor (using as.numeric() as below), but what is the best way to obtain these codes for the levels() of a factor (since the as.numeric() results don't really make clear which code maps to which level)? fdata-factor(c(b,b,c,a,b,c),labels=c(I,II,III)) fdata levels(fdata) as.numeric(fdata) I thought something like this would make sense and work, but it throws an error: as.numeric(levels(fdata)) Thanks! Dan __ 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-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] Read text file in R
res[res$EN==min(res$EN),] # m1 n1 m n cterm1_P0L cterm1_P0H c11 c12 c1 c2 alpha beta T_error #25 9 4 13 12 0.6302494 0.7565041 0.03 0.07 0.15 0.15 0.1 0.2 0.4403712 #27 9 4 13 12 0.6302494 0.7565041 0.03 0.07 0.15 0.20 0.1 0.2 0.4473437 #33 9 4 13 12 0.6302494 0.7565041 0.05 0.07 0.15 0.15 0.1 0.2 0.4403712 #35 9 4 13 12 0.6302494 0.7565041 0.05 0.07 0.15 0.20 0.1 0.2 0.4473437 N EN BH BL AH AL #25 25 16.42697 0.1201138 0.1933632 0.09321455 0.03367957 #27 25 16.42697 0.1553998 0.1933632 0.06490110 0.03367957 #33 25 16.42697 0.1201138 0.1933632 0.09321455 0.03367957 #35 25 16.42697 0.1553998 0.1933632 0.06490110 0.03367957 A.K. From: Joanna Zhang zjoanna2...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 25, 2013 12:13 PM Subject: Re: Read text file in R Great! When I tried to extract the min of EN, it has an error; opt-con[con$EN==min(con$EN),] opt Error in con$EN : $ operator is invalid for atomic vectors On Mon, Mar 25, 2013 at 11:05 AM, arun smartpink...@yahoo.com wrote: Hi, Try this: con-file(Routallnew.txt) Lines1- readLines(con) close(con) indx-rep(rep(c(TRUE,FALSE),each=2),24) Lines2-Lines1[!grepl([A-Za-z],Lines1)] res-read.table(text=paste(gsub(^\\s+,,Lines2[indx]),gsub(^\\s+,,Lines2[!indx])),sep=,header=FALSE) nm1-unlist(strsplit(gsub(^ +,,paste(Lines1[grepl([A-Za-z],Lines1)][1:2],collapse= )), )) colnames(res)- nm1[nm1!=] head(res) # m1 n1 m n cterm1_P0L cterm1_P0H c11 c12 c1 c2 alpha beta T_error N #1 13 5 17 9 0.7851203 0.6689925 0.03 0.03 0.15 0.15 0.1 0.2 0.3669373 26 #2 13 5 17 9 0.7851203 0.6689925 0.03 0.03 0.15 0.15 0.1 0.2 0.3669373 26 #3 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.20 0.1 0.2 0.4137296 24 #4 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.20 0.1 0.2 0.4137296 24 #5 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.25 0.1 0.2 0.4782406 24 #6 9 5 13 11 0.6302494 0.4876750 0.03 0.03 0.15 0.25 0.1 0.2 0.4782406 24 # EN BH BL AH AL #1 20.18355 0.07718537 0.1865207 0.08079875 0.02243240 #2 20.18355 0.07718537 0.1865207 0.08079875 0.02243240 #3 18.55295 0.08482219 0.1996013 0.09569044 0.03361565 #4 18.55295 0.08482219 0.1996013 0.09569044 0.03361565 #5 18.55295 0.19596330 0.1996013 0.04906038 0.03361565 #6 18.55295 0.19596330 0.1996013 0.04906038 0.03361565 A.K. From: Joanna Zhang zjoanna2...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 25, 2013 11:19 AM Subject: Read text file in R Hi Arun, I just sent you a text file via R, but I think it is being held for moderator approval. I attached the final output file here, could you help me read it in R when you have time? __ 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] Plot Matrix with Data
Hi , I would like to use ggplot2 to plot a matrix as an image. You can copy paste the following Data-matrix(data=rnorm(900,80,20),nrow=30,ncol=30) lengthOut-5 Lengths- 15 library(reshape2) library(ggplot2) tdm - melt(Data) ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value)),levels=seq(0,1,by=0.1)) + labs(x = MHz, y = Threshold, fill = Duty Cycle) + geom_raster(alpha=1) + scale_fill_discrete(h.start=1,breaks=c(20,30,40,50,60,70,80,90),labels=c(20,30,40,50,60,70,80,90),fill=red) + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) # End of code part What I wanted to do is to print the values below 20, with an x color between 20-30, with a y clor between 30-40, with a z color and so on I would not care now for the color palette. Any would do. Could you please help me with that? Regards A [[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] nested 'while' loops
Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x=coln) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { lim-(n-1); S-100; (...) } opdf[[y]][x]-sum; } y-y+1; } x-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 0 8 [[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] nested 'while' loops
Hi, sorry that got sent without the output : Please ignore the aligning in the input, I am re-adding that here as well : 1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00012902334171612422939635201325342732312 3amt:Amet_000219315421218835254372613914212030084 Output: 1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00010.831808022254316NA0.831808022254316 0.8318080222543160.8318080222543160.8318080222543160.831808022254316 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553843amt:Amet_0002NANANANANANANANANANANANANANANANANANANANA4 On Mon, Mar 25, 2013 at 5:43 PM, Sahana Srinivasan sahanasrinivasan...@gmail.com wrote: Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x=coln) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { lim-(n-1); S-100; (...) } opdf[[y]][x]-sum; } y-y+1; } x-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 422939 6 3520 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 91421 20 300 8 [[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] nested 'while' loops
On 25-03-2013, at 18:43, Sahana Srinivasan sahanasrinivasan...@gmail.com wrote: Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x=coln) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { lim-(n-1); S-100; (...) } opdf[[y]][x]-sum; } y-y+1; } x-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 0 8 This does not provide reproducible code or an example. You should also count the opening { and the closing }. 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] nested 'while' loops
while(x=21) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { while(k=lim) { k-k+1; } # while loop for k closes opdf[[y]][x]-sum; } # if statement closes y-y+1; } #while for y closes x-x+1; } #while with x closes Have sent the code with matching brackets after confirming with what I am using. [[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] nested 'while' loops
Your immediate problem is that 'y' is not reset to 1. Easier code to write would be to use 'for' loops rather than 'while' loops. Better still would be to use neither if possible. I suspect that you are in Circle 3 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 25/03/2013 17:43, Sahana Srinivasan wrote: Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x=coln) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { lim-(n-1); S-100; (...) } opdf[[y]][x]-sum; } y-y+1; } x-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 0 8 [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ 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] error installing RcppClassic
On Mar 25, 2013, at 9:09 AM, C Lin wrote: Anyone knows what does this error means? library(RcppClassic) Error in gzfile(file, rb) : cannot open the connection I thought I installed the package successfully: install.packages('RcppClassic') Installing package(s) into ŒC:/Program Files/R/library‚ (as Œlib‚ is unspecified) also installing the dependency ŒRcpp‚ trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/Rcpp_0.10.2.zip'Content type 'application/zip' length 3882722 bytes (3.7 Mb) opened URL downloaded 3.7 Mb trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/RcppClassic_0.9.3.zip'Content type 'application/zip' length 854312 bytes (834 Kb) opened URL downloaded 834 Kb package ŒRcpp‚ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ŒRcpp‚ package ŒRcppClassic‚ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\\AppData\Local\Temp\Rtmp2t8Lm6\downloaded_packages Thanks,Lin [[alternative HTML version deleted]] The jumbled content that reached the mailing list is due to your failing to heed the directions in the Posting Guide. (There is quite a bit more that you should be doing that is also described therein.) It appears that your error may be due to your failure to read and properly address the warnings in that console output: Warning: cannot remove prior installation of package ŒRcpp‚ package ŒRcppClassic. My guess is that you are trying to install a package when it is already loaded and that your (unnamed but obviously Windows) OS is trying to protect it. But it is a guess since I work in a different OS and I do not use Rcpp. (I do however read warnings and search Rhelp for prior reports.) If my guess is correct, you may get success by exiting R, restarting and then installing the new version of the package and then re-load it. -- David Winsemius 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.
[R] gamma regression zeros
Dear all, I have a problem with gamma regression (glm - family = Gamma) and zeros in R. The problem is the following when I try to estimate a dataset without zeros (endogenous variable) there is no problem. However, if I try to do the same with zeros I always get an error message. In STATA and MATLAB I can do a gamma regression with zeros. What could be the problem? If someone could help me I would really appreciate this. Thanks in advance, Soeren [[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] Reading dataset in R
Hi Arun, I finally got the output file (attached here). It has multiple rows. could you help me read it in R? On Fri, Mar 22, 2013 at 3:35 PM, arun kirshna [via R] ml-node+s789695n4662243...@n4.nabble.com wrote: Hi, Try this: con-file(Rout2122.text) Lines1- readLines(con) close(con) indx-rep(c(TRUE,FALSE),each=2) #changed here Lines2-Lines1[!grepl([A-Za-z],Lines1)] res-read.table(text=paste(gsub(^\\s+,,Lines2[indx]),gsub(^\\s+,,Lines2[!indx])),sep=,header=FALSE)#some changes nm1-unlist(strsplit(gsub(^ +,,paste(Lines1[grepl([A-Za-z],Lines1)][1:2],collapse= )), )) colnames(res)- nm1[nm1!=] res # m1 n1 m n cterm1_P0L cterm1_P0H c11 c12 c1 c2 alpha beta T_error N #1 4 4 10 8 0.8145062 0.6634204 0.03 0.05 0.15 0.2 0.1 0.4 0.6918138 18 #2 5 4 9 8 0.7737809 0.6302494 0.03 0.05 0.15 0.2 0.1 0.4 0.6643599 17 #ENBHBL AH AL #1 10.45928 0.1690183 0.3952494 0.07470420 0.05284197 #2 11.38388 0.1694641 0.3624458 0.07208597 0.06036395 A.K. From: Zjoanna [hidden email]http://user/SendEmail.jtp?type=nodenode=4662243i=0 To: [hidden email] http://user/SendEmail.jtp?type=nodenode=4662243i=1 Sent: Friday, March 22, 2013 3:54 PM Subject: Re: [R] Reading dataset in R Hi, I also need to read this format of file in R, it is a Wordpad file. On Thu, Mar 21, 2013 at 5:07 PM, arun [hidden email]http://user/SendEmail.jtp?type=nodenode=4662243i=2 wrote: Hi, con-file(Rout1112.text) Lines1- readLines(con) close(con) indx-rep(rep(c(TRUE,FALSE),each=2),2) Lines2-Lines1[!grepl([A-Za-z],Lines1)] res-read.table(text=paste(gsub(^\\d+\\s+,,Lines2[indx]),gsub(^\\d+\\s+,,Lines2[!indx])),sep=,header=FALSE) nm1-unlist(strsplit(gsub(^ +,,paste(Lines1[grepl([A-Za-z],Lines1)][1:2],collapse= )), )) colnames(res)- nm1[nm1!=] res #m1 n1 m n cterm1_P0L cterm1_P0H c11 c12 c1 c2 alpha beta T_error N #1 4 5 11 9 0.8145062 0.6302494 0.03 0.03 0.15 0.15 0.1 0.4 0.6339515 20 #2 7 4 11 8 0.6983373 0.000 0.03 0.03 0.15 0.15 0.1 0.4 0.4899431 19 #3 4 5 10 9 0.8145062 0.6302494 0.03 0.03 0.15 0.20 0.1 0.4 0.6831988 19 #4 5 4 9 8 0.7737809 0.000 0.03 0.03 0.15 0.20 0.1 0.4 0.6458095 17 # EN BHBL AH AL #1 11.77746 0.12159579 0.3846999 0.09567271 0.03198315 #2 16.20665 0.09819012 0.2550532 0.09581478 0.04088503 #3 11.59196 0.15166360 0.3943098 0.08405337 0.05317206 #4 13.90488 0.14031630 0.3624458 0.08268336 0.06036395 A.K. From: Joanna Zhang [hidden email]http://user/SendEmail.jtp?type=nodenode=4662243i=3 To: arun [hidden email]http://user/SendEmail.jtp?type=nodenode=4662243i=4 Sent: Thursday, March 21, 2013 3:03 PM Subject: Re: [R] cumulative sum by group and under some criteria Hi, I used a computer cluster and output the file attached here, I need to read the file in R. I tried to use read.table, but it seems that the format is not recognized by R, could you help? Rout2122.text (528 bytes) http://r.789695.n4.nabble.com/attachment/4662237/0/Rout2122.text -- View this message in context: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4662237.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4662243i=5mailing 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. __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4662243i=6mailing 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/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4662243.html To unsubscribe from cumulative sum by group and under some criteria, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4657074code=WmpvYW5uYTIwMTNAZ21haWwuY29tfDQ2NTcwNzR8LTE3NTE1MDA0MzY= . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml Routallnew.txt (12K)
[R] About name of list elements
Hi folks, I am starter for R. While I tried list as following: l - list() l$foo NULL l$foobar - 1 l$foo [1] 1 Apparently, foo and foobar are different name for elements in list (actually foo does not exist). But why they are sharing same value? Thanks a lot! Max [[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] Newbie code to count runs of up or down moves
There are probably many mistakes with this code. I am used to coding in C with a debugger, so I am very new to coding in R without one and different syntax. My code in the upper left panel of R Studio isz-c(3,1,4,5,2,1,0,3,5,8)zlength(z)y-c(0,0,0,0,0,0,0,0,0,0)ylength(y)zdiff = diff(z)zdiffn-length(zdiff)nx-zdifff-function(x) { for (k in 1:n){for (i in 1:n-k){ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y[k]-y[k]+1 } } return(y)}f(x)__I get back z [1] 3 1 4 5 2 1 0 3 5 8 length(z)[1] 10 y-c(0,0,0,0,0,0,0,0,0,0) y [1] 0 0 0 0 0 0 0 0 0 0 length(y)[1] 10 zdiff = diff(z) zdiff[1] -2 3 1 -3 -1 -1 3 2 3 n-length(zdiff) n[1] 9 x-zdiff f-function(x) + {+ for (k in 1:n){+ for (i in 1:n-k){+ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0)+ y[k]-y[k]+1 Error: unexpected symbol in: if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y }Error: unexpected '}' in } }Error: unexpected '}' in } return(y)Error: no function to return from, jumping to top level }Error: unexpected '}' in } f(x)Error in if (all(x[i] * x[i - 1] 0) ((x[i + k + 1] * x[i + k]) : missing value where TRUE/FALSE needed_Perhaps there is someway to use NULL or NA? -- View this message in context: http://r.789695.n4.nabble.com/Newbie-code-to-count-runs-of-up-or-down-moves-tp4662423.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] Newbie code to count runs of up or down moves
My apologies, I didn't realize checking HTML format would produce unreadable text. WIll repost after some more thought to the problem. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Newbie-code-to-count-runs-of-up-or-down-moves-tp4662423p4662431.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] Faster way of summing values up based on expand.grid
Hello! # I have 3 vectors of values: values1-rnorm(10) values2-rnorm(10) values3-rnorm(10) # In real life, all 3 vectors have a length of 25 # I create all possible combinations of 4 based on 10 elements: mycombos-expand.grid(1:10,1:10,1:10,1:10) dim(mycombos) # Removing rows that contain pairs of identical values in any 2 of these columns: mycombos-mycombos[!(mycombos$Var1 == mycombos$Var2),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var3 == mycombos$Var4),] dim(mycombos) # I want to write sums of elements from values1, values2, and values 3 whose numbers are contained in each column of mycombos. Here is how I am going it now - using a loop: mycombos$sum1-NA mycombos$sum2-NA mycombos$sum3-NA for(i in 1:nrow(mycombos)){ mycombos$sum1[i]-values1[[mycombos[i,Var1]]] + values1[[mycombos[i,Var2]]] + values1[[mycombos[i,Var3]]] + values1[[mycombos[i,Var4]]] mycombos$sum2[i]-values2[[mycombos[i,Var1]]] + values2[[mycombos[i,Var2]]] + values2[[mycombos[i,Var3]]] + values2[[mycombos[i,Var4]]] mycombos$sum3[i]-values3[[mycombos[i,Var1]]] + values3[[mycombos[i,Var2]]] + values3[[mycombos[i,Var3]]] + values3[[mycombos[i,Var4]]] } head(mycombos);tail(mycombos) # It's going to take me forever with this loop. Is there a faster way of doing the dame thing? Thanks a lot! -- Dimitri Liakhovitski [[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] Faster way of summing values up based on expand.grid
This is another method I can think of, but it's also slow: for(i in 1:nrow(mycombos)){ # i=1 indexes=rep(0,10) myitems-unlist(mycombos[i,1:4]) indexes[myitems]-1 mycombos$sum1[i]-sum(values1 * indexes) mycombos$sum2[i]-sum(values2 * indexes) mycombos$sum3[i]-sum(values3 * indexes) } On Mon, Mar 25, 2013 at 5:00 PM, Dimitri Liakhovitski dimitri.liakhovit...@gmail.com wrote: Hello! # I have 3 vectors of values: values1-rnorm(10) values2-rnorm(10) values3-rnorm(10) # In real life, all 3 vectors have a length of 25 # I create all possible combinations of 4 based on 10 elements: mycombos-expand.grid(1:10,1:10,1:10,1:10) dim(mycombos) # Removing rows that contain pairs of identical values in any 2 of these columns: mycombos-mycombos[!(mycombos$Var1 == mycombos$Var2),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var3 == mycombos$Var4),] dim(mycombos) # I want to write sums of elements from values1, values2, and values 3 whose numbers are contained in each column of mycombos. Here is how I am going it now - using a loop: mycombos$sum1-NA mycombos$sum2-NA mycombos$sum3-NA for(i in 1:nrow(mycombos)){ mycombos$sum1[i]-values1[[mycombos[i,Var1]]] + values1[[mycombos[i,Var2]]] + values1[[mycombos[i,Var3]]] + values1[[mycombos[i,Var4]]] mycombos$sum2[i]-values2[[mycombos[i,Var1]]] + values2[[mycombos[i,Var2]]] + values2[[mycombos[i,Var3]]] + values2[[mycombos[i,Var4]]] mycombos$sum3[i]-values3[[mycombos[i,Var1]]] + values3[[mycombos[i,Var2]]] + values3[[mycombos[i,Var3]]] + values3[[mycombos[i,Var4]]] } head(mycombos);tail(mycombos) # It's going to take me forever with this loop. Is there a faster way of doing the dame thing? Thanks a lot! -- Dimitri Liakhovitski -- Dimitri Liakhovitski [[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] Faster way of summing values up based on expand.grid
Hi Dimitri, If I understood correctly, the following will do: system.time(sum1 - apply(mycombos, 1, function(x) sum(values1[x]))) system.time(sum2 - apply(mycombos, 1, function(x) sum(values2[x]))) system.time(sum3 - apply(mycombos, 1, function(x) sum(values3[x]))) cbind(sum1, sum2, sum3) HTH, Jorge.- On Tue, Mar 26, 2013 at 8:12 AM, Dimitri Liakhovitski wrote: This is another method I can think of, but it's also slow: for(i in 1:nrow(mycombos)){ # i=1 indexes=rep(0,10) myitems-unlist(mycombos[i,1:4]) indexes[myitems]-1 mycombos$sum1[i]-sum(values1 * indexes) mycombos$sum2[i]-sum(values2 * indexes) mycombos$sum3[i]-sum(values3 * indexes) } On Mon, Mar 25, 2013 at 5:00 PM, Dimitri Liakhovitski dimitri.liakhovit...@gmail.com wrote: Hello! # I have 3 vectors of values: values1-rnorm(10) values2-rnorm(10) values3-rnorm(10) # In real life, all 3 vectors have a length of 25 # I create all possible combinations of 4 based on 10 elements: mycombos-expand.grid(1:10,1:10,1:10,1:10) dim(mycombos) # Removing rows that contain pairs of identical values in any 2 of these columns: mycombos-mycombos[!(mycombos$Var1 == mycombos$Var2),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var3 == mycombos$Var4),] dim(mycombos) # I want to write sums of elements from values1, values2, and values 3 whose numbers are contained in each column of mycombos. Here is how I am going it now - using a loop: mycombos$sum1-NA mycombos$sum2-NA mycombos$sum3-NA for(i in 1:nrow(mycombos)){ mycombos$sum1[i]-values1[[mycombos[i,Var1]]] + values1[[mycombos[i,Var2]]] + values1[[mycombos[i,Var3]]] + values1[[mycombos[i,Var4]]] mycombos$sum2[i]-values2[[mycombos[i,Var1]]] + values2[[mycombos[i,Var2]]] + values2[[mycombos[i,Var3]]] + values2[[mycombos[i,Var4]]] mycombos$sum3[i]-values3[[mycombos[i,Var1]]] + values3[[mycombos[i,Var2]]] + values3[[mycombos[i,Var3]]] + values3[[mycombos[i,Var4]]] } head(mycombos);tail(mycombos) # It's going to take me forever with this loop. Is there a faster way of doing the dame thing? Thanks a lot! -- Dimitri Liakhovitski -- Dimitri Liakhovitski [[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.
Re: [R] About name of list elements
Hi Max, This is known as fuzzy matching. When using `$`, if R can uniquely match the element name based on what is typed, it returns it. Thus, in your example, foo uniquely matches foobar, but if you had foobar, foobox, $foo would not be a unique match. Cheers, Josh On Mon, Mar 25, 2013 at 12:21 PM, Andrew Lin hlin0...@gmail.com wrote: Hi folks, I am starter for R. While I tried list as following: l - list() l$foo NULL l$foobar - 1 l$foo [1] 1 Apparently, foo and foobar are different name for elements in list (actually foo does not exist). But why they are sharing same value? Thanks a lot! Max [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.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] Faster way of summing values up based on expand.grid
Thank you so much, Jorge. I checked and your method is at least 200 times faster than mine! Exactly what I was looking for. Dimitri On Mon, Mar 25, 2013 at 5:49 PM, Jorge I Velez jorgeivanve...@gmail.comwrote: Hi Dimitri, If I understood correctly, the following will do: system.time(sum1 - apply(mycombos, 1, function(x) sum(values1[x]))) system.time(sum2 - apply(mycombos, 1, function(x) sum(values2[x]))) system.time(sum3 - apply(mycombos, 1, function(x) sum(values3[x]))) cbind(sum1, sum2, sum3) HTH, Jorge.- On Tue, Mar 26, 2013 at 8:12 AM, Dimitri Liakhovitski wrote: This is another method I can think of, but it's also slow: for(i in 1:nrow(mycombos)){ # i=1 indexes=rep(0,10) myitems-unlist(mycombos[i,1:4]) indexes[myitems]-1 mycombos$sum1[i]-sum(values1 * indexes) mycombos$sum2[i]-sum(values2 * indexes) mycombos$sum3[i]-sum(values3 * indexes) } On Mon, Mar 25, 2013 at 5:00 PM, Dimitri Liakhovitski dimitri.liakhovit...@gmail.com wrote: Hello! # I have 3 vectors of values: values1-rnorm(10) values2-rnorm(10) values3-rnorm(10) # In real life, all 3 vectors have a length of 25 # I create all possible combinations of 4 based on 10 elements: mycombos-expand.grid(1:10,1:10,1:10,1:10) dim(mycombos) # Removing rows that contain pairs of identical values in any 2 of these columns: mycombos-mycombos[!(mycombos$Var1 == mycombos$Var2),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var3 == mycombos$Var4),] dim(mycombos) # I want to write sums of elements from values1, values2, and values 3 whose numbers are contained in each column of mycombos. Here is how I am going it now - using a loop: mycombos$sum1-NA mycombos$sum2-NA mycombos$sum3-NA for(i in 1:nrow(mycombos)){ mycombos$sum1[i]-values1[[mycombos[i,Var1]]] + values1[[mycombos[i,Var2]]] + values1[[mycombos[i,Var3]]] + values1[[mycombos[i,Var4]]] mycombos$sum2[i]-values2[[mycombos[i,Var1]]] + values2[[mycombos[i,Var2]]] + values2[[mycombos[i,Var3]]] + values2[[mycombos[i,Var4]]] mycombos$sum3[i]-values3[[mycombos[i,Var1]]] + values3[[mycombos[i,Var2]]] + values3[[mycombos[i,Var3]]] + values3[[mycombos[i,Var4]]] } head(mycombos);tail(mycombos) # It's going to take me forever with this loop. Is there a faster way of doing the dame thing? Thanks a lot! -- Dimitri Liakhovitski -- Dimitri Liakhovitski [[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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitri Liakhovitski [[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] About name of list elements
If you use the shortcut $ then R will use partial matching to hunt for the list element you mean. l$fo will also match. l[[foo]] will not match - the full subsetting construct doesn't use partial matching. I think the intro to R covers this, and you can also see ?$ ?[[ Sarah On Monday, March 25, 2013, Andrew Lin wrote: Hi folks, I am starter for R. While I tried list as following: l - list() l$foo NULL l$foobar - 1 l$foo [1] 1 Apparently, foo and foobar are different name for elements in list (actually foo does not exist). But why they are sharing same value? Thanks a lot! Max [[alternative HTML version deleted]] __ R-help@r-project.org javascript:; 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. -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[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] About name of list elements
Hello Starter Before posting, please read relevant Help files! ?$ where it tells you: x$name is equivalent to x[[name, exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument. ..etc. -- Bert On Mon, Mar 25, 2013 at 12:21 PM, Andrew Lin hlin0...@gmail.com wrote: Hi folks, I am starter for R. While I tried list as following: l - list() l$foo NULL l$foobar - 1 l$foo [1] 1 Apparently, foo and foobar are different name for elements in list (actually foo does not exist). But why they are sharing same value? Thanks a lot! Max [[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. -- 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 [[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] Reassign Multiple Factors to same Factor Value
Dear All, Probably something very easy, but I am looking for the most efficient ways to achieve this. Consider the following snippet y-c('a','b','c','d','e','f','g') x-rnorm(length(y)) df-data.frame(y,x) leading to df$y [1] a b c d e f g Levels: a b c d e f g Now, I would like to replace levels ('e','f','g') of df$y with a new level 'other' so that levels(df$y) returns a b c d other What is the easiest way to achieve this, considering that df$y[5:7]-'other' Does not work? Cheers Lorenzo __ 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] Newbie code to count runs of up or down moves
This is unreadable. Please repost in plain text, ideally NOT using Nabble, and please also tell us what you're trying to accomplish. Sarah On Monday, March 25, 2013, Newbie1234 wrote: There are probably many mistakes with this code. I am used to coding in C with a debugger, so I am very new to coding in R without one and different syntax. My code in the upper left panel of R Studio isz-c(3,1,4,5,2,1,0,3,5,8)zlength(z)y-c(0,0,0,0,0,0,0,0,0,0)ylength(y)zdiff = diff(z)zdiffn-length(zdiff)nx-zdifff-function(x) { for (k in 1:n){for (i in 1:n-k){ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y[k]-y[k]+1 } } return(y)}f(x)__I get back z [1] 3 1 4 5 2 1 0 3 5 8 length(z)[1] 10 y-c(0,0,0,0,0,0,0,0,0,0) y [1] 0 0 0 0 0 0 0 0 0 0 length(y)[1] 10 zdiff = diff(z) zdiff[1] -2 3 1 -3 -1 -1 3 2 3 n-length(zdiff) n[1] 9 x-zdiff f-function(x) + {+ for (k in 1:n){+ for (i in 1:n-k){+ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0)+ y[k]-y[k]+1 Error: unexpected symbol in: if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y }Error: unexpected '}' in } }Error: unexpected '}' in } return(y)Error: no function to return from, jumping to top level }Error: unexpected '}' in } f(x)Error in if (all(x[i] * x[i - 1] 0) ((x[i + k + 1] * x[i + k]) : missing value where TRUE/FALSE needed_Perhaps there is someway to use NULL or NA? -- View this message in context: http://r.789695.n4.nabble.com/Newbie-code-to-count-runs-of-up-or-down-moves-tp4662423.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org javascript:; 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. -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[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] Newbie code to count runs of up or down moves
In full agreement with Sarah's comments. That being said, the Magic 8 Ball has the following in the little window for the OP: ?rle Regards, Marc Schwartz On Mar 25, 2013, at 2:44 PM, Sarah Goslee sarah.gos...@gmail.com wrote: This is unreadable. Please repost in plain text, ideally NOT using Nabble, and please also tell us what you're trying to accomplish. Sarah On Monday, March 25, 2013, Newbie1234 wrote: There are probably many mistakes with this code. I am used to coding in C with a debugger, so I am very new to coding in R without one and different syntax. My code in the upper left panel of R Studio isz-c(3,1,4,5,2,1,0,3,5,8)zlength(z)y-c(0,0,0,0,0,0,0,0,0,0)ylength(y)zdiff = diff(z)zdiffn-length(zdiff)nx-zdifff-function(x) { for (k in 1:n){for (i in 1:n-k){ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y[k]-y[k]+1 } } return(y)}f(x)__I get back z [1] 3 1 4 5 2 1 0 3 5 8 length(z)[1] 10 y-c(0,0,0,0,0,0,0,0,0,0) y [1] 0 0 0 0 0 0 0 0 0 0 length(y)[1] 10 zdiff = diff(z) zdiff[1] -2 3 1 -3 -1 -1 3 2 3 n-length(zdiff) n[1] 9 x-zdiff f-function(x) + {+ for (k in 1:n){+ for (i in 1:n-k){+ if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0)+ y[k]-y[k]+1 Error: unexpected symbol in: if (all(x[i] * x[i-1] 0) ((x[i+k+1] * x[i+k]) 0) y }Error: unexpected '}' in } }Error: unexpected '}' in } return(y)Error: no function to return from, jumping to top level }Error: unexpected '}' in } f(x)Error in if (all(x[i] * x[i - 1] 0) ((x[i + k + 1] * x[i + k]) : missing value where TRUE/FALSE needed_Perhaps there is someway to use NULL or NA? -- View this message in context: http://r.789695.n4.nabble.com/Newbie-code-to-count-runs-of-up-or-down-moves-tp4662423.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org javascript:; 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. -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[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] Reassign Multiple Factors to same Factor Value
Hi Lorenzo, On Mon, Mar 25, 2013 at 6:18 PM, Lorenzo Isella lorenzo.ise...@gmail.com wrote: Dear All, Probably something very easy, but I am looking for the most efficient ways to achieve this. Consider the following snippet y-c('a','b','c','d','e','f','g') x-rnorm(length(y)) df-data.frame(y,x) leading to df$y [1] a b c d e f g Levels: a b c d e f g Now, I would like to replace levels ('e','f','g') of df$y with a new level 'other' so that levels(df$y) returns a b c d other What is the easiest way to achieve this, levels(df$y) - c(a, b, c, d, rep(others, 3)) will do the job. considering that df$y[5:7]-'other' Does not work? Cheers Lorenzo __ 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] About name of list elements
To the OP: Sooner or later most R beginners are bitten by this all too convenient shortcut. As an R newbie, think of R as your bank account: overuse of $-extraction can lead to undesirable consequences. It's best to acquire the '[[' and '[' habit early. Peter Ehlers On 2013-03-25 12:43, Bert Gunter wrote: Hello Starter Before posting, please read relevant Help files! ?$ where it tells you: x$name is equivalent to x[[name, exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument. ..etc. -- Bert On Mon, Mar 25, 2013 at 12:21 PM, Andrew Lin hlin0...@gmail.com wrote: Hi folks, I am starter for R. While I tried list as following: l - list() l$foo NULL l$foobar - 1 l$foo [1] 1 Apparently, foo and foobar are different name for elements in list (actually foo does not exist). But why they are sharing same value? Thanks a lot! Max [[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] Reassign Multiple Factors to same Factor Value
or just levels(df$y)[5:7]-others Ista Zahn wrote Hi Lorenzo, On Mon, Mar 25, 2013 at 6:18 PM, Lorenzo Isella lt; lorenzo.isella@ gt; wrote: Dear All, Probably something very easy, but I am looking for the most efficient ways to achieve this. Consider the following snippet y-c('a','b','c','d','e','f','g') x-rnorm(length(y)) df-data.frame(y,x) leading to df$y [1] a b c d e f g Levels: a b c d e f g Now, I would like to replace levels ('e','f','g') of df$y with a new level 'other' so that levels(df$y) returns a b c d other What is the easiest way to achieve this, levels(df$y) - c(a, b, c, d, rep(others, 3)) will do the job. considering that df$y[5:7]-'other' Does not work? Cheers Lorenzo __ R-help@ 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@ 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. -- View this message in context: http://r.789695.n4.nabble.com/Reassign-Multiple-Factors-to-same-Factor-Value-tp4662437p4662440.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] CP in rpart library
Can anyone help me to compute manually de CP cost-parameter in library rpart when the command printcp is executed in a Classification tree?? Thank you very much Alberto Cebollada Solanas Grupo de Genética de Micobacterias (http://genmico.unizar.es/) CIBERes - CIBER Enfermedades Respiratorias (http://www.ciberes.org) Facultad de Medicina - Aulario A Universidad de Zaragoza C/ Domingo Miral s/n CP 50009-Zaragoza, Aragón albe...@unizar.es Tel: +34-976-762420 Fax: +34-976-762420 __ 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] GAM model with interactions between continuous variables and factors
Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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] GAM model with interactions between continuous variables and factors
Just to clarify: gam.1 has wealth inside the smooths and as a fixed effect predictor while gam.2 only have wealth inside the smooths. Thanks On Mon, Mar 25, 2013 at 6:09 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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] GAM model with interactions between continuous variables and factors
Hi Antonio, If wealth is a factor variable, you should include the main effect in the model, as the smooths will be centered. Cheers, Josh On Mon, Mar 25, 2013 at 6:09 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.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] Reassign Multiple Factors to same Factor Value
Hi, You could also try: library(plyr) df1- df df2- df df$y-revalue(df$y,c(e=others,f=others,g=others)) df$y #[1] a b c d others others others #or df1$y-mapvalues(df1$y,from=c(e,f,g),to=rep(others,3)) levels(df1$y) #[1] a b c d others #or levels(df2$y)[match(c(e,f,g),levels(df2$y))]-others levels(df2$y) #[1] a b c d others A.K. From: Lorenzo Isella lorenzo.ise...@gmail.com To: r-h...@stat.math.ethz.ch r-h...@stat.math.ethz.ch Sent: Monday, March 25, 2013 6:18 PM Subject: [R] Reassign Multiple Factors to same Factor Value Dear All, Probably something very easy, but I am looking for the most efficient ways to achieve this. Consider the following snippet y-c('a','b','c','d','e','f','g') x-rnorm(length(y)) df-data.frame(y,x) leading to df$y [1] a b c d e f g Levels: a b c d e f g Now, I would like to replace levels ('e','f','g') of df$y with a new level 'other' so that levels(df$y) returns a b c d other What is the easiest way to achieve this, considering that df$y[5:7]-'other' Does not work? Cheers Lorenzo __ 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] GAM model with interactions between continuous variables and factors
Just to clarify: I should include wealth - the categorical variable - as a fixed effects *and* within the smooth using the argument by. It that correct? thanks a bunch On Mon, Mar 25, 2013 at 6:18 PM, Joshua Wiley jwiley.ps...@gmail.comwrote: Hi Antonio, If wealth is a factor variable, you should include the main effect in the model, as the smooths will be centered. Cheers, Josh On Mon, Mar 25, 2013 at 6:09 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.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] GAM model with interactions between continuous variables and factors
Yep that's exactly right! :) On Mon, Mar 25, 2013 at 6:22 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Just to clarify: I should include wealth - the categorical variable - as a fixed effects *and* within the smooth using the argument by. It that correct? thanks a bunch On Mon, Mar 25, 2013 at 6:18 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Antonio, If wealth is a factor variable, you should include the main effect in the model, as the smooths will be centered. Cheers, Josh On Mon, Mar 25, 2013 at 6:09 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.com -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.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] GAM model with interactions between continuous variables and factors
Thanks! On Mon, Mar 25, 2013 at 6:25 PM, Joshua Wiley jwiley.ps...@gmail.comwrote: Yep that's exactly right! :) On Mon, Mar 25, 2013 at 6:22 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Just to clarify: I should include wealth - the categorical variable - as a fixed effects *and* within the smooth using the argument by. It that correct? thanks a bunch On Mon, Mar 25, 2013 at 6:18 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Antonio, If wealth is a factor variable, you should include the main effect in the model, as the smooths will be centered. Cheers, Josh On Mon, Mar 25, 2013 at 6:09 PM, Antonio P. Ramos ramos.grad.stud...@gmail.com wrote: Hi all, I am not sure how to handle interactions with categorical predictors in the GAM models. For example what is the different between these bellow two models. Tests are indicating that they are different but their predictions are essentially the same. Thanks a bunch, gam.1 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + ++ wealth + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) gam.2 - gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ +s(birth_year,by=wealth) + + + sex + +residence+ maternal_educ + birth_order, + ,data=rwanda2,family=binomial) anova(gam.1,gam.2,test=Chi) Analysis of Deviance Table Model 1: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +wealth + sex + residence + maternal_educ + birth_order Model 2: mortality.under.2 ~ maternal_age_c + I(maternal_age_c^2) + s(birth_year, by = wealth) + +sex + residence + maternal_educ + birth_order Resid. Df Resid. Dev Df Deviance Pr(Chi) 1 28986 24175 2 28989 24196 -3.6952 -21.378 0.0001938 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 str(rwanda2) 'data.frame': 29027 obs. of 18 variables: $ CASEID: Factor w/ 10718 levels 1 5 2,..: 289 2243 7475 9982 6689 10137 7426 428 8415 10426 ... $ mortality.under.2 : int 0 1 0 0 0 0 0 0 1 0 ... $ maternal_age_disct: Factor w/ 3 levels -25,+35,25-35: 1 1 1 1 1 1 3 1 3 1 ... $ maternal_age : int 18 21 21 23 21 22 26 18 27 21 ... $ time : int 3 3 3 3 3 3 3 3 3 3 ... $ child_mortality : num 0.232 0.232 0.232 0.232 0.232 ... $ democracy : Factor w/ 1 level dictatorship: 1 1 1 1 1 1 1 1 1 1 ... $ wealth: Factor w/ 5 levels Lowest quintile,..: 2 4 1 4 5 1 4 1 4 5 ... $ birth_year: int 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ... $ residence : Factor w/ 2 levels Rural,Urban: 1 1 1 1 2 1 1 1 1 2 ... $ birth_order : int 1 2 2 5 1 1 3 1 2 2 ... $ maternal_educ : Factor w/ 4 levels Higher,No education,..: 3 2 2 3 4 2 3 2 2 2 ... $ sex : Factor w/ 2 levels Female,Male: 1 1 2 2 1 1 2 2 2 2 ... $ quinquennium : Factor w/ 7 levels 00-5's,70-4,..: 2 2 2 2 2 2 2 2 2 2 ... $ time.1: int 3 3 3 3 3 3 3 3 3 3 ... $ new_time : int 0 0 0 0 0 0 0 0 0 0 ... $ maternal_age_c: num -6.12 -3.12 -3.12 -1.12 -3.12 ... $ birth_year_c : num -14.8 -14.8 -14.8 -14.8 -14.8 ... [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.com -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.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] Problem with nested for-loop
Hello, I'm working on a problem using nested for-loops and I don't know if it's a problem with the order of the loops or something within the loop so any help with the problem would be appreciated. To briefly set up the problem. I have 259 trees (from 11 different species, of unequal count for each species) of which I am trying to predict biomass. For each tree species I have 1 iterations for the regression coefficients, which were estimated previously in WinBUGS, and what I'm trying to do is for each tree I want to predict the biomass using each species-specific iteration of the regression coefficients, so that ultimately each tree has 1 estimates of biomass, organized into a 1x259 matrix. The input data used in the model equation is stored in two separate files and I don't think this is creating problems, but I thought it might be worth mentioning. I've pasted the code below and if any additional info is needed please write back and I will post it. #read in the model output data from Jenkins eq. 1 under data #read in the prediction data set under predict data j1data=read.delim(reduced_j1_forR.txt,header=T) predictdata=read.delim(predictset_forR.txt,header=T) j1data=j1data[,2:4] its=c(rep(1:1,each=11)) j1data=cbind(its,j1data) #set up a matrix full of zeros lnbm where prediction results are placed #set up for loop that first loops over iteration, then species #(total iterations=1it/spp*11spp=11 iterations)and then over each tree #(total # of trees=259) niter=1 nspp=11 ntrees=259 lnbm=matrix(0,1,259) k=numeric() for (i in 1:ntrees) { for (j in 1:nspp) { for (m in 1:niter) { k=((j1data$its[m]-1)*1000)+(j1data$spp[j]) #print(k) lnbm[m,i]=j1data$b0[k]+j1data$b1[k]*predictdata$lndbh[i] } } } Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Converting 2D matrix to 3D array
Hi, I would like to create M paths of 2 correlated brownian motion incrementals where each path is of length N. I use mvrnorm to create all the increments, i.e. h - 1.0; COV - matrix(c(1,0,0,1),nrow=2); dW- h * t(mvrnorm(n=N*M,mu=c(0,0),Sigma=COV)); The next step is that I'd like to wrap dW (2D matrix of size 2x(NM) into a 3D array where each slice is 2xN matrix and the entire array consists of M slices. Just wondering if there is any way to do that? Or is there a better way than the way I am describing? Thank you. Roebert [[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] gamma regression zeros
Sören Prehn Prehn at iamo.de writes: Dear all, I have a problem with gamma regression (glm - family = Gamma) and zeros in R. The problem is the following when I try to estimate a dataset without zeros (endogenous variable) there is no problem. However, if I try to do the same with zeros I always get an error message. In STATA and MATLAB I can do a gamma regression with zeros. What could be the problem? If someone could help me I would really appreciate this. It's hard to know what Stata and MATLAB are doing to allow you to fit this model. As long as the scale parameter is =1 everything should be OK, but as soon as the scale parameter drops below 1 the likelihood (density) is infinite: dgamma(0,shape=1) ## [1] 1 dgamma(0,shape=0.) ## [1] Inf It's conceivable that you could succeed in this task by constraining the shape parameter to be =1; e.g. library(bbmle) mle2(y~dgamma(shape=1+exp(logshape),scale=exp(logscale)), parameters=list(logshape~...,logscale~...), data= ...) We would need more detail though. __ 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] Can you help with local source file instalation?
Hi R, I tried to install one local source package---qtl into R, but failed with the following message. I am using Macbook OS 10.8.3 with Xcode 4.6.1. ld: warning: directory not found for option '-L/usr/local/lib' ld: library not found for -lgfortran collect2: ld returned 1 exit status make: *** [qtl.so] Error 1 ERROR: compilation failed for package ¡®qtl¡¯ * removing ¡®/Library/Frameworks/R.framework/Versions/2.15/Resources/library/qtl¡¯ * restoring previous ¡®/Library/Frameworks/R.framework/Versions/2.15/Resources/library/qtl¡¯ Any comments are appreciated. Liu [[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] Distance calculation
Hi Elisa, Based on the formula you gave, this is what I got: dat1- read.csv(rate.csv,sep=,) res- do.call(cbind,lapply(seq_len(nrow(dat1)),function(i) do.call(rbind,lapply(split(rbind(dat1[i,],dat1[-i,]),1:nrow(rbind(dat1[i,],dat1[-i,]))), function(x) {x1-rbind(dat1[i,],x);colnames(x1)-gsub([.],,colnames(x1)); if({indx- colSums(x1[,2:5]==0);indx[1]==0 indx[2]==0 indx[3]==1 indx[4]==2}) #3 peaks 2 peak comparison {x2- x1[order(x1$Peak3t,x1$Npeak3t),]; with(x2,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+ abs((Peak2v[1]-Peak2v[2])*(Peak2t[1]-Peak2t[2]))+abs((Peak1v[1]-Peak3v[2])*((Peak1t[1]+12)-Peak3t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+ abs((Npeak2v[1]-Npeak2v[2])*(Npeak2t[1]-Npeak2t[2]))+abs((Npeak1v[1]-Npeak3v[2])*((Npeak1t[1]+12)-Npeak3t[2]))}) } else if({indx[1]==0 indx[2]==0 indx[3]==1 indx[4]==1}) #4 peaks 2 peak comparison. Peak3 Peak4 value compared with Peak1 {x4- x1[order(x1$Peak3t,x1$Peak4t,x1$Npeak3t,x1$Npeak4t),]; with(x4,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+ abs((Peak2v[1]-Peak2v[2])*(Peak2t[1]-Peak2t[2]))+ abs((Peak1v[1]-Peak3v[2])*((Peak1t[1]+12)-Peak3t[2]))+abs((Peak1v[1]-Peak4v[2])*((Peak1t[1]+12)-Peak4t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+ abs((Npeak2v[1]-Npeak2v[2])*(Npeak2t[1]-Npeak2t[2]))+ abs((Npeak1v[1]-Npeak3v[2])*((Npeak1t[1]+12)-Npeak3t[2]))+abs((Npeak1v[1]-Npeak4v[2])*((Npeak1t[1]+12)-Npeak4t[2])) }) } else if({indx[1]==0 indx[2]==1 indx[3]==2 indx[4]==2}) #2 peaks 1 peak comparison {x5- x1[order(x1$Peak2t,x1$Npeak2t),]; with(x5,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+abs((Peak1v[1]-Peak2v[2])*((Peak1t[1]+12)-Peak2t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+abs((Npeak1v[1]-Npeak2v[2])*((Npeak1t[1]+12)-Npeak2t[2])) }) } else if({indx[1]==0 indx[2]==1 indx[3]==1 indx[4]==2 }) #3 peak 1 peak comparison {x6- x1[order(x1$Peak2t,x1$Peak3t,x1$Npeak2t,x1$Npeak3t),]; with(x6,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+ abs((Peak1v[1]-Peak2v[2])*((Peak1t[1]+12)-Peak2t[2]))+ abs((Peak1v[1]-Peak3v[2])*((Peak1t[1]+12)-Peak3t[2])) + abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+ abs((Npeak1v[1]-Npeak2v[2])*((Npeak1t[1]+12)-Npeak2t[2]))+ abs((Npeak1v[1]-Npeak3v[2])*((Npeak1t[1]+12)-Npeak3t[2])) }) } else if({indx[1]==0 indx[2]==0 indx[3]==0 indx[4]==1}) # 4 peak 3 peak comparison {x7- x1[order(x1$Peak4t,x1$Npeak4t),]; with(x7,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+abs((Peak2v[1]-Peak2v[2])*(Peak2t[1]-Peak2t[2]))+abs((Peak3v[1]-Peak3v[2])*(Peak3t[1]-Peak3t[2]))+ abs((Peak1v[1]-Peak4v[2])*((Peak1t[1]+12)-Peak4t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+abs((Npeak2v[1]-Npeak2v[2])*(Npeak2t[1]-Npeak2t[2]))+abs((Npeak3v[1]-Npeak3v[2])*(Npeak3t[1]-Npeak3t[2]))+ abs((Npeak1v[1]-Npeak4v[2])*((Npeak1t[1]+12)-Npeak4t[2])) }) } else if({indx[1]==0 indx[2]==1 indx[3]==1 indx[4]==1}) #4 peak 1 peak comparison {x8- x1[order(x1$Peak2t,x1$Peak3t,x1$Peak4t,x1$Npeak2t,x1$Npeak3t,x1$Npeak4t),]; with(x8,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+ abs((Peak1v[1]-Peak2v[2])*((Peak1t[1]+12)-Peak2t[2]))+ abs((Peak1v[1]-Peak3v[2])*((Peak1t[1]+12)-Peak3t[2]))+abs((Peak1v[1]-Peak4v[2])*((Peak1t[1]+12)-Peak4t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+ abs((Npeak1v[1]-Npeak2v[2])*((Npeak1t[1]+12)-Npeak2t[2]))+ abs((Npeak1v[1]-Npeak3v[2])*((Npeak1t[1]+12)-Npeak3t[2]))+abs((Npeak1v[1]-Npeak4v[2])*((Npeak1t[1]+12)-Npeak4t[2]))}) } else ({ #cases where peaks are similar with(x1,{abs((Peak1v[1]-Peak1v[2])*(Peak1t[1]-Peak1t[2]))+abs((Peak2v[1]-Peak2v[2])*(Peak2t[1]-Peak2t[2]))+abs((Peak3v[1]-Peak3v[2])*(Peak3t[1]-Peak3t[2]))+abs((Peak4v[1]-Peak4v[2])*(Peak4t[1]-Peak4t[2]))+ abs((Npeak1v[1]-Npeak1v[2])*(Npeak1t[1]-Npeak1t[2]))+abs((Npeak2v[1]-Npeak2v[2])*(Npeak2t[1]-Npeak2t[2]))+abs((Npeak3v[1]-Npeak3v[2])*(Npeak3t[1]-Npeak3t[2]))+abs((Npeak4v[1]-Npeak4v[2])*(Npeak4t[1]-Npeak4t[2])) }) }) } res2-do.call(cbind,lapply(seq_len(ncol(res)),function(i) c(c(tail(res[seq(1,i,1),i],-1),0),res[-c(1:i),i]))) row.names(res2)-1:nrow(res2) res2[1:5,1:5] # [,1] [,2] [,3] [,4] [,5] #1 0. 0. 0. 857.6834 0.000 #2 0. 0. 0. 611.1167 0.000 #3 0. 0. 0. 854.3765 0.000 #4 857.6834 611.1167 854.3765 0. 579.756 #5 0. 0. 0. 579.7560 0.000 dim(res2) #[1] 124 124 I also validated each of the cases by calculating the values: For example: stations 1 and 4, your previous email indicated but the distance between 1 and 4 is not 379.1364, actually its 1495.01 Based on my calculation: dat1[c(1,4),] # St. Peak1.t. Peak2.t. Peak3.t. Peak4.t. Npeak1.t. Npeak2.t. Npeak3.t. #1 1 5 10 0 0 7 13 0 #4 4 4 8 10 0 6 9 14 # Npeak4.t. Peak1.v. Peak2.v. Peak3.v. Peak4.v. Npeak1.v. Npeak2.v. Npeak3.v. #1 0 56.28785 17.43170