Re: [R] how to subtotal by rows
Hello Shyam, This is one way to do it jd1 - read.table(text= fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7 , sep=, header=T) jd1 library(plyr) ddply(jd1,.(fid,year),summarise, rice=sum(rice,na.rm=T),wheat=sum(wheat,na.rm=T),maize=sum(maize,na.rm=T)) Good luck Janesh On Fri, Apr 19, 2013 at 10:59 AM, shyam basnet shyamabc2...@yahoo.comwrote: Dear R-users, I have a dataset as like below, and I want to subtotal the values of rice,wheat and maize by year for each fid. fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7--- And, my output should look like below: fid year rice wheat maize 11995 53 2 11996 42 6 21995 38 4 21996 76 7I am looking for some ideas or r-codes on resolving my problem. I appreciate your kind help, Thanks a lot, Sincerely yours, Shyam Nepal [[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] how to subtotal by rows
You can also use this short command. library(plyr) ddply(jd1,.(fid,year),colSums,na.rm=T) Janesh On Fri, Apr 19, 2013 at 2:30 PM, Janesh Devkota janesh.devk...@gmail.comwrote: Hello Shyam, This is one way to do it jd1 - read.table(text= fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7 , sep=, header=T) jd1 library(plyr) ddply(jd1,.(fid,year),summarise, rice=sum(rice,na.rm=T),wheat=sum(wheat,na.rm=T),maize=sum(maize,na.rm=T)) Good luck Janesh On Fri, Apr 19, 2013 at 10:59 AM, shyam basnet shyamabc2...@yahoo.comwrote: Dear R-users, I have a dataset as like below, and I want to subtotal the values of rice,wheat and maize by year for each fid. fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7--- And, my output should look like below: fid year rice wheat maize 11995 53 2 11996 42 6 21995 38 4 21996 76 7I am looking for some ideas or r-codes on resolving my problem. I appreciate your kind help, Thanks a lot, Sincerely yours, Shyam Nepal [[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] how to subtotal by rows
Thanks, Arun. I was also trying to come up with that solution. On Fri, Apr 19, 2013 at 2:43 PM, arun smartpink...@yahoo.com wrote: #or ddply(jd1,.(fid,year),numcolwise(sum,na.rm=TRUE)) # fid year rice wheat maize #1 1 19955 3 2 #2 1 19964 2 6 #3 2 19953 8 4 #4 2 19967 6 7 A.K. - Original Message - From: Janesh Devkota janesh.devk...@gmail.com To: shyam basnet shyamabc2...@yahoo.com Cc: r-help@R-project.org r-help@r-project.org Sent: Friday, April 19, 2013 3:30 PM Subject: Re: [R] how to subtotal by rows Hello Shyam, This is one way to do it jd1 - read.table(text= fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7 , sep=, header=T) jd1 library(plyr) ddply(jd1,.(fid,year),summarise, rice=sum(rice,na.rm=T),wheat=sum(wheat,na.rm=T),maize=sum(maize,na.rm=T)) Good luck Janesh On Fri, Apr 19, 2013 at 10:59 AM, shyam basnet shyamabc2...@yahoo.com wrote: Dear R-users, I have a dataset as like below, and I want to subtotal the values of rice,wheat and maize by year for each fid. fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA2 NA 11996 NANA 6 21995 3NA NA 21995 NA8 NA 21995 NANA 4 21996 7NA NA 21996 NA6 NA 21996 NANA 7--- And, my output should look like below: fid year rice wheat maize 11995 53 2 11996 42 6 21995 38 4 21996 76 7I am looking for some ideas or r-codes on resolving my problem. I appreciate your kind help, Thanks a lot, Sincerely yours, Shyam Nepal [[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. [[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] Spider Plot
Xing, I cannot open the attachment. Can you attach the png file or pdf or any other format ? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of XINLI LI Sent: Friday, April 19, 2013 3:30 PM To: r-help Subject: [R] Spider Plot Does any one have a sample code for a Spider Plot as attached? Thanks, Xing __ 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] Subsetting a large number into smaller numbers and find the largest product
Hello, I have a big number lets say of around hundred digits. I want to subset that big number into consecutive number of 5 digits and find the product of those 5 digits. For example my first 5 digit number would be 73167. I need to check the product of the individual numbers in 73167 and so on. The sample number is as follows: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota [[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] Merge
Hi, I have a quick question here. Lets say he has three data frames and he needs to combine those three data frame using merge. Can we simply use merge to join three data frames ? I remember I had some problem using merge for more than two dataframes. Thanks. On Wed, Apr 17, 2013 at 1:05 AM, Farnoosh farnoosh...@yahoo.com wrote: Thanks a lot:) Sent from my iPad On Apr 16, 2013, at 10:15 PM, arun smartpink...@yahoo.com wrote: Hi Farnoosh, YOu can use either ?merge() or ?join() DataA- read.table(text= ID v1 1 10 2 1 3 22 4 15 5 3 6 6 7 8 ,sep=,header=TRUE) DataB- read.table(text= ID v2 2 yes 5 no 7 yes ,sep=,header=TRUE,stringsAsFactors=FALSE) merge(DataA,DataB,by=ID,all.x=TRUE) # ID v1 v2 #1 1 10 NA #2 2 1 yes #3 3 22 NA #4 4 15 NA #5 5 3 no #6 6 6 NA #7 7 8 yes library(plyr) join(DataA,DataB,by=ID,type=left) # ID v1 v2 #1 1 10 NA #2 2 1 yes #3 3 22 NA #4 4 15 NA #5 5 3 no #6 6 6 NA #7 7 8 yes A.K. From: farnoosh sheikhi farnoosh...@yahoo.com To: smartpink...@yahoo.com smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 12:52 AM Subject: Merge Hi Arun, I want to merge a data set with another data frame with 2 columns and keep the sample size of the DataA. DataA DataB DataCombine ID v1 ID V2 ID v1 v2 1 10 2 yes 1 10 NA 2 1 5 no 2 1 yes 3 22 7 yes 3 22 NA 4 15 4 15 NA 5 3 5 3 no 6 6 6 6 NA 7 8 7 8 yes Thanks a lot for your help and 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. [[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] Merge
Hello Arun, Thank you so much for the prompt reply. I have one simple question here. DOes three dots (...) in the reduce function means we are applying for three dataframes here ? So, if we were to combine four would that dots be four dots ? Thanks. On Wed, Apr 17, 2013 at 12:16 PM, arun smartpink...@yahoo.com wrote: HI Janesh, YOu can use: library(plyr) ?join_all() #From the help page: dfs - list( a = data.frame(x = 1:10, a = runif(10)), b = data.frame(x = 1:10, b = runif(10)), c = data.frame(x = 1:10, c = runif(10)) ) join_all(dfs) join_all(dfs, x) join_all(dfs, x) #x a b c #1 1 0.7113766 0.1348978 0.1153703 #2 2 0.2520057 0.7249154 0.2362936 #3 3 0.5670157 0.8166805 0.3049683 #4 4 0.7441726 0.4929165 0.6779029 #5 5 0.5616914 0.5272339 0.6202915 #6 6 0.2858429 0.1203205 0.8399356 #7 7 0.9910520 0.1251815 0.4729418 #8 8 0.7079778 0.5465055 0.8951371 #9 9 0.0564100 0.1837211 0.6451289 #10 10 0.7169663 0.1328287 0.2467554 Reduce(function(...) merge(...,by=x),dfs) #x a b c #1 1 0.7113766 0.1348978 0.1153703 #2 2 0.2520057 0.7249154 0.2362936 #3 3 0.5670157 0.8166805 0.3049683 #4 4 0.7441726 0.4929165 0.6779029 #5 5 0.5616914 0.5272339 0.6202915 #6 6 0.2858429 0.1203205 0.8399356 #7 7 0.9910520 0.1251815 0.4729418 #8 8 0.7079778 0.5465055 0.8951371 #9 9 0.0564100 0.1837211 0.6451289 #10 10 0.7169663 0.1328287 0.2467554 A.K. From: Janesh Devkota janesh.devk...@gmail.com To: Farnoosh farnoosh...@yahoo.com Cc: arun smartpink...@yahoo.com; R help r-help@r-project.org Sent: Wednesday, April 17, 2013 1:05 PM Subject: Re: [R] Merge Hi, I have a quick question here. Lets say he has three data frames and he needs to combine those three data frame using merge. Can we simply use merge to join three data frames ? I remember I had some problem using merge for more than two dataframes. Thanks. On Wed, Apr 17, 2013 at 1:05 AM, Farnoosh farnoosh...@yahoo.com wrote: Thanks a lot:) Sent from my iPad On Apr 16, 2013, at 10:15 PM, arun smartpink...@yahoo.com wrote: Hi Farnoosh, YOu can use either ?merge() or ?join() DataA- read.table(text= ID v1 1 10 2 1 3 22 4 15 5 3 6 6 7 8 ,sep=,header=TRUE) DataB- read.table(text= ID v2 2 yes 5 no 7 yes ,sep=,header=TRUE,stringsAsFactors=FALSE) merge(DataA,DataB,by=ID,all.x=TRUE) # ID v1 v2 #1 1 10 NA #2 2 1 yes #3 3 22 NA #4 4 15 NA #5 5 3 no #6 6 6 NA #7 7 8 yes library(plyr) join(DataA,DataB,by=ID,type=left) # ID v1 v2 #1 1 10 NA #2 2 1 yes #3 3 22 NA #4 4 15 NA #5 5 3 no #6 6 6 NA #7 7 8 yes A.K. From: farnoosh sheikhi farnoosh...@yahoo.com To: smartpink...@yahoo.com smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 12:52 AM Subject: Merge Hi Arun, I want to merge a data set with another data frame with 2 columns and keep the sample size of the DataA. DataA DataB DataCombine ID v1 ID V2 ID v1 v2 1 10 2 yes 1 10 NA 2 1 5 no 2 1 yes 3 22 7 yes 3 22 NA 4 15 4 15 NA 5 3 5 3 no 6 6 6 6 NA 7 8 7 8 yes Thanks a lot for your help and 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. [[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] Change the default resolution for plotting figures?
I have been using the following so far without having any problems: dev.copy(png,sample.png,width=8, height=10, units=in,res=500) dev.off() On Tue, Apr 16, 2013 at 6:32 PM, jt...@mappi.helsinki.fi wrote: Hi, I want to save a plot in the windows device as png and the default resolution is 72dpi. Is it possible to increase the default resolution to for example 300 dpi? I have thought of using function png(..., res=300), but the problem is that the figure produced this way looks different than the one shown in the windows device. One notable difference is the missing of some ticks in the x axis. Therefore I would rather to produce the figure in a window device and then save it as a png. Unfortunately in the device window there is no such an option to change the resolution. Little information can be found so far. Any ideas are appreciated! Best, Jing __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Read the data from a text file and reshape the data
Hi Arun, Thank you so much for your answer. It surely does help. Having to know different approaches for the same problem has given me more insights on R language. I appreciate your time and effort. Best, Janesh Devkota On Thu, Apr 11, 2013 at 10:00 PM, arun smartpink...@yahoo.com wrote: Hi, May be this helps: lines1- readLines(WAT_DEP.DAT.part) indx- which(grepl([*],lines1)) indx2-indx[seq(from=indx[2],length(indx),by=2)]+1 lines2-str_trim(lines1[indx2],side=left) dat1-read.table(text=lines2,sep=,header=FALSE) library(stringr) lst1- lapply(split(indx,((seq_along(indx)-1)%/%2)+1), function(x) {x1-seq(max(x)+2,max(x)+2+49,by=1); x2- str_trim(lines1[x1][!is.na(lines1[x1])],side=left); x3-as.vector(as.matrix(read.table(text=x2[x2!=],header=FALSE,sep=))); x4- if(length(x3) 500) c(x3,rep(NA,500-length(x3))) else x3;x4 }) dat2- as.data.frame(do.call(cbind,lst1)) colnames(dat2)-paste(t,colnames(dat2),_,dat1[,2],sep=) dim(dat2) #[1] 500 622 dat2[1:3,1:3] # t1_0.00208 t2_0.00417 t3_0.00625 #1 3.224 4.124 4.502 #2 3.205 4.118 4.500 #3 3.189 4.114 4.498 A.K. - Original Message - From: Janesh Devkota janesh.devk...@gmail.com To: r-help@r-project.org Cc: Sent: Thursday, April 11, 2013 5:55 PM Subject: [R] Read the data from a text file and reshape the data I have a data set for different time intervals. The data has three comment lines before data for each time interval. For each time interval there are 500 data points. I want to change the dataset such that I have the following format: t1t2t3 0.00208 0.00417 0.00625 . a1 a2 a3 ... b1 b2 b3 ... c1c2c3 . ... The link to the file is as follows: https://www.dropbox.com/s/hc8n3qcai1mlxca/WAT_DEP.DAT As you will see on the file, time for each interval is the second data of the third line before the data starts. For the first time, t= 0.00208. I need to change the data in several rows into one column. At last I need to create a dataframe with the format shown above. In the sample above, a1, b1, c1 are the data for time t1, and so on. The sample data is as follows: ** N:SNAPSHOTTIME DELT[S] ** WATER DEPTH [M]: (HP(L),L=2,LA) 18000.00208 0.1 3.224 3.221 3.220 3.217 3.216 3.214 3.212 3.210 3.209 3.207 3.205 3.203 3.202 3.200 3.199 3.197 3.196 3.193 3.192 3.190 3.189 3.187 3.186 3.184 3.184 3.182 3.181 3.179 3.178 3.176 3.175 3.174 3.173 3.171 3.170 3.169 3.168 3.167 3.166 3.164 3.164 3.162 3.162 3.160 3.160 3.158 3.158 3.156 3.156 3.155 3.154 3.153 3.152 3.151 3.150 3.150 3.149 3.149 3.147 3.147 3.146 3.146 3.145 3.145 3.144 3.144 3.143 3.143 3.142 3.142 3.141 3.142 3.141 3.141 3.140 3.141 3.140 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.140 3.140 3.140 3.141 3.141 3.142 3.141 3.142 3.142 3.142 3.143 3.143 3.144 3.144 3.145 3.145 3.146 3.146 3.147 3.148 3.149 3.149 3.150 3.150 3.152 3.152 3.153 3.154 3.155 3.156 3.157 3.158 3.159 3.160 3.161 3.162 3.163 3.164 3.165 3.166 3.168 3.169 3.170 3.171 3.173 3.174 3.176 3.176 3.178 3.179 3.181 3.182 3.184 3.185 3.187 3.188 3.190 3.191 3.194 3.195 3.196 3.198 3.199 3.202 3.203 3.205 3.207 3.209 3.210 3.213 3.214 3.217 3.218 3.221 3.222 3.225 3.226 3.229 3.231 3.233 3.235 3.238 3.239 3.242 3.244 3.247 3.248 3.251 3.253 3.256 3.258 3.261 3.263 3.266 3.268 3.271 3.273 3.276 3.278 3.281 3.283 3.286 3.289 3.292 3.294 3.297 3.299 3.303 3.305 3.307 3.311 3.313 3.317 3.319 3.322 3.325 3.328 3.331 3.334 3.337 3.340 3.343 3.347 3.349 3.353 3.356 3.359 3.362 3.366 3.369 3.372 3.375 3.379 3.382 3.386 3.388 3.392 3.395 3.399 3.402 3.406 3.409 3.413
Re: [R] Solving an integral in R gives the error The integral is probably divergent
Hi Peter, It is supposed to be t^{3/2} instead of t^{0.5}. The code had a typo. Yes, I figured out that when x =0, the solution is divergent because it is like dividing something by zero. Thanks. Best, Janesh -Original Message- From: peter dalgaard [mailto:pda...@gmail.com] Sent: Friday, April 12, 2013 1:32 PM To: Thomas Lumley Cc: Janesh Devkota; groep R-help Subject: Re: [R] Solving an integral in R gives the error The integral is probably divergent But is it supposed to be t^{-3/2} or t^{-0.5}?? The formula has the former and the code the latter, and the integral is clearly divergent with the former. -pd On Apr 12, 2013, at 04:51 , Thomas Lumley wrote: I don't get an error message (after I correct the missing line break after the comment b- sapply(a, Cfun, upper=1) b [1] 1.583458e-54 7.768026e-50 2.317562e-45 4.206260e-41 4.645737e-37 3.123801e-33 1.279358e-29 3.193257e-26 4.860876e-23 [10] 4.516582e-20 2.564400e-17 8.908932e-15 1.896996e-12 2.481084e-10 1.998561e-08 9.946570e-07 3.067751e-05 5.862075e-04 [19] 6.818952e-03 4.297061e-02 0.00e+00 3.175122e-01 3.723022e-01 2.364930e-01 9.144836e-02 2.190878e-02 3.252754e-03 [28] 2.983763e-04 1.685692e-05 5.849602e-07 1.244158e-08 1.619155e-10 1.287603e-12 6.250149e-15 1.850281e-17 3.338241e-20 [37] 3.668412e-23 2.454192e-26 9.991546e-30 2.474577e-33 3.727226e-37 3.413319e-41 1.900112e-45 6.428505e-50 1.321588e-54 [46] 1.650722e-59 1.252524e-64 5.772750e-70 1.615916e-75 2.746972e-81 2.835655e-87 1.777399e-93 6.764271e-100 1.562923e-106 [55] 2.192373e-113 1.866955e-120 9.651205e-128 3.028623e-135 5.769185e-143 6.670835e-151 4.682023e-159 1.994643e-167 5.157808e-176 [64] 8.095084e-185 7.711162e-194 4.458042e-203 1.564139e-212 3.330362e-222 4.302974e-232 3.373500e-242 1.604721e-252 4.631224e-263 [73] 8.108474e-274 8.611898e-285 5.547745e-296 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 [82] 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 [91] 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 [100] 0.00e+00 -thomas On Tue, Apr 9, 2013 at 3:14 PM, Janesh Devkota janesh.devk...@gmail.comwrote: I am trying to solve an integral in R. However, I am getting an error when I am trying to solve for that integral. The equation that I am trying to solve is as follows: $$ C_m = \frac{{abs{x}}e^{2x}}{\pi^{1/2}}\int_0^t t^{-3/2}e^{-x^2/t-t}dt $$ [image: enter image description here] The code that I am using is as follows: a - seq(from=-10, by=0.5,length=100) ## Create a function to compute integrationCfun - function(XX, upper){ integrand - function(x)x^(-0.5)*exp((-XX^2/x)-x) integrated - integrate(integrand, lower=0, upper=upper)$value (final - abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) } b- sapply(a, Cfun, upper=1) The error that I am getting is as follows: Error in integrate(integrand, lower = 0, upper = upper) : the integral is probably divergent Does this mean I cannot solve the integral ? Any possible ways to fix this problem will be highly appreciated.The question can be found on http://stackoverflow.com/questions/15892586/solving-an-integral-in-r- gives-error-the-integral-is-probably-divergent also. 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. -- Thomas Lumley Professor of Biostatistics University of Auckland [[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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Read the data from a text file and reshape the data
I have a data set for different time intervals. The data has three comment lines before data for each time interval. For each time interval there are 500 data points. I want to change the dataset such that I have the following format: t1t2t3 0.00208 0.00417 0.00625 . a1 a2 a3 ... b1 b2 b3 ... c1c2c3 . ... The link to the file is as follows: https://www.dropbox.com/s/hc8n3qcai1mlxca/WAT_DEP.DAT As you will see on the file, time for each interval is the second data of the third line before the data starts. For the first time, t= 0.00208. I need to change the data in several rows into one column. At last I need to create a dataframe with the format shown above. In the sample above, a1, b1, c1 are the data for time t1, and so on. The sample data is as follows: ** N:SNAPSHOTTIME DELT[S] ** WATER DEPTH [M]: (HP(L),L=2,LA) 18000.00208 0.1 3.224 3.221 3.220 3.217 3.216 3.214 3.212 3.210 3.209 3.207 3.205 3.203 3.202 3.200 3.199 3.197 3.196 3.193 3.192 3.190 3.189 3.187 3.186 3.184 3.184 3.182 3.181 3.179 3.178 3.176 3.175 3.174 3.173 3.171 3.170 3.169 3.168 3.167 3.166 3.164 3.164 3.162 3.162 3.160 3.160 3.158 3.158 3.156 3.156 3.155 3.154 3.153 3.152 3.151 3.150 3.150 3.149 3.149 3.147 3.147 3.146 3.146 3.145 3.145 3.144 3.144 3.143 3.143 3.142 3.142 3.141 3.142 3.141 3.141 3.140 3.141 3.140 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.140 3.140 3.140 3.141 3.141 3.142 3.141 3.142 3.142 3.142 3.143 3.143 3.144 3.144 3.145 3.145 3.146 3.146 3.147 3.148 3.149 3.149 3.150 3.150 3.152 3.152 3.153 3.154 3.155 3.156 3.157 3.158 3.159 3.160 3.161 3.162 3.163 3.164 3.165 3.166 3.168 3.169 3.170 3.171 3.173 3.174 3.176 3.176 3.178 3.179 3.181 3.182 3.184 3.185 3.187 3.188 3.190 3.191 3.194 3.195 3.196 3.198 3.199 3.202 3.203 3.205 3.207 3.209 3.210 3.213 3.214 3.217 3.218 3.221 3.222 3.225 3.226 3.229 3.231 3.233 3.235 3.238 3.239 3.242 3.244 3.247 3.248 3.251 3.253 3.256 3.258 3.261 3.263 3.266 3.268 3.271 3.273 3.276 3.278 3.281 3.283 3.286 3.289 3.292 3.294 3.297 3.299 3.303 3.305 3.307 3.311 3.313 3.317 3.319 3.322 3.325 3.328 3.331 3.334 3.337 3.340 3.343 3.347 3.349 3.353 3.356 3.359 3.362 3.366 3.369 3.372 3.375 3.379 3.382 3.386 3.388 3.392 3.395 3.399 3.402 3.406 3.409 3.413 3.416 3.420 3.423 3.427 3.430 3.435 3.438 3.442 3.445 3.449 3.453 3.457 3.460 3.464 3.468 3.472 3.475 3.479 3.483 3.486 3.491 3.494 3.498 3.502 3.506 3.510 3.514 3.518 3.522 3.526 3.531 3.534 3.539 3.542 3.547 3.551 3.555 3.559 3.564 3.567 3.572 3.576 3.581 3.584 3.589 3.593 3.598 3.602 3.606 3.610 3.615 3.619 3.624 3.628 3.633 3.637 3.642 3.646 3.651 3.655 3.660 3.664 3.669 3.673 3.678 3.682 3.686 3.691 3.695 3.700 3.704 3.710 3.714 3.719 3.723 3.728 3.733 3.738 3.742 3.747 3.752 3.757 3.761 3.766 3.771 3.776 3.780 3.786 3.790 3.795 3.800 3.805 3.810 3.815 3.819 3.825 3.829 3.835 3.839 3.845 3.849 3.855 3.859 3.865 3.869 3.875 3.879 3.885 3.889 3.895 3.900 3.905 3.910 3.915 3.920 3.926 3.930 3.935 3.941 3.945 3.951 3.956 3.961 3.966 3.972 3.976 3.982 3.987 3.993 3.997 4.003 4.008 4.014 4.018 4.024 4.029 4.035 4.039 4.045
Re: [R] Read the data from a text file and reshape the data
Hi Jim, Yes, this is exactly what I was looking for. The code works perfect. Thanks a lot for your time and effort. Best, Janesh Devkota From: jim holtman [mailto:jholt...@gmail.com] Sent: Thursday, April 11, 2013 7:32 PM To: Janesh Devkota Cc: R mailing list Subject: Re: [R] Read the data from a text file and reshape the data Is this what you are looking for: input - readLines(C:\\Users\\Owner\\Downloads\\WAT_DEP.DAT) start - grep(N:SNAPSHOT, input) # find start of the data # add index of what would have been the last block start - c(start, tail(start, 1) + 53L) # now read in the data using 'text' parameter of 'scan' columns - lapply(seq(length(start) - 1L) + , function(indx){ + scan(text = input[(start[indx] + 3L):(start[indx + 1L] - 1L)] + , what = 0 # read in numeric + , quiet = TRUE + ) + } + ) # create the columns columns - do.call(cbind, columns) # add column names based on the time cNames - vapply(seq(length(start) - 1L) + , function(indx){ + scan(text = input[start[indx] + 2L] + , what = 0 + , quiet = TRUE + )[2L] # only the second value + } + , 1.0 # numeric + ) colnames(columns) - cNames # sample data columns[1:10, 1:10] 0.00208 0.00417 0.00625 0.00833 0.01042 0.0125 0.01458 0.01667 0.01875 0.02083 [1,] 3.224 4.124 4.502 4.649 4.705 4.726 4.734 4.737 4.738 4.738 [2,] 3.221 4.123 4.501 4.649 4.704 4.725 4.733 4.736 4.737 4.738 [3,] 3.220 4.123 4.502 4.649 4.705 4.726 4.734 4.737 4.738 4.738 [4,] 3.217 4.122 4.501 4.648 4.704 4.725 4.733 4.736 4.737 4.738 [5,] 3.216 4.122 4.501 4.649 4.705 4.726 4.734 4.737 4.738 4.738 [6,] 3.214 4.121 4.500 4.648 4.704 4.725 4.733 4.736 4.737 4.738 [7,] 3.212 4.121 4.501 4.649 4.705 4.726 4.734 4.737 4.738 4.738 [8,] 3.210 4.120 4.500 4.648 4.704 4.725 4.733 4.736 4.737 4.738 [9,] 3.209 4.120 4.500 4.649 4.705 4.726 4.734 4.737 4.738 4.738 [10,] 3.207 4.119 4.500 4.648 4.704 4.725 4.733 4.736 4.737 4.738 On Thu, Apr 11, 2013 at 5:55 PM, Janesh Devkota janesh.devk...@gmail.com wrote: I have a data set for different time intervals. The data has three comment lines before data for each time interval. For each time interval there are 500 data points. I want to change the dataset such that I have the following format: t1t2t3 0.00208 0.00417 0.00625 . a1 a2 a3 ... b1 b2 b3 ... c1c2c3 . ... The link to the file is as follows: https://www.dropbox.com/s/hc8n3qcai1mlxca/WAT_DEP.DAT As you will see on the file, time for each interval is the second data of the third line before the data starts. For the first time, t= 0.00208. I need to change the data in several rows into one column. At last I need to create a dataframe with the format shown above. In the sample above, a1, b1, c1 are the data for time t1, and so on. The sample data is as follows: ** N:SNAPSHOTTIME DELT[S] ** WATER DEPTH [M]: (HP(L),L=2,LA) 18000.00208 0.1 3.224 3.221 3.220 3.217 3.216 3.214 3.212 3.210 3.209 3.207 3.205 3.203 3.202 3.200 3.199 3.197 3.196 3.193 3.192 3.190 3.189 3.187 3.186 3.184 3.184 3.182 3.181 3.179 3.178 3.176 3.175 3.174 3.173 3.171 3.170 3.169 3.168 3.167 3.166 3.164 3.164 3.162 3.162 3.160 3.160 3.158 3.158 3.156 3.156 3.155 3.154 3.153 3.152 3.151 3.150 3.150 3.149 3.149 3.147 3.147 3.146 3.146 3.145 3.145 3.144 3.144 3.143 3.143 3.142 3.142 3.141 3.142 3.141 3.141 3.140 3.141 3.140 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.139 3.140 3.140 3.140 3.140 3.141 3.141 3.142 3.141 3.142 3.142 3.142 3.143 3.143 3.144 3.144 3.145 3.145 3.146 3.146 3.147 3.148 3.149 3.149 3.150 3.150 3.152 3.152 3.153 3.154 3.155 3.156 3.157 3.158
Re: [R] Plot two separate curves in R Graphics and R Lattice package
Hi, This should be fairly easy by using base R graphics. Lets suppose your first data is represented by (x1,y1) and second data is represented by (x2,y2) You can use the following command. plot(x1,y1,type=l) points(x2,y2) Hope it helps. On Tue, Apr 9, 2013 at 8:24 PM, jpm miao miao...@gmail.com wrote: Hi, How can I plot two curves with distinct x and y vectors? I would like to join one of them by regular lines and plot the other just by points (no lines). Can it be done in regular R graphic tools, say plot function? Can it be done in Lattice package, say xyplot function? Thanks, Miao My data look like this: two curves with different vector size x y 3973730 0.00322 2391576 0.003487 2840944 0.005145 2040943 0.006359 1982715 0.006253 1618162 0.00544 820082.3 0.004213 1658597 0.004883 1762794 0.006216 93439.5 0.004255 218481.3 0.006924 2332477 0.004862 725835.5 0.00089 811575.3 0.012962 292223 0.002614 153862.3 0.007524 1272367 0.006899 734199 0.00988 421404.5 0.005048 189047.5 0.004821 529102 0.009637 56833 0.006171 125856.3 0.00839 598893.8 0.006622 258240 0.00613 159086.3 0.008819 122863 0.010093 404699.5 0.008148 453514.5 0.008407 545028 0.006096 1233366 0.006111 1192758 0.008162 147563.3 0.00838 247293 0.010283 1074838 0.007413 459227.5 0.00862 202332 0.009061 377401.3 0.006923 1876753 0.010226 and x y 50118.72 0.012117 51286.14 0.012054 52480.75 0.011991 53703.18 0.011928 54954.09 0.011865 56234.13 0.011803 57543.99 0.011742 58884.37 0.01168 60255.96 0.011619 61659.5 0.011559 63095.73 0.011498 64565.42 0.011438 66069.34 0.011379 67608.3 0.011319 69183.1 0.01126 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Solving tridiagonal matrix in R
Dear R Users, I am trying to solve a tridiagonal matrix in R. I am wondering if there is an inbuilt R function or package to solve that. I tried looking on google but couldn't find something that would help directly. Any help is highly appreciated. Thanks. Janesh [[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] Solving an integral in R gives the error “The integral is probably divergent”
I am trying to solve an integral in R. However, I am getting an error when I am trying to solve for that integral. The equation that I am trying to solve is as follows: $$ C_m = \frac{{abs{x}}e^{2x}}{\pi^{1/2}}\int_0^t t^{-3/2}e^{-x^2/t-t}dt $$ [image: enter image description here] The code that I am using is as follows: a - seq(from=-10, by=0.5,length=100) ## Create a function to compute integrationCfun - function(XX, upper){ integrand - function(x)x^(-0.5)*exp((-XX^2/x)-x) integrated - integrate(integrand, lower=0, upper=upper)$value (final - abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) } b- sapply(a, Cfun, upper=1) The error that I am getting is as follows: Error in integrate(integrand, lower = 0, upper = upper) : the integral is probably divergent Does this mean I cannot solve the integral ? Any possible ways to fix this problem will be highly appreciated.The question can be found on http://stackoverflow.com/questions/15892586/solving-an-integral-in-r-gives-error-the-integral-is-probably-divergent also. 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] Read ADCP data in R
I have ADCP measured data for a river and I am wondering if it is possible to read the ADCP file in R. I found a package called oce but I couldn't read the ADCP file. The function I found in oce package is as follows: read.oce read.adp I have uploaded the sample file here https://www.dropbox.com/sh/owian354auah6h3/379D5spA2X. If anyone could help me how to read this kind of ADCP, I would highly appreciate. Thanks. Janesh Devkota [[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] Convert to date and time of the year
Dear R Users, I have data for more than 3 years. For each year I want to find the day corresponding to Jaunary 1 of that year. For example: x - c('5/5/2007','12/31/2007','1/2/2008') #Convert to day of year (julian date) - strptime(x,%m/%d/%Y)$yday+1 [1] 125 365 2 I want to know how to do the same thing but with time added. But I still get the day not time. Can anyone suggest what is the better way to find the julian date with date and time ? x1 - c('5/5/2007 02:00','12/31/2007 05:58','1/2/2008 16:25') #Convert to day of year (julian date) - strptime(x1,%m/%d/%Y %H:%M)$yday+1 [1] 125 365 2 Thank you so much. Janesh [[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] Is it possible to create a html file by using shiny in Windows ?
Dear R users, I have just started using Shiny in R. I can run the program using R on my local computer but I don't know how can I create the html file on my local server. Can you guys suggest how can I proceed to create a html file on my computer in Windows interface ? Thanks. Janesh [[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] ADCP data processing in R
Hello R Users, I have ADCP (Acoustic Doppler Current Profiler) data measurements for a river and I want to process these data using R. Is there a R package to handle ADCP data ? Any suggestions are highly appreciated. Thanks. Janesh [[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] Any R package to do the harmonic analysis
Hi R Users, I was wondering if there is any R package available to do the harmonic analysis of tide. Any suggestion is highly appreciated. Janesh [[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 the data in year month day hour and minutes to date
Hi , I am trying to convert the date as factor to date using as.date function in R. I have the date in the following format 2008-01-01 02:30 I tried to use the following command : as.Date(mydata$Date, format=%y-%m-%d ) Can somebody help me with this ? I was able to convert the format with no hour but getting difficulty with hour included. Thank you. Janesh [[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] Interpret R-squared and cor in R
Hi I am trying to find the relationship between two variables. First I fitted a linear model between two variables and I found the following results: Residual standard error: 0.03253 on 2498 degrees of freedom Multiple R-squared: 0.5551, Adjusted R-squared: 0.5549 F-statistic: 3116 on 1 and 2498 DF, p-value: 2.2e-16 Then I used the cor function to see the correlation between two variable I get the following result -0.7450344 How can we interpret the result based on R-squared and correlation ? From the p-value we can see that there is very strong relationship between variables as it is way less that 0.001 Can anyone kindly explain the difference between Multiple R squared, adjusted R-squared and correlation and how to report these values while writing a report ? Thank you so much. [[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] Clip a contour with shapefile while using contourplot
Hi, I have done the interpolation for my data and I was able to create the contours in multipanel with the help of Pascal. Now, I want to clip the contour with the shapefile. I want only the portion of contour to be displayed which falls inside the boundary of the shapefile. The data mydata.csv can be found on https://www.dropbox.com/s/khi7nv0160hi68p/mydata.csv The data for shapefile can be found on https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p THe code I have used so far is as follows: # Load Libraries library(latticeExtra) library(sp) library(rgdal) library(lattice) library(gridExtra) #Read Shapefile hello - readOGR(shape, layer=Export_Output_4) ## Project the shapefile to the UTM 16 zone proj4string(hello) - CRS(+proj=utm +zone=16 +ellps=WGS84) ## Read Contour data mydata - read.csv(mydata.csv) head(mydata ) summary(mydata) #Create a contourplot contourplot(Salinity ~ Eastings+Northings | Time, mydata, cuts=30,pretty=TRUE) Thank you so much. I would welcome any other ways to do this aside from contourplot and lattice. Best Regards, Janesh [[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] low pass filter analysis in R
Dear Franklin Bretschneider, Thank you so much for your reply and explanation about the filter using the stats and signal package. I decided to opt the filter method in signal package. I have a simple question about the cut off frequency here. I have 30 minute collected tidal data and I want to use the 48 hour low pass filter to my data to remove the fluctuations and then get only the residuals. What should be the cutoff frequency in my case ? I have tried to figure out cut off frequency with the following rationale : . The parameters for butter filter are n, Wn and type. In the help, W is defined as critical frequencies of the filter. W must be a scalar for low-pass and high-pass filters, and W must be a two-element vector c(low, high) specifying the lower and upper bands. For digital filters, W must be between 0 and 1 where 1 is the Nyquist frequency. A value of 1 corresponds to half the sampling frequency. In my case the sampling frequency is 2 hr^-1. Hence a value of 0.01 corresponds to a frequency cutoff of .01*1 = .01 hr^-1 or 100 hrs time. Using unitary method, if 100 hours cut off frequency is 0.01 then 48 hours cut off frequency is 0.01/100*48 = 0.0048 hr^-1 . Is that correct ? Thank you so much Janesh On Thu, Feb 7, 2013 at 6:07 AM, Bretschneider SIG-R brets...@xs4all.nlwrote: Dear Janesh Devkota, Sorry, I forgot an edit. The last command should read: yfiltered = signal:::filter(myfilter, y) # apply filter Best wishes, Franklin Bretschneider -- Dept Biologie Kruytgebouw W711 Padualaan 8 3584 CH Utrecht The Netherlands [[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] low pass filter analysis in R
Hello R users, I am trying to use R to do the low pass filter analysis for the tidal data. I am a novice in R and so far been doing only simple stuffs on R. I found a package called signal but couldn't find the proper tutorial for the low pass filter. Could anyone point me to the proper tutorial or starting point on how to do low pass filter analysis in R ? Thank you so much. Janesh [[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] Summary of data for each year
Hello All, I have a data with two columns. In one column it is date and in another column it is flow data. I was able to read the data as date and flow data. I used the following code: creek - read.csv(creek.csv) library(ggplot2) creek[1:10,] colnames(creek) - c(date,flow) creek$date - as.Date(creek$date, %m/%d/%Y) The link to my data is https://www.dropbox.com/s/eqpena3nk82x67e/creek.csv Now, I want to find the summary of each year. I want to especially know mean, median, maximum etc. Thanks. Janesh [[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 assign time series to a vector with one leap year
Hello All, I am trying to do the time series analysis in R and I want to assign a vector as a time series. The data I provided is hourly. The data is from Jan 1 2008 to Dec 31 2009. How can I assign the data such that the first year is leap year and second is not ? airtemp - read.csv(airtemp.csv,header=T,sep=) aw - ts(airtemp,start=2008,frequency=8784,end=2009) I assigned frequency as 8784 because 2008 year will have 8784 hourly data points and 2009 has 8760 data points. The total data points are 17544 The data can be found on https://www.dropbox.com/s/03z74632v1f3g1e/airtemp.csv I apologize if this is very trivial to some of you. 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.
Re: [R] How to read a file with two data sets in text format
I was able to read the data using the following code: jd1 - read.table('Practicedata.dat',header=T,sep=\t,nrow=6240) jd2 - read.table('Practicedata.dat',header=T,sep=\t,skip=6241) colnames(jd1) - c(Date,Mod) colnames(jd2) - c(Date, Obs) p - ggplot(jd1,aes(x=Date,y=Mod))+geom_line() p - p + geom_line(data=jd2,aes(x=Date,y=Obs),color=red) p Now, I want to make a scatter plot between jd1$Mod and jd2$Obs. But I cannot create one since both of them have different number of rows. Since I have less number of rows for Mod I am planning to use the date of Mod and then find the corresponding values of Obs for those time periods. How can I find the corresponding values of Obs for the give date in jd1 ? Or is there any way to create a scatter plot and put the regression equation and correlation coefficient. Thank you so much. Best Regards, Janesh On Mon, Jan 21, 2013 at 1:19 AM, Jd Devkota janesh.devk...@gmail.comwrote: Hello All, I have a data file in a text format and there are two data sets. The data set are continuous. For each data set there is a header which has the number of data rows and the name of data series. For example first data set has 6240 Terry Cove-Model. Then the data for that series follows upto 6240 rows. Then another data would start and it will have the header such as 5200 Terry-Observed The sample data would look like: 6240 Terry Cove-Model 300 .300110459327698 300.041656494141 .289277672767639 300.083343505859 .276237487792969 300.125 .258902788162231 300.166656494141 .236579895019531 300.208343505859 .221315026283264 300.25 .214318037033081 300.291656494141 .190926909446716 300.43505859 .158144593238831 300.375 .113302707672119 300.416656494141 .103684902191162 300.458343505859 9.72903966903687E-02 300.5 8.76948833465576E-02 300.541656494141 8.42459201812744E-02 300.583343505859 .078397274017334 300.625 8.44632387161255E-02 300.56494141 9.32939052581787E-02 300.708343505859 .113663911819458 300.75 .123064398765564 300.791656494141 .157548069953918 300.833343505859 .148393034934998 300.875 .135645747184753 300.916656494141 .137590646743774 300.958343505859 .133154153823853 301 .131152510643005 301.041656494141 .114152908325195 301.083343505859 8.04083347320557E-02 301.125 5.53587675094604E-02 301.166656494141 3.17397117614746E-02 301.208343505859 4.07266616821289E-03 301.25 -2.15455293655396E-02 301.291656494141 -4.07489538192749E-02 301.43505859 -5.85414171218872E-02 301.375 -7.53517150878906E-02 301.416656494141 -8.49723815917969E-02 301.458343505859 -7.91778564453125E-02 301.5 -7.02846050262451E-02 301.541656494141 -7.24701881408691E-02 301.583343505859 -7.76907205581665E-02 301.625 -6.82642459869385E-02 62401 Terry Cove-Data 300 .216407993 300.0042 .204216005 300.0083 .210311999 300.0125 .195071996 300.0167 .192023999 300.0208 .179831992 300.025 .188976001 300.0292 .185928004 300.0333 .195071996 300.0375 .219456009 300.0417 .210311999 300.0458 .204216005 300.05 .195071996 300.0542 .188976001 300.0583 .195071996 300.0625 .195071996 300.0667 .185928004 300.0708 .173735998 300.075 .170688001 300.0792 .167640004 300.0833 .167640004 300.0875 .167640004 300.0917 .167640004 300.0958 .161543991 300.1 .1524 300.1042 .158495994 300.1083 .149352003 300.1125 .158495994 300.1167 .1524 300.1208 .1524 300.125 .149352003 300.1292 .143256 300.1333 .146303997 300.1375 .149352003 300.1417 .146303997 300.1458 .137159996 300.15 .131064002 300.1542 .124967999 300.1583 .128015996 300.1625 .124967999 300.1667 .131064002 300.1708 .124967999 300.175 .124967999 300.1792 .134111999 300.1833 .118871996 300.1875 .128015996 300.1917 .131064002 300.1958 .128015996 300.2 .131064002 300.2042 .128015996 300.2083 .121920002 300.2125 .115823999 300.2167 .112776001 300.2208 .103632001 300.225 .097535998 300.2292 .103632001 300.2333 .094488001 300.2375 .082296003 300.2417 .0762 300.2458 .079247997 300.25 .067056 300.2542 .064007998 300.2583 .045720002 300.2625 .033528 300.2667 .036575999 300.2708 .036575999 300.275 .036575999 300.2792 .027432001 300.2833 .027432001 300.2875 .021336 300.2917 .012192 300.2958 .009144 300.3 .009144 300.3042 .003048 300.3083 0 300.3125 -.003048 300.3167 -.006096 300.3208 0 300.325 .006096 300.3292 -.003048 300. .006096 The full data set can be downloaded from https://www.dropbox.com/s/chhw3vz6ru1godk/Practicedata.Dat I want to make a comparison graph between modeled and observed. Once I am able to read two data sets as two sets of data or combined in one I would be able to create the time series graph. Another thing I need to do is create another sub data set where both the series have common data. One data might have more intervals than another. After I find two data sets of same interval then I want to plot a correlation graph. I hope I made it clear what I want to do. Thank you so much. Best Regards, Janesh
[R] Very slow in processing the equation in the scatter plot ggplot
Hello All, I have plotted a scatter plot in ggplot2 and added a regression line and a regression equation. But the processing is very very slow. One reason might be because I have so many data pairs. Is there any way to speed up this code ? I need to create a multiple layout as well. The code I have used is as follows: setwd(C:/Users/jzd0009/Documents/R software) mydata - read.table(dataset.csv,header=TRUE,sep=,) library(ggplot2) p - ggplot(mydata,aes(date))+geom_line(aes(y=modeled,colour=modeled))+geom_line(aes(y=observed,colour=observed)) p p1 - ggplot(mydata, aes(modeled,observed))+geom_point(aes(y=observed)) #p1 - p1+stat_smooth() lm_eqn = function(mydata){ m = lm(modeled ~ observed, mydata); eq - substitute(italic(y) == a + b %.% italic(x)*,~~italic(r)^2~=~r2, list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 - p1 + geom_text(aes(x = -0.1, y = 0.5, label = lm_eqn(mydata)), parse = TRUE) p1 - p1+geom_smooth(method=lm,se=FALSE,color=green,formula=y~x,lwd=2) p1 #For multiple layout library(grid) grid.newpage() pushViewport(viewport(layout=grid.layout(2,2))) vplayout - function(x,y) viewport(layout.pos.row=x,layout.pos.col=y) print(p,vp=vplayout(1,1)) print(p1,vp=vplayout(1,2)) print(p,vp=vplayout(2,1)) print(p1,vp=vplayout(2,2)) The data for the above code can be found on https://www.dropbox.com/s/1xrgvnge0prf0a6/dataset.csv Thank you so much. Best Regards, Janesh Devkota [[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] Very slow in processing the equation in the scatter plot ggplot
Dear Ista, Thank you so much for the prompt reply. Yes, using the annotate instead of the geom_text definitely speed up my graph and look much better. Thanks. Best Regards, Janesh Devkota On Mon, Jan 21, 2013 at 3:46 PM, Ista Zahn istaz...@gmail.com wrote: Hi, One problem is that you are printing the regression equation multiple times. Compare: system.time({ + p2 - p1 + annotate(x = -0.1, y = 0.5, geom=text, label = lm_eqn(mydata), parse = TRUE) + print(p2) + }) user system elapsed 0.930 0.006 0.976 system.time({ + p3 - p1 + geom_text(aes(x = -0.1, y = 0.5, label = lm_eqn(mydata)), parse = TRUE) + print(p3) + }) user system elapsed 38.667 0.046 38.956 So, use annotate instead of geom_text (as a bonus the equation will look better). Further speedups are possible, but this is probably the biggest problem. Best, Ista On Mon, Jan 21, 2013 at 4:21 PM, Janesh Devkota janesh.devk...@gmail.com wrote: Hello All, I have plotted a scatter plot in ggplot2 and added a regression line and a regression equation. But the processing is very very slow. One reason might be because I have so many data pairs. Is there any way to speed up this code ? I need to create a multiple layout as well. The code I have used is as follows: setwd(C:/Users/jzd0009/Documents/R software) mydata - read.table(dataset.csv,header=TRUE,sep=,) library(ggplot2) p - ggplot(mydata,aes(date))+geom_line(aes(y=modeled,colour=modeled))+geom_line(aes(y=observed,colour=observed)) p p1 - ggplot(mydata, aes(modeled,observed))+geom_point(aes(y=observed)) #p1 - p1+stat_smooth() lm_eqn = function(mydata){ m = lm(modeled ~ observed, mydata); eq - substitute(italic(y) == a + b %.% italic(x)*,~~italic(r)^2~=~r2, list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 - p1 + geom_text(aes(x = -0.1, y = 0.5, label = lm_eqn(mydata)), parse = TRUE) p1 - p1+geom_smooth(method=lm,se=FALSE,color=green,formula=y~x,lwd=2) p1 #For multiple layout library(grid) grid.newpage() pushViewport(viewport(layout=grid.layout(2,2))) vplayout - function(x,y) viewport(layout.pos.row=x,layout.pos.col=y) print(p,vp=vplayout(1,1)) print(p1,vp=vplayout(1,2)) print(p,vp=vplayout(2,1)) print(p1,vp=vplayout(2,2)) The data for the above code can be found on https://www.dropbox.com/s/1xrgvnge0prf0a6/dataset.csv Thank you so much. Best Regards, Janesh Devkota [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Faceting in ggplot with 3 series in each plot
I have created a graph with basic R plot with 2 rows and 3 columns. With the basic plot feature of R I had to write several lines of code to come up with the graph https://www.dropbox.com/s/f7d6ei6krtcrtti/hello.png I was wondering whether we can plot multiple series in each plot using ggplot as created in the above link. If anyone could give me any suggestion then I could try on ggplot. I know how to create basic plot with 3 series in ggplot but don't know how to use facet and then use 3 series in each sub plot. As I am a new user in R and ggplot and on a learning curve please excuse me if this question is very naive. Best Regards, Janesh Devkota [[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.