[R] avoid row/column indexing in capture.output
Hi everyone, This might be a very petty thing but Its not working for me. I want to export an output to a txt file but without indexing. Here is what I have tried to do outfile- function(Time, var.names, output) { var.names = c(names(para)) for(i in 1: ncol(output)){ cat(length(var.names), '\n') cat(A, S, C, I, '\n') cat(time =, yes, '\n') cat(RUN, i, '\n') cat(length(Time), '\n') print(cbind(Time, output[,i])) } } This works fine and I get the output like as follows 4 A S C I time = yes RUN 1 111 [,1] [,2] [1,] 0.00 1.750 [2,] 0.05 0.3983540 [3,] 0.10 0.6010150 [4,] 0.15 0.6759570 [5,] 0.20 0.7165215 [6,] 0.25 0.7423391 [7,] 0.30 0.7603594 [8,] 0.35 0.7737155 [9,] 0.40 0.7840432 [10,] 0.45 0.7922850 . . But I need need to have an output like this 4 A S C I time = yes RUN 1 111 0.00 1.750 0.05 0.3983540 0.10 0.6010150 ... Any kind of help will be highly appreciated. Thank you in advance -- Acharya, Subodh [[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 with iteration using while loop
Hello everyone, I am trying to use while loop to iterate a function until convergence. But I am having problem when I try to use a fixed number of iterations. Say I want to use maximum iteration of 150. If the value don't converge within maximum iteration, show warning of no convergence. Currently I don't have non- convergence problem so I think my code works fine. But in future I may encounter such problem that are likely to not converge easily. Below is my function that is working when I don't provide maximum iteration. iter- function (Fpi, Time, tolerance){ S = 22.4 Ts = 0.499 Ti = 0.25 K = 0.044 r- 1.5 M = Ts- Ti Ks = 0.044 Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) while((Fpt-Fpi) tolerance) { Fpi = Fpt Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) Fp0 = Fpt } return(Fpt) } x- iter(Fpi = 0.224, Time = 0.2, tolerance = 0.01) But I want do something like this ( conceptually) for( i in 2:itermax) { Fpt[i] = K*Time + M*S*log(1+ Fpi/(M*S)) if((Fpt[i]- Fpt[i-1])= tolerance) break print(Fpt[i] } something like this. any kind of help is highly appreciated. thank you -- Acharya, Subodh [[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] Help with iteration using while loop
Hi Steve, Actually I figured that out after I emailed you but I forgot to email you before I left office. Anyway thanks a lot.And thanks for the note. I'll do that from now on. Subodh On Fri, Jun 4, 2010 at 4:54 PM, Steve Lianoglou mailinglist.honey...@gmail.com wrote: Hi Subodh, Minor note: please keep replies on list so everyone benefits from answers/questions. Now: On Fri, Jun 4, 2010 at 2:49 PM, Subodh Acharya shoeb...@gmail.com wrote: Thanks a lot Steve, It worked. I appreciate. But I have another question, may be thats trivial. Say, it doesn't converge at itermax. I need to display error message saying values don't converge at at itermax. Is there another statement that accompanies while for this? Perhaps you can use warning(...). You can check whether (or not) your Fpt - Fpi is tolerance after you loop. If it is, you know your loop terminated because you hit itermax, and not because your algo converged: ... ... iter - 0 while (((Fpt - Fpi) tolerance) (iter itermax)) { Fpi = Fpt Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) Fp0 = Fpt iter - iter + 1 } if (Fpt - Fpi tolerance) { warning(Algorithm reached itermax and did not converge) } ... -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact -- Acharya, Subodh [[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] finite difference scheme for 2D differential equations
Hello everyone, I am trying to solve 2D differential equations using finite difference scheme in R. I have been able to work with the equations with only one spatial dimensions but I want to extend it to the two dimensional problem. For example i can simulate one dimensional diffusion using a code like the following. But I want to write a similar code for,say, a two dimensional diffusion equation. Any kind of help/advice is highly appreciated. Here is how I did for the 1D equation (in this case for 1D diffusion) Equation: dc/dt = D*d^2c/dx^2 dx = 10 dt = 0.1 D = 15 n = 20 tstep = 200 C = 50 dt = 0.1 dx = 10 conc-matrix(0,tstep,n) conc[1,1:10] = C for (i in 2:tstep) { for (j in 1:n){ if (j==1){ conc[i,j] = conc[i-1,j] - dt*(D/dx^2)*(conc[i-1,j] - conc[i-1,j+1]) } conc[i,j] = conc[i,j] if(j 1 j n){ conc[i,j] = conc[i-1,j] + dt*(D/dx^2)* (conc[i-1,(j-1)] - 2*conc[i-1,j] + conc[i-1,(j+1)]) } if (j==n){ conc[i,n] = conc[i-1,n] + dt*(D/dx^2)* ( conc[i-1,n-1] - conc[i-1,n]) } conc[i,j] = conc[i,j] } } Now in 2D the equation will be like this dc/dt = Dx*d^2c/dx^2 + Dy*d^C/dy^2 So that when I solve it I will get C(x, y, t) at each node of the grid. Thanks for the help in advance. -- Acharya, Subodh [[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] Filtering a zoo object based on index of another object
Hello everybody, I have two datasets, observed and predicted. Since my observed dataset is not in regular intervals, I need to filter my predicted dataset based on the measurement date of my observed data. Here, is an example similar to what I have library(chron);library(zoo) DATE- seq(as.Date(2009-01-01), as.Date(2009-05-01), by = 1) mydat- rnorm(length(DATE), 20,5) myzoo- zoo(mydat, order.by = DATE) DATE2- seq(as.Date(2009-01-01), as.Date(2009-01-30), by = 7) Now I need to create a new zoo object with index as DATE2 and corresponding data values. Any kind of help will be highly appreciated. Thanks in advance you very much -- Acharya, Subodh [[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] option to control the spac between columns in data frame
Hello, I have a question regarding a way to control the appreance of output exported by R when I use capture.output( x, file = Directory/file.txt) , I get a text file which when I paste to a word file looks like the first table below. The following table has its clumns spaced closely so when I paste it to a word file it looks continuous. Is there any option in R to make the outputs like the second table? Any help will be highly appreciated. Thank you very much in advance. Model 1 4/70-3/80 Year 2 Source Area Runoff Nitrogen Phosphorus V1 LDR_imperv 203 52.6 1496.0 190.0 V2 LDR_perv 11475.7489.6 163.6 V3 MDR_imperv 281 52.6 4141.7 654.7 V4 MDR_Perv 6545.7511.8 227.4 V5 IND_imperv 97 52.6 1604.4 226.0 V6 IND_perv 425.7 17.9 7.1 V7 COM_imperv 158 52.6 2613.4 368.1 V8 COM_perv 1575.7 67.0 26.6 Total 10942 1302.2 Source Area Runoff Nitrogen Phosphorus V1 LDR_imperv 20352.6 1496.0 190.0 V2 LDR_perv1147 5.7 489.6 163.6 V3 MDR_imperv 281 52.64141.7 654.7 V4 MDR_Perv 654 5.7 511.8 227.4 V5 IND_imperv 97 52.6 1604.4 226.0 -- Acharya, Subodh [[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] Formatting outputs:(chronological object)
Hello everyone, I have a data generated in a way similar to the following library(chron);library(zoo) date- seq(as.Date(1990-01-01),, as.Date(2000-12-31), by = 1) obs- zoo(rnorm(length(date), mean = 10, sd = 2.5), order.by = date) monthly- function(date) as.Date(as.yearmon(Date)) result- data.frame ( Date = obs = aggregate(obs, monthly, sum)) Now, I want to get the result data in the following format instead of the continuous table Observation for year 1990, 1990-01-01 314.7547 1990-02-01 266.0105 1990-03-01 353.7275 1990-04-01 273.8321 1990-05-01 325.3691 1990-06-01 253.1501 1990-07-01 318.1684 1990-08-01 291.2097 1990-09-01 298.6409 1990-10-01 304.0167 1990-11-01 279.4668 1990-12-01 286.9456 Total for year 1990 = xxx Observation for year 1991 1991-01-01 313.1515 1991-02-01 297.9274 1991-03-01 315.2565 1991-04-01 290.9797 1991-05-01 327.0480 1991-06-01 281.5030 1991-07-01 316.7332 1991-08-01 289.9122 1991-09-01 296.9334 1991-10-01 280.2053 1991-11-01 295.6098 1991-12-01 321.6451 Total for year 1991 = xxx Any help would be highly appreciated Thanks -- Acharya, Subodh [[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 with date specification
Hi everyone,I have a data daily data (x) for 10 years starting from 04-01-1995 to 03-31-2005. I was able to get the yearly sum for the ten years using aggregate(x, years, sum). But this gave me the yearly sum for 1995 (Apr- Dec); 1996 (Jan-Dec) -2005 (Jan-Mar). But I want to get the aggregates for Apr-1995 to Mar 1996, Apr 1996- mar 1997 and so on. your help will be higly appreciated. Thanks in advance -- Acharya, Subodh [[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 with data containing date
Hello Everyone,I think this is a very simple problem, I have been struggling with it for a few days now. I have a 10-year daily data in the following format. Date ABC D E 1978-10-22 18 20.64 0.0 0.176 -1.76 1978-10-23 15 17.06 0.4 0.147 2.52 1978-10-243 7.588 0.0 0.068 -6.86 1978-10-259 11.491 0.0 0.102-1.01 1978-10-26 13 14.98 1.40.130 1.26 I want to calculate the monthly and Annual average averages of A, B, C, D, and E, for the 10 years. I tried to use the xts package to convert the data into a time series object but was not able to even change it into the time series object. Any help would be highly appreciated Thank you in advance. -- Subodh Acharya University of Florida Gainesville, FL. [[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 with use of rep function in R
Dear List,I am trying to use rep function in the following conditions A = c( 5, 6, 7, 11, 9, 12, 10, 15) B = c(12,15, 21, 31, 25, 27,32, *34*,13,12, 34, 33, 24, 29, 26, *28*,22,14,27,22,21,12,32, 16) I need to repeat each element of A, as many times as each element of B, for the entire length of B. for example, repeat 5, for 12 times, 6 for 15 times,, 15 for 34 times, and then, again, 5 for 13 times, 6 for 12 times,, 15 for 28 times, and so on. I used, the function rep(A, times = B) It didn't work because apparently, the times command , worked for only if the length of A and B was equal. Thank you for your help in advance. -- Subodh Acharya University of Florida Gainesville, FL. [[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 with use of rep function in R
Dear List,I am trying to use rep function in the following conditions A = c( 5, 6, 7, 11, 9, 12, 10, 15) B = c(12,15, 21, 31, 25, 27,32, *34*,13,12, 34, 33, 24, 29, 26, *28*,22,14,27,22,21,12,32, 16) I need to repeat each element of A, as many times as each element of B, for the entire length of B. for example, repeat 5, for 12 times, 6 for 15 times,, 15 for 34 times, and then, again, 5 for 13 times, 6 for 12 times,, 15 for 28 times, and so on. I used, the function rep(A, times = B) It didn't work because apparently, the times command , worked for only if the length of A and B was equal. Thank you for your help in advance. -- Subodh Acharya University of Florida Gainesville, FL. [[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.