[R] Aggregating the matrices
Hello everyone. Say we have the following: a - matrix(c(-75, 3, 5, 9, 2, 3, 5), nrow=1, dim=list(06092010, c(ES, PT, Z , CF, GX, ST, EO))) b - matrix(c(-5, 2, 4, 12, 5), nrow=1, dim=list(06092010, c(PT, CF, AT, EM, ST))) d - cbind(a, b) I want to calculate sums of the columns that have similar column names and then output this summary What I want to have is an array that looks like: ES PT Z CF... -75 -2 5 11... I tried the following, but it did not work: aggregate(d, list(colnames(d)), sum) How can I achieve my objective? Thank you in advance. Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Aggregating the matrices
Gabor, David, thank you. David, your last suggestion is what I need. Regards, Sergey On Mon, Sep 6, 2010 at 16:12, David Winsemius dwinsem...@comcast.net wrote: On Sep 6, 2010, at 9:56 AM, Sergey Goriatchev wrote: Hello everyone. Say we have the following: a - matrix(c(-75, 3, 5, 9, 2, 3, 5), nrow=1, dim=list(06092010, c(ES, PT, Z , CF, GX, ST, EO))) b - matrix(c(-5, 2, 4, 12, 5), nrow=1, dim=list(06092010, c(PT, CF, AT, EM, ST))) d - cbind(a, b) I want to calculate sums of the columns that have similar column names and then output this summary What I want to have is an array that looks like: ES PT Z CF... -75 -2 5 11... I tried the following, but it did not work: aggregate(d, list(colnames(d)), sum) ES is not in the duplicated column names so perhaps your English specification is not what you meant: d ES PT Z CF GX ST EO PT CF AT EM ST 06092010 -75 3 5 9 2 3 5 -5 2 4 12 5 dupled - colnames(d)[duplicated(colnames(d))] sapply(dupled, function(x) sum( d[, x])) PT CF ST 3 9 3 If you wanted simple a sum over unique column names then it would have been somewhat simpler (no need to construct a duplicated set): sapply(unique(colnames(d)), function(x) sum( d[, x])) ES PT Z CF GX ST EO AT EM -75 3 5 9 2 3 5 4 12 How can I achieve my objective? Thank you in advance. Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting future and past workday dates
Henrique, that is what I need! Big thanks! Regards, Sergey On Thu, Aug 5, 2010 at 14:56, Henrique Dallazuanna www...@gmail.com wrote: n - 3 w - as.numeric(format(Sys.Date(), '%w')) fut - c(Sys.Date() - 0:(n + ifelse(w - n 6, (w - n) - 6, 0)), Sys.Date() + 1:(n + 1 + ifelse(w + n 6, (w + n) - 6, 0))) sort(fut[!format(fut, '%w') %in% c(6, 0)]) -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting future and past workday dates
Hello everyone I need to extract a vector of (t-3) to (t+3) dates, only working days. How can I do that? For today I need a vector: 10.08.210 09.08.2010 06.08.2010 05.08.2010 04.08.2010 03.08.2010 02.08.2010 Regards, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting future and past workday dates
Hi, Henrique Thank you for trying, but that is not what I want. You get WEEKdays, I need WORKdays, and preferably sorted in order from future to the past. Best, Sergey On Thu, Aug 5, 2010 at 14:06, Henrique Dallazuanna www...@gmail.com wrote: Try this: c(Sys.Date() + 0:3, Sys.Date() - 0:3) or 0:3 %*% matrix(c(1, -1), ncol = 2) + Sys.Date() On Thu, Aug 5, 2010 at 8:36 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello everyone I need to extract a vector of (t-3) to (t+3) dates, only working days. How can I do that? For today I need a vector: 10.08.210 09.08.2010 06.08.2010 05.08.2010 04.08.2010 03.08.2010 02.08.2010 Regards, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting future and past workday dates
Thank you David, will check it out, as I use chron and zoo! Regards, Sergey On Thu, Aug 5, 2010 at 14:29, David Winsemius dwinsem...@comcast.net wrote: On Aug 5, 2010, at 8:09 AM, Sergey Goriatchev wrote: Hi, Henrique Thank you for trying, but that is not what I want. You get WEEKdays, I need WORKdays, and preferably sorted in order from future to the past. Perhaps you would be pleased to know that there is an is.holiday function in chron: http://finzi.psych.upenn.edu/R/library/chron/html/is.holiday.html -- David. Best, Sergey On Thu, Aug 5, 2010 at 14:06, Henrique Dallazuanna www...@gmail.com wrote: Try this: c(Sys.Date() + 0:3, Sys.Date() - 0:3) or 0:3 %*% matrix(c(1, -1), ncol = 2) + Sys.Date() On Thu, Aug 5, 2010 at 8:36 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello everyone I need to extract a vector of (t-3) to (t+3) dates, only working days. How can I do that? For today I need a vector: 10.08.210 09.08.2010 06.08.2010 05.08.2010 04.08.2010 03.08.2010 02.08.2010 Regards, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] F# vs. R
Hello, everyone F# is now public. Compiled code should run faster than R. Anyone has opinion on F# vs. R? Just curious Best, S -- --- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] F# vs. R
Hello, Marc No, I do not want to validate Cox PH. :-) I do use R daily, though right now I do not use the statistical part that much. I just generally wonder if any R-user tried F# and his/her opinions. Regards, Sergey On Wed, Jul 7, 2010 at 17:56, Marc Schwartz marc_schwa...@me.com wrote: On Jul 7, 2010, at 10:31 AM, Sergey Goriatchev wrote: Hello, everyone F# is now public. Compiled code should run faster than R. Anyone has opinion on F# vs. R? Just curious Best, S The key time critical parts of R are written in compiled C and FORTRAN. Of course, if you want to take the time to code and validate a Cox PH or mixed effects model in F# and then run them against R's coxph() or lme()/lmer() functions to test the timing, feel free... :-) So unless there is a pre-existing library of statistical and related functionality for F#, perhaps you need to reconsider your query. Regards, Marc Schwartz -- --- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Parallel computing on Windows (foreach)
Thank you, Mario. On Wed, Jun 16, 2010 at 14:51, Mario Valle mva...@cscs.ch wrote: On 15-Jun-10 17:07, Sergey Goriatchev wrote: Hello, I am reading Using The foreach Package document and I have tried the following: - sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C LC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] foreach_1.3.0 codetools_0.2-2 iterators_1.0.3 x- numeric(1) system.time(for(i in 1:1) x[i]- sqrt(i)) user system elapsed 0.03 0.00 0.03 system.time(system.time(x- foreach(i=1:1, .combine=c) %do% sqrt(i))) user system elapsed 7.14 0.00 7.14 system.time(system.time(x- foreach(i=1:1, .combine=c) %dopar% sqrt(i))) user system elapsed 7.19 0.00 7.19 Warning message: executing %dopar% sequentially: no parallel backend registered Not only is the sequential foreach much slower than the simple for-loop (as least in this particular instance), but I am not quite sure how to make foreach run parallel. Where would I get this parallel backend? Use doMPI and run R through mpirun (for example run on 8 cores): mpirun -np 8 R --slave -f your-script.r Hope it helps mario I looked at doMC and doRedis, but these do not run on Windows, as far as I understand. And doSNOW is something to use when you have a cluster, while I have a simple dual-core PC. It is not really clear for how to make parallel computing work. Please, help. Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Ing. Mario Valle Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60 v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82 -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party Genuine Replica Toxic Assets Italian Government Feminine Logic Amicable Divorce Military Intelligence Money Multiplier Fiscal Conservative Abundant Poverty Educated Investor Government Worker Green Shoots Hope and Change Change you can believe in Becky Quick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Parallel computing on Windows (foreach) (Sergey Goriatchev)
John, Steve, thank you for answering my post! On Wed, Jun 16, 2010 at 20:55, Steve Lianoglou mailinglist.honey...@gmail.com wrote: Hi, I think I lost the reference email somewhere, but: Not only is the sequential foreach much slower than the simple for-loop (as least in this particular instance), but I am not quite sure how to make foreach run parallel. Where would I get this parallel backend? I looked at doMC and doRedis, but these do not run on Windows, as far as I understand. See this blog post: http://www.r-statistics.com/tag/dosmp/ -- 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 -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party Genuine Replica Toxic Assets Italian Government Feminine Logic Amicable Divorce Military Intelligence Money Multiplier Fiscal Conservative Abundant Poverty Educated Investor Government Worker Green Shoots Hope and Change Change you can believe in Becky Quick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 see how a function is written
Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Thank you for help! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 see how a function is written
Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day14 svn rev50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party Genuine Replica Toxic Assets Italian Government Feminine Logic Amicable Divorce Military Intelligence Money Multiplier Fiscal Conservative Abundant Poverty Educated Investor Government Worker Green Shoots Hope and Change Change you can believe in Becky Quick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 see how a function is written
Maybe I have to much stuff loaded in the workspace, Gavin, you are right: sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 [4] LC_NUMERIC=CLC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13TTR_0.20-1 Defaults_1.1-1 xts_0.7-0 [6] fPortfolio_2100.78 Rglpk_0.3-5slam_0.1-9 fAssets_2100.78fCopulae_2110.78 [11] sn_0.4-14 mnormt_1.3-3 fBasics_2110.79timeSeries_2110.87 timeDate_2110.87 [16] robustbase_0.5-0-1 quadprog_1.4-12MASS_7.3-5 fEcofin_290.76 foreach_1.3.0 [21] codetools_0.2-2iterators_1.0.3zoo_1.6-3 loaded via a namespace (and not attached): [1] grid_2.10.1lattice_0.18-3 tools_2.10.1 On Tue, Jun 15, 2010 at 14:56, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day 14 svn rev 50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party
Re: [R] How to see how a function is written
showMethods(apply) Function: apply (package base) X=ANY X=missing (inherited from: X=ANY) X=timeSeries On Tue, Jun 15, 2010 at 15:10, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:56 +0200, Sergey Goriatchev wrote: Maybe I have to much stuff loaded in the workspace, Gavin, you are right: OK, so now do showMethods(apply) And R should list out the available methods. See which package (re)defines apply. But it is likely going to be simpler to start a clean session and look at the code in there. If you need the S4 method/generic code then you'll have to find out which package is redefining apply and look in the sources for that package. HTH G sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 [4] LC_NUMERIC=C LC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13 TTR_0.20-1 Defaults_1.1-1 xts_0.7-0 [6] fPortfolio_2100.78 Rglpk_0.3-5 slam_0.1-9 fAssets_2100.78 fCopulae_2110.78 [11] sn_0.4-14 mnormt_1.3-3 fBasics_2110.79 timeSeries_2110.87 timeDate_2110.87 [16] robustbase_0.5-0-1 quadprog_1.4-12 MASS_7.3-5 fEcofin_290.76 foreach_1.3.0 [21] codetools_0.2-2 iterators_1.0.3 zoo_1.6-3 loaded via a namespace (and not attached): [1] grid_2.10.1 lattice_0.18-3 tools_2.10.1 On Tue, Jun 15, 2010 at 14:56, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day 14 svn rev 50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just
[R] Parallel computing on Windows (foreach)
Hello, I am reading Using The foreach Package document and I have tried the following: - sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C LC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] foreach_1.3.0 codetools_0.2-2 iterators_1.0.3 x - numeric(1) system.time(for(i in 1:1) x[i] - sqrt(i)) user system elapsed 0.030.000.03 system.time(system.time(x - foreach(i=1:1, .combine=c) %do% sqrt(i))) user system elapsed 7.140.007.14 system.time(system.time(x - foreach(i=1:1, .combine=c) %dopar% sqrt(i))) user system elapsed 7.190.007.19 Warning message: executing %dopar% sequentially: no parallel backend registered Not only is the sequential foreach much slower than the simple for-loop (as least in this particular instance), but I am not quite sure how to make foreach run parallel. Where would I get this parallel backend? I looked at doMC and doRedis, but these do not run on Windows, as far as I understand. And doSNOW is something to use when you have a cluster, while I have a simple dual-core PC. It is not really clear for how to make parallel computing work. Please, help. Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Deleting a file on a drive from within R
Hello, I have an Excel file on a drive and I extract data from it into R session. Once I have extracted the data, I want to delete that Excel file from the drive. Can I do that from within R, please? Thank you for help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Deleting a file on a drive from within R
Thank you, Dave! Regards, Sergey On Thu, May 20, 2010 at 10:55, Dave Deriso dder...@ucsd.edu wrote: file_to_delete = file.choose() file.remove(file_to_delete) Best, Dave Deriso UCSD Psychology On Thu, May 20, 2010 at 1:46 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, I have an Excel file on a drive and I extract data from it into R session. Once I have extracted the data, I want to delete that Excel file from the drive. Can I do that from within R, please? Thank you for help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Deleting a file on a drive from within R
Thanks, Tal Will check it out. Regards, Sergey On Thu, May 20, 2010 at 10:51, Tal Galili tal.gal...@gmail.com wrote: Check out: ?unlink Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- On Thu, May 20, 2010 at 11:46 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, I have an Excel file on a drive and I extract data from it into R session. Once I have extracted the data, I want to delete that Excel file from the drive. Can I do that from within R, please? Thank you for help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting specific rows from irregular zoo object and merging with a regular zoo object
Hello, everyone I have the following problem: Say I have an irregular zoo timeseries like this: a - zoo(rep(1:9), as.Date(c(2009-03-20, 2009-03-27, 2009-04-24, 2009-04-25, 2009-04-30, 2009-05-15, 2009-05-22, 2009-05-29, 2009-06-26))) and I have regular zoo timeseries like this: b - zoo(rep(1:4), as.Date(c(2009-03-31, 2009-04-30, 2009-05-31, 2009-06-30))) From a I need to extract those rows that hold values for the last day of each month (creating series c). Then I have to merge these values with b, such that the result has the index of c. How could I do this most efficiently? Thank you in advance! Best, Sergey -- Simplicity is the last step of art./Bruce Lee __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Extracting specific rows from irregular zoo object and merging with a regular zoo object
Thank you, Gabor! This is a very elegant solution. But instead of general last day of month in the index, how can I have last day of each month as they are presented in a, for example, not March 31, but March 27? Regards, Sergey On Thu, Apr 8, 2010 at 17:27, Gabor Grothendieck ggrothendi...@gmail.com wrote: Omit rep. You just want a - zoo(1:9, ...). To get the last day of the month you don`t need b since as.Date.yearmon will give it with the argument frac = 1: aggregate(a, as.Date(as.yearmon(time(a)), frac = 1), tail, 1) 2009-03-31 2009-04-30 2009-05-31 2009-06-30 2 5 8 9 On Thu, Apr 8, 2010 at 11:18 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have the following problem: Say I have an irregular zoo timeseries like this: a - zoo(rep(1:9), as.Date(c(2009-03-20, 2009-03-27, 2009-04-24, 2009-04-25, 2009-04-30, 2009-05-15, 2009-05-22, 2009-05-29, 2009-06-26))) and I have regular zoo timeseries like this: b - zoo(rep(1:4), as.Date(c(2009-03-31, 2009-04-30, 2009-05-31, 2009-06-30))) From a I need to extract those rows that hold values for the last day of each month (creating series c). Then I have to merge these values with b, such that the result has the index of c. How could I do this most efficiently? Thank you in advance! Best, Sergey -- Simplicity is the last step of art./Bruce Lee __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Simplifying particular piece of code
Hello, everyone I have a piece of code that looks like this: mrets - merge(mrets, BMM.SR=apply(mrets, 1, MyFunc, ret=BMM.AV120, stdev=BMM.SD120)) mrets - merge(mrets, GM1.SR=apply(mrets, 1, MyFunc, ret=GM1.AV120, stdev=GM1.SD120)) mrets - merge(mrets, IYC.SR=apply(mrets, 1, MyFunc, ret=IYC.AV120, stdev=IYC.SD120)) mrets - merge(mrets, FCA.SR=apply(mrets, 1, MyFunc, ret=FCA.AV120, stdev=FCA.SD120)) mrets - merge(mrets, IMM.SR=apply(mrets, 1, MyFunc, ret=IMM.AV120, stdev=IMM.SD120)) mrets - merge(mrets, BME.SR=apply(mrets, 1, MyFunc, ret=BME.AV120, stdev=BME.SD120)) mrets - merge(mrets, CRT.SR=apply(mrets, 1, MyFunc, ret=CRT.AV120, stdev=CRT.SD120)) mrets - merge(mrets, GTF.SR=apply(mrets, 1, MyFunc, ret=GTF.AV120, stdev=GTF.SD120)) mrets - merge(mrets, ERU.SR=apply(mrets, 1, MyFunc, ret=ERU.AV120, stdev=ERU.SD120)) mrets - merge(mrets, ERE.SR=apply(mrets, 1, MyFunc, ret=ERE.AV120, stdev=ERE.SD120)) mrets - merge(mrets, EPT.SR=apply(mrets, 1, MyFunc, ret=EPT.AV120, stdev=EPT.SD120)) mrets - merge(mrets, EVA.SR=apply(mrets, 1, MyFunc, ret=EVA.AV120, stdev=EVA.SD120)) mrets - merge(mrets, EMT.SR=apply(mrets, 1, MyFunc, ret=EMT.AV120, stdev=EMT.SD120)) mrets - merge(mrets, EMM.SR=apply(mrets, 1, MyFunc, ret=EMM.AV120, stdev=EMM.SD120)) mrets - merge(mrets, EMV.SR=apply(mrets, 1, MyFunc, ret=EMV.AV120, stdev=EMV.SD120)) mrets - merge(mrets, ETM.SR=apply(mrets, 1, MyFunc, ret=ETM.AV120, stdev=ETM.SD120)) Is there a way to simplify this, some sort of loop? mrets is a zoo object. .AV120 and .SD120 are columns in this object. I need the exact .SR column names. This does not work: SRnames - paste(colnames.mrets, .SR, sep=) AVnames - paste(colnames.mrets, .AV120, sep=) SDnames - paste(colnames.mrets, .SD120, sep=) for(i in seq(SRnames)){ mrets - merge(mrets, SRnames[i]=apply(mrets, 1, MyFunc, ret=AVnames[i], stdev=SDnames[i])) } Help much appreciated. Regards, Sergey -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Simplifying particular piece of code
Thanks for help, Gustaf! (Kan man säga man tackar oxa?) :-) On Wed, Mar 31, 2010 at 17:34, Gustaf Rydevik gustaf.ryde...@gmail.com wrote: On Wed, Mar 31, 2010 at 5:11 PM, Sergey Goriatchev serg...@gmail.com wrote: but data - merge(data,data.list) works. Neither data or data.list is a list, so do.call does not work. I am very weak on lists, never used them before Best, Sergey Hej Sergey, Ok; I was wondering if the apply thing would work. Cool that merge would be clever enough to append a matrix. I'm guessing that you've got what you needed then? For reference, (and for the general list) I had changed the code before Sergeys response, replacing apply() with lapply(). That code follows below. Best regards, Gustaf - cnames - c(BMM, GM1, IYC, FCA, IMM, BME, CRT, GTF, ERU, ERE, EPT, EVA, EMT, EMM, EMV, ETM) AVnames - paste(cnames, .AV120, sep=) SDnames - paste(cnames, .SD120, sep=) a - zoo(matrix(rep(seq(from=160, to=10, by=-10), 1000), ncol=16, byrow=TRUE)) colnames(a) - AVnames b - zoo(matrix(rep(2, 16000), ncol=16)) colnames(b) - SDnames data - merge(a, b) MyFunc - function(x, ret, stdev){ if(any(is.na(c(x[ret], x[stdev]{ return(NA) }else{ return(x[ret]/x[stdev]) } } names.df-data.frame(rbind(SRnames,AVnames,SDnames)) func - function(.names){ apply(data, 1, MyFunc, ret=.names[2], stdev=.names[3]) } data.list-lapply(names.df, func) mrets-do.call(merge,c(list(data),data.list)) On Wed, Mar 31, 2010 at 12:33, Gustaf Rydevik gustaf.ryde...@gmail.com wrote: How about this (not tested, since you did not provide example data nor function code): --- SRnames - paste(colnames.mrets, .SR, sep=) AVnames - paste(colnames.mrets, .AV120, sep=) SDnames - paste(colnames.mrets, .SD120, sep=) names.matrix-cbind(SRnames,AVnames,SDnames) mrets.list-apply(names.matrix,1,function(.names){ apply(mrets,1,MyFunc,ret=.names[2],stdev=.names[3]} ) names(mrets.list)-names.matrix[,1] mrets-do.call(merge,mrets.list) - ? /Gustaf On Wed, Mar 31, 2010 at 12:10 PM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a piece of code that looks like this: mrets - merge(mrets, BMM.SR=apply(mrets, 1, MyFunc, ret=BMM.AV120, stdev=BMM.SD120)) mrets - merge(mrets, GM1.SR=apply(mrets, 1, MyFunc, ret=GM1.AV120, stdev=GM1.SD120)) mrets - merge(mrets, IYC.SR=apply(mrets, 1, MyFunc, ret=IYC.AV120, stdev=IYC.SD120)) mrets - merge(mrets, FCA.SR=apply(mrets, 1, MyFunc, ret=FCA.AV120, stdev=FCA.SD120)) mrets - merge(mrets, IMM.SR=apply(mrets, 1, MyFunc, ret=IMM.AV120, stdev=IMM.SD120)) mrets - merge(mrets, BME.SR=apply(mrets, 1, MyFunc, ret=BME.AV120, stdev=BME.SD120)) mrets - merge(mrets, CRT.SR=apply(mrets, 1, MyFunc, ret=CRT.AV120, stdev=CRT.SD120)) mrets - merge(mrets, GTF.SR=apply(mrets, 1, MyFunc, ret=GTF.AV120, stdev=GTF.SD120)) mrets - merge(mrets, ERU.SR=apply(mrets, 1, MyFunc, ret=ERU.AV120, stdev=ERU.SD120)) mrets - merge(mrets, ERE.SR=apply(mrets, 1, MyFunc, ret=ERE.AV120, stdev=ERE.SD120)) mrets - merge(mrets, EPT.SR=apply(mrets, 1, MyFunc, ret=EPT.AV120, stdev=EPT.SD120)) mrets - merge(mrets, EVA.SR=apply(mrets, 1, MyFunc, ret=EVA.AV120, stdev=EVA.SD120)) mrets - merge(mrets, EMT.SR=apply(mrets, 1, MyFunc, ret=EMT.AV120, stdev=EMT.SD120)) mrets - merge(mrets, EMM.SR=apply(mrets, 1, MyFunc, ret=EMM.AV120, stdev=EMM.SD120)) mrets - merge(mrets, EMV.SR=apply(mrets, 1, MyFunc, ret=EMV.AV120, stdev=EMV.SD120)) mrets - merge(mrets, ETM.SR=apply(mrets, 1, MyFunc, ret=ETM.AV120, stdev=ETM.SD120)) Is there a way to simplify this, some sort of loop? mrets is a zoo object. .AV120 and .SD120 are columns in this object. I need the exact .SR column names. This does not work: SRnames - paste(colnames.mrets, .SR, sep=) AVnames - paste(colnames.mrets, .AV120, sep=) SDnames - paste(colnames.mrets, .SD120, sep=) for(i in seq(SRnames)){ mrets - merge(mrets, SRnames[i]=apply(mrets, 1, MyFunc, ret=AVnames[i], stdev=SDnames[i])) } Help much appreciated. Regards, Sergey -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE
[R] Help function ? in R 2.10.1
Hello everyone, I have versions 2.7.2 and 2.10.1 installed on a machine that has no access to internet. In 2.7.2 I can use ? to get help on functions, which in 2.10.1 that does not work, all I see is starting httpd help server...done and then nothing. Have I downloaded 2.10.1 incorrectly (=forgot to tick some box for local help file repository) or is the internet help now the standard way to search help on functions? Thank you in advance for help. Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Finding a date 6 months before
Hello everyone I use zoo objects and I need to find a date 6 months before today's date. Example, today we have 3rd March 2010, I need to find the date 3rd September 2009. How could I do that, please? Regards, Sergey -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Finding a date 6 months before
Dear Gabor, Fantastic! Thank you very much for your help, as always! Regards, Sergey On Wed, Mar 3, 2010 at 12:24, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this. as.Date(as.yearmon(today) - 6/12) gives the 1st of the month 6 months ago and the remainder of that line moves ahead by d-1 days if its the d-th day of the month now. library(zoo) z - zoo(0:399, as.Date(2009-06-01)+0:399) today - Sys.Date(); today [1] 2010-03-03 ago6mos - as.Date(as.yearmon(today) - 6/12) - 1 + as.numeric(format(today, %d)) window(z, index = ago6mos) 2009-09-03 94 If we are at the last day of the month and 6 months ago that month has fewer days than today's month then what to do is not well defined but we resolve that here by just moving ahead d-1 days anyways. On Wed, Mar 3, 2010 at 6:07 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello everyone I use zoo objects and I need to find a date 6 months before today's date. Example, today we have 3rd March 2010, I need to find the date 3rd September 2009. How could I do that, please? Regards, Sergey -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Physically open Excel file from R
Hello, everyone I wonder if it is possible to PHYSICALLY open an Excel file from R. The reason I ask is, I produce regularly an Excel file in R, and then I want to make it look good, so I have a VBA routine in another Excel file that works on the regular Excel file. This formatting file executes VBA code on open, so all I need to do is physically open it (no reading/writing at all). I wonder if that can be done from R, so that I do as little as possible manually. Thanks in advance for help! Regards, Sergey -- Simplicity is the last step of art./Bruce Lee Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Physically open Excel file from R
Dear Uwe, Worked as a charm! :-) Thank you kindly. Now, after I've run the VBA with shell.exec(), and closed the formater and the formatted file, I still have Excel shell (=Excel with no files open) open. Is it possible to close it from R as well? Best, Sergey 2010/2/8 Uwe Ligges lig...@statistik.tu-dortmund.de: On 08.02.2010 13:48, Sergey Goriatchev wrote: Hello, everyone I wonder if it is possible to PHYSICALLY open an Excel file from R. The reason I ask is, I produce regularly an Excel file in R, and then I want to make it look good, so I have a VBA routine in another Excel file that works on the regular Excel file. This formatting file executes VBA code on open, so all I need to do is physically open it (no reading/writing at all). I wonder if that can be done from R, so that I do as little as possible manually. If you are under Windows and Excel is installed properly, shell.exec(filename) should do the trick. Uwe Ligges Thanks in advance for help! Regards, Sergey -- Simplicity is the last step of art./Bruce Lee Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Simplicity is the last step of art./Bruce Lee The more you know, the more you know you don't know. /Myself I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 check if file is on a drive?
Hello, everyone How would I check in R if a particular file (Excel file) is in a particular folder on a particular drive? I am writing a piece of code around that file, using xls.open() in xlsReadWritePro to open the file in memory and use it. But before I try to open the file I want to catch the possibility of the file not being on the drive. Thank you in advance! SG __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 check if file is on a drive?
Great! Thank you, Romain! On Thu, Dec 3, 2009 at 11:33, Romain Francois romain.franc...@dbmail.com wrote: ?file.exists On 12/03/2009 11:31 AM, Sergey Goriatchev wrote: Hello, everyone How would I check in R if a particular file (Excel file) is in a particular folder on a particular drive? I am writing a piece of code around that file, using xls.open() in xlsReadWritePro to open the file in memory and use it. But before I try to open the file I want to catch the possibility of the file not being on the drive. Thank you in advance! SG -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/Gq7i : ohloh |- http://tr.im/FtUu : new package : highlight `- http://tr.im/EAD5 : LondonR slides -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Taking specific/timed differences in a zoo timeseries
Hello everyone. I have a specific problem that I have difficulties to solve. Assume I have a zoo object: set.seed(12345) data - round(runif(27)*10+runif(27)*5, 0) dates - as.Date(c(09/03/09, 09/04/09, 09/07/09, 09/09/09, 09/10/09, 09/11/09, 09/14/09, 09/16/09, 09/17/09, 09/18/09, 09/21/09, 09/22/09, 09/23/09, 09/24/09, 09/25/09, 09/28/09, 09/29/09, 09/30/09, 10/01/09, 10/02/09, 10/05/09, 10/06/09, 10/07/09, 10/08/09, 10/09/09, 10/13/09, 10/14/09), %m/%d/%y) temp - zoo(data, order.by=dates) What I need to do is to take differences between say October 14th and September 14, then October 13th and September 13th, that is 1 month difference independent of number of days inbetween. And when there is no matching date in an earlier month, like here where there is no September 13th, the date should be the first preceding date, that is September 11th in this example. How can I do that? The above is just an example, my zoo object is very big and I need to take differences between years, that is between October 14th, 2009 and October 14th, 2008, then Oct.13, 2009 and Oct.13, 2008, and so on. Also, the time index of my zoo object has format 10/14/09 (that is Oct.14, 2009), and that is the format I need to operate with and do not want to change. In the example I reformated just so that I can create a zoo object. Could some friendly person please show me how to do such a calculation? Thank you in advance! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Taking specific/timed differences in a zoo timeseries
Dear Gabor, Thank you very much for your help! I'm now using your suggestion with my data. May I ask a stupid question? The output's index now has format 2009-10-14. How can I transform it back into original 10/14/09 and use this in a zoo object? Regards, Sergey On Wed, Oct 14, 2009 at 17:03, Gabor Grothendieck ggrothendi...@gmail.comwrote: Try this: library(zoo) # temp - ... from post asking question # create a day sequence, dt, with no missing days # and create a 0 width series with those times. # merge that with original series giving original # series plus a bunch of times having NA values. # Use na.locf to fill in those values with the last # non-missing so far. rng - range(time(temp)) dt - seq(rng[1], rng[2], day) temp.m - na.locf(merge(temp, zoo(, dt))) # create a lagged time scale and subtract the # lagged series from original dt.lag - as.Date(as.yearmon(dt)+1/12) + as.numeric(format(dt, %d)) - 1 temp - zoo(coredata(temp.m), dt.lag) Using your data the output from the last line is: temp - zoo(coredata(temp.m), dt.lag) 2009-10-05 2009-10-06 2009-10-07 2009-10-08 2009-10-09 2009-10-13 2009-10-14 -5 -6 3 2 -2 2 1 On Wed, Oct 14, 2009 at 10:39 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello everyone. I have a specific problem that I have difficulties to solve. Assume I have a zoo object: set.seed(12345) data - round(runif(27)*10+runif(27)*5, 0) dates - as.Date(c(09/03/09, 09/04/09, 09/07/09, 09/09/09, 09/10/09, 09/11/09, 09/14/09, 09/16/09, 09/17/09, 09/18/09, 09/21/09, 09/22/09, 09/23/09, 09/24/09, 09/25/09, 09/28/09, 09/29/09, 09/30/09, 10/01/09, 10/02/09, 10/05/09, 10/06/09, 10/07/09, 10/08/09, 10/09/09, 10/13/09, 10/14/09), %m/%d/%y) temp - zoo(data, order.by=dates) What I need to do is to take differences between say October 14th and September 14, then October 13th and September 13th, that is 1 month difference independent of number of days inbetween. And when there is no matching date in an earlier month, like here where there is no September 13th, the date should be the first preceding date, that is September 11th in this example. How can I do that? The above is just an example, my zoo object is very big and I need to take differences between years, that is between October 14th, 2009 and October 14th, 2008, then Oct.13, 2009 and Oct.13, 2008, and so on. Also, the time index of my zoo object has format 10/14/09 (that is Oct.14, 2009), and that is the format I need to operate with and do not want to change. In the example I reformated just so that I can create a zoo object. Could some friendly person please show me how to do such a calculation? Thank you in advance! Best, Sergey __ R-help@r-project.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. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. [[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] Putting a text box in a plot
Hello, Jim Thank you a lot for suggestions, I will check that package out. It could be the one I used way back then! :-) Best, Sergey On Tue, Sep 22, 2009 at 01:55, Jim Lemon j...@bitwrit.com.au wrote: On 09/21/2009 07:42 PM, Sergey Goriatchev wrote: Hello everyone, I have a plot and I want to but a (formatted) box containing text and numbers, say: Mean: 0.1 St.Deviation: 1.1 Skewness: 1.1 Kurtosis: 0.5 I know there is a way to do this, there is a function in some library, but it's been years since I used this function, and I do not remember where I found it. Could anyone help out? Thank you in advance! Sergey Hi Sergey, Maybe addtable2plot in the plotrix package will do what you want. Jim -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Function similar to cumsum/cumprod
Hello, everyone I wonder if there is in R somewhere a function similar to cumsum(). The function calculates a statistic (say mean or standard deviation) buy adding consequtively one more data point. So, say I have a timeseries of 100 observations. I start by calculating mean of first 30 observations Then I add one observation and calculate mean of 31 observations Then I add one more observation and calculate mean of 32 observation, and so on until the end Is there a function like that in R? Best, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Function similar to cumsum/cumprod
Henrique, thank you! On Tue, Sep 22, 2009 at 16:32, Henrique Dallazuanna www...@gmail.com wrote: Try this; set.seed(123) x - rnorm(100) sapply(30:length(x), function(i)mean(x[1:i])) On Tue, Sep 22, 2009 at 11:12 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I wonder if there is in R somewhere a function similar to cumsum(). The function calculates a statistic (say mean or standard deviation) buy adding consequtively one more data point. So, say I have a timeseries of 100 observations. I start by calculating mean of first 30 observations Then I add one observation and calculate mean of 31 observations Then I add one more observation and calculate mean of 32 observation, and so on until the end Is there a function like that in R? Best, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Putting a text box in a plot
Hello everyone, I have a plot and I want to but a (formatted) box containing text and numbers, say: Mean: 0.1 St.Deviation: 1.1 Skewness: 1.1 Kurtosis: 0.5 I know there is a way to do this, there is a function in some library, but it's been years since I used this function, and I do not remember where I found it. Could anyone help out? Thank you in advance! Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Putting a text box in a plot
Hi, Baptiste Yes, I've found textplot() function, but I doubt it is the one I need. What I have is a density plot, and I want to add to this density plot a text box containing the stats I mentioned in previous email. I could do that kind of stuff with simple text() function, but then everytime x values change I will have to change x-axis output coordinates in text() function. No, there must be another function somewhere that produces these text boxes. Best, Sergey On Mon, Sep 21, 2009 at 11:49, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, Maybe the textplot() function in the gplots package? HTH, baptiste 2009/9/21 Sergey Goriatchev serg...@gmail.com: Hello everyone, I have a plot and I want to but a (formatted) box containing text and numbers, say: Mean: 0.1 St.Deviation: 1.1 Skewness: 1.1 Kurtosis: 0.5 I know there is a way to do this, there is a function in some library, but it's been years since I used this function, and I do not remember where I found it. Could anyone help out? Thank you in advance! Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Putting a text box in a plot
Here is a kind of example: plot(density(rnorm(1000))) text(c(2,2),c(0.2,0.21),labels=c(Skewness,Kurtosis), pos=4, cex=0.7) text(c(3,3),c(0.2, 0.21), labels=c(1.1, 2.2), pos=4, cex=0.7) There is a function in some library that produces text boxes (with borders) that can be placed inside a plot. That is what I need - a bordered box containing text inside a plot. So legend() and mtext() do not apply here, and text() is just too raw. There is a special function for that, as I say. I once asked on an R forum, and somebody directed me to this function. Unfortunately, I am not successful googling that question of mine. It's been a few years. Best, Sergey On Mon, Sep 21, 2009 at 12:13, baptiste auguie baptiste.aug...@googlemail.com wrote: Where do you want this text to be placed then? maybe a call to legend(), or mtext() would suffice, it's hard to say more without a reproducible example. baptiste 2009/9/21 Sergey Goriatchev serg...@gmail.com: Hi, Baptiste Yes, I've found textplot() function, but I doubt it is the one I need. What I have is a density plot, and I want to add to this density plot a text box containing the stats I mentioned in previous email. I could do that kind of stuff with simple text() function, but then everytime x values change I will have to change x-axis output coordinates in text() function. No, there must be another function somewhere that produces these text boxes. Best, Sergey On Mon, Sep 21, 2009 at 11:49, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, Maybe the textplot() function in the gplots package? HTH, baptiste 2009/9/21 Sergey Goriatchev serg...@gmail.com: Hello everyone, I have a plot and I want to but a (formatted) box containing text and numbers, say: Mean: 0.1 St.Deviation: 1.1 Skewness: 1.1 Kurtosis: 0.5 I know there is a way to do this, there is a function in some library, but it's been years since I used this function, and I do not remember where I found it. Could anyone help out? Thank you in advance! Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Executing R scripts from another R script
Hello, everyone I run Eclipse Ganymede and R 2.7.2 at work. I have one R script file where I open in memory a new xls file (using xlsReadWritePro), call other R scripts, which are in the same folder as the main R script, which get data from an existing xls file, process data, and output results in the xls file which is in memory. That is the idea. But I cannot make it work. First, I do not really know how to call other R scripts from an R script. I am using function source like this: source(file=G:/data/datafile.xls) I get a following error (in German, as I work with German Windows): Converting xls file to csv file... Fehler in system(cmd, intern = !verbose) : perl nicht gefunden Fehler in file.exists(tfn) : ungültiges 'file' Argument Basically it says: Converting xls file to csv file...Error in system(cmd, intern = !verbose) : perl not found Error in file.exists(tfn): invalid 'file' Argument What is wrong? How can I call and execute R-scripts from another R-script? Thanks in advance! Regards, Sergey -- Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Executing R scripts from another R script
Sorry everyone Disregard this email. I found the problem. I have xlsReadWritePro loaded automatically at start of R. Then I load package gplots, which also loads gdata gdata masks function read.xls() from xlsReadWritePro and that causes all the problems. Regards, Sergey On Mon, Sep 21, 2009 at 17:52, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I run Eclipse Ganymede and R 2.7.2 at work. I have one R script file where I open in memory a new xls file (using xlsReadWritePro), call other R scripts, which are in the same folder as the main R script, which get data from an existing xls file, process data, and output results in the xls file which is in memory. That is the idea. But I cannot make it work. First, I do not really know how to call other R scripts from an R script. I am using function source like this: source(file=G:/data/datafile.xls) I get a following error (in German, as I work with German Windows): Converting xls file to csv file... Fehler in system(cmd, intern = !verbose) : perl nicht gefunden Fehler in file.exists(tfn) : ungültiges 'file' Argument Basically it says: Converting xls file to csv file...Error in system(cmd, intern = !verbose) : perl not found Error in file.exists(tfn): invalid 'file' Argument What is wrong? How can I call and execute R-scripts from another R-script? Thanks in advance! Regards, Sergey -- Kniven skärpes bara mot stenen. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten Kniven skärpes bara mot stenen. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question related to merging zoo objects and apply function
Hello everyone, Say I have defined a convenience function in my code like this: func - function(x, j){ x[168+j] - x[72+j]+x[144+j] } And now I apply it to some columns in a big zoo object like this: for (m in 1:24){ combined - merge(combined, LA1sum=apply(combined, 1, func, j=m)) } output of this for-loop will be zoo object with columns named LA1sum.1, LA1sum.2, ..., LA1.sum24. If I have a vector of names like this: namesVec - c(LA1sum, LP1sum, GC1sum, LL1sum, LN1sum, SI1sum, LX1sum, CO1sum, CL1sum, QS1sum, HO1sum, NG1sum, XB1sum, C.1sum, FC1sum, LH1sum, LC1sum, S.1sum, W.1sum, KW1sum, CC1sum, KC1sum, CT1sum, SB1sum) What I want is in merge() for each m the name of new column to be one of the elements in namesVec, that is for m=1 I have LA1sum=apply(...,j=1) for m=2 I have LP1sum=apply(...,j=2) etc What it the way to do this? Thank you in advance for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question related to merging zoo objects and apply function
Hello, Gabor Generally, I follow the r-help rules and provide working code snippets to illustrate the problem. In this case it was more methodological question of how to loop names in merge() function. I solved this very simply buy renaming specific columns after I have ran merge(). Thank you for your time! Best, Sergey On Wed, Jul 15, 2009 at 17:18, Gabor Grothendieckggrothendi...@gmail.com wrote: Please read the last line of every message to r-help. In particular simplify this as much as possible and construct some small artificial test data to illustrate. Anyways, func is probably not what you want. It has the same effect as func - function(x, j) x[72+j] + [144+j] On Wed, Jul 15, 2009 at 9:26 AM, Sergey Goriatchevserg...@gmail.com wrote: Hello everyone, Say I have defined a convenience function in my code like this: func - function(x, j){ x[168+j] - x[72+j]+x[144+j] } And now I apply it to some columns in a big zoo object like this: for (m in 1:24){ combined - merge(combined, LA1sum=apply(combined, 1, func, j=m)) } output of this for-loop will be zoo object with columns named LA1sum.1, LA1sum.2, ..., LA1.sum24. If I have a vector of names like this: namesVec - c(LA1sum, LP1sum, GC1sum, LL1sum, LN1sum, SI1sum, LX1sum, CO1sum, CL1sum, QS1sum, HO1sum, NG1sum, XB1sum, C.1sum, FC1sum, LH1sum, LC1sum, S.1sum, W.1sum, KW1sum, CC1sum, KC1sum, CT1sum, SB1sum) What I want is in merge() for each m the name of new column to be one of the elements in namesVec, that is for m=1 I have LA1sum=apply(...,j=1) for m=2 I have LP1sum=apply(...,j=2) etc What it the way to do this? Thank you in advance for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Ordering zoo-object by its index
Hello everyone, Say I have zoo object x.Date - as.Date(2003-02-01) + c(1, 3, 7, 9, 14) - 1 x - zoo(rnorm(5), x.Date) y - zoo(rt(5, df=2), x.Date) z - zoo(rt(5, df=5), x.Date) Data - merge(x,y,z) What should I do to make the latest values appear at the top? Thank you for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Ordering zoo-object by its index
Hi, Gabor Thank you! That is exactly what I did, even before your email. :-) Regards, Sergey On Thu, Jul 9, 2009 at 12:52, Gabor Grothendieckggrothendi...@gmail.com wrote: To display that object in reverse time order try this: as.data.frame(Data)[nrow(Data):1, ] On Thu, Jul 9, 2009 at 5:21 AM, Sergey Goriatchevserg...@gmail.com wrote: Hello everyone, Say I have zoo object x.Date - as.Date(2003-02-01) + c(1, 3, 7, 9, 14) - 1 x - zoo(rnorm(5), x.Date) y - zoo(rt(5, df=2), x.Date) z - zoo(rt(5, df=5), x.Date) Data - merge(x,y,z) What should I do to make the latest values appear at the top? Thank you for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Ordering zoo-object by its index
Hi, Gabor Yes, I am familiar with tail() function. I use it extensively on a day to day basis. In this particular case I needed to order a zoo object by date, after I have created it with RBloomberg. Thank you for your time! Regards, Sergey On Thu, Jul 9, 2009 at 14:36, Gabor Grothendieckggrothendi...@gmail.com wrote: One additional thought. If the reason you want to do that is just so that you can see the last few rows more easily then tail(Data) will display the last few rows or tail(Data, 10) will display the last 10 rows. On Thu, Jul 9, 2009 at 7:36 AM, Sergey Goriatchevserg...@gmail.com wrote: Hi, Gabor Thank you! That is exactly what I did, even before your email. :-) Regards, Sergey On Thu, Jul 9, 2009 at 12:52, Gabor Grothendieckggrothendi...@gmail.com wrote: To display that object in reverse time order try this: as.data.frame(Data)[nrow(Data):1, ] On Thu, Jul 9, 2009 at 5:21 AM, Sergey Goriatchevserg...@gmail.com wrote: Hello everyone, Say I have zoo object x.Date - as.Date(2003-02-01) + c(1, 3, 7, 9, 14) - 1 x - zoo(rnorm(5), x.Date) y - zoo(rt(5, df=2), x.Date) z - zoo(rt(5, df=5), x.Date) Data - merge(x,y,z) What should I do to make the latest values appear at the top? Thank you for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 optimize a piece of code involving zoo objects
Hi, everybody OK, I got it working with recursive. Don't know why this argument slipped my mind, as I use filter() so often! Now it is 44 times faster, which is good enough for me. :-) Thank you, Gabor and Jim. Best, Sergey On Fri, Jun 19, 2009 at 15:23, jim holtmanjholt...@gmail.com wrote: check out 'filter' to see if it does what you want with the 'recursive' option. On Fri, Jun 19, 2009 at 3:33 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a long script that uses zoo objects. In this script I used simple moving averages and these I can very efficiently calculate with filter() functions. Now, I have to use special exponential moving averages, and the only way I could write the code was with a for-loop, which makes everything extremely slow. I don't know how to optimize the code, but I need to find a solution. I hope someone can help me. The special moving average is calculated in the following way: EMA = ( K x ( C - P ) ) + P where, C = Current Value P = Previous periods EMA (A SMA is used for the first period's calculation) K = Exponential smoothing constant K = 2 / ( 1 + Periods ) Below is the code with the for-loop. -temp contains C -Periods is variable j in the for loop (so K varies) - I first produce a vector of simple equally weighted moving average, and use the first non-NA value to initiate the second for-loop x.Date - as.Date(2003-02-01) + seq(1,1100) - 1 temp - zoo(rnorm(1100, 0, 10)+100, x.Date) start.time - proc.time() for(j in seq(5,100,by=5)){ #PRODUCE FAST MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smafast - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/j, j), sides=1)), order.by=time(temp)) #index of first non-NA value, which is the first SMA needed #which(is.na(smafast))[length(which(is.na(smafast)))]+1 #Calculate decay factor K #number of periods is j K - 2/(1+j) #Calculate recursively the EMA for the fast index (starting with second non-NA value) for (k in (which(is.na(smafast))[length(which(is.na(smafast)))]+2):length(smafast)) { smafast[k] - coredata(smafast[k-1])+K*(coredata(temp[k,1])-coredata(smafast[k-1])) } #PRODUCE SLOW MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smaslow - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/(j*4), (j*4)), sides=1)), order.by=time(temp)) K - 2/(1+j*4) #Calculate EMA for (k in (which(is.na(smaslow))[length(which(is.na(smaslow)))]+2):length(smaslow)) { smaslow[k] - coredata(smaslow[k-1])+K*(coredata(temp[k,1])-coredata(smaslow[k-1])) } #COMBINE DIFFERENCES OF FAST AND SLOW temp - merge(temp, ma=smafast-smaslow) } proc.time()-start.time -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] (FULL) Need help to optimize a piece of code involving zoo objects
(Sorry, sent the message before I finished it) Hello, everyone I have a long script that uses zoo objects. In this script I used simple moving averages and these I can very efficiently calculate with filter() functions. Now, I have to use special exponential moving averages, and the only way I could write the code was with a for-loop, which makes everything extremely slow. I don't know how to optimize the code, but I need to find a solution. I hope someone can help me. The special moving average is calculated in the following way: EMA = ( K x ( C - P ) ) + P where, C = Current Value P = Previous periods EMA (A SMA is used for the first period's calculation) K = Exponential smoothing constant K = 2 / ( 1 + Periods ) Below is the code with the for-loop. -temp contains C -Periods is variable j in the for loop (so K varies) - I first produce a vector of simple equally weighted moving average, and use the first non-NA value to initiate the second for-loop x.Date - as.Date(2003-02-01) + seq(1,1100) - 1 temp - zoo(rnorm(1100, 0, 10)+100, x.Date) start.time - proc.time() for(j in seq(5,100,by=5)){ #PRODUCE FAST MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smafast - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/j, j), sides=1)), order.by=time(temp)) #index of first non-NA value, which is the first SMA needed #which(is.na(smafast))[length(which(is.na(smafast)))]+1 #Calculate decay factor K #number of periods is j K - 2/(1+j) #Calculate recursively the EMA for the fast index (starting with second non-NA value) for (k in (which(is.na(smafast))[length(which(is.na(smafast)))]+2):length(smafast)) { smafast[k] - coredata(smafast[k-1])+K*(coredata(temp[k,1])-coredata(smafast[k-1])) } #PRODUCE SLOW MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smaslow - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/(j*4), (j*4)), sides=1)), order.by=time(temp)) K - 2/(1+j*4) #Calculate EMA for (k in (which(is.na(smaslow))[length(which(is.na(smaslow)))]+2):length(smaslow)) { smaslow[k] - coredata(smaslow[k-1])+K*(coredata(temp[k,1])-coredata(smaslow[k-1])) } #COMBINE DIFFERENCES OF FAST AND SLOW temp - merge(temp, ma=smafast-smaslow) } proc.time()-start.time -- Could someone help me to optimize the two EMA for-loops within the bigger for-loop? I need to cut the execution time down by at least half. Thank you in advance for your help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Need help to optimize a piece of code involving zoo objects
Hello, everyone I have a long script that uses zoo objects. In this script I used simple moving averages and these I can very efficiently calculate with filter() functions. Now, I have to use special exponential moving averages, and the only way I could write the code was with a for-loop, which makes everything extremely slow. I don't know how to optimize the code, but I need to find a solution. I hope someone can help me. The special moving average is calculated in the following way: EMA = ( K x ( C - P ) ) + P where, C = Current Value P = Previous periods EMA(A SMA is used for the first period's calculation) K = Exponential smoothing constant K = 2 / ( 1 + Periods ) Below is the code with the for-loop. -temp contains C -Periods is variable j in the for loop (so K varies) - I first produce a vector of simple equally weighted moving average, and use the first non-NA value to initiate the second for-loop x.Date - as.Date(2003-02-01) + seq(1,1100) - 1 temp - zoo(rnorm(1100, 0, 10)+100, x.Date) start.time - proc.time() for(j in seq(5,100,by=5)){ #PRODUCE FAST MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smafast - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/j, j), sides=1)), order.by=time(temp)) #index of first non-NA value, which is the first SMA needed #which(is.na(smafast))[length(which(is.na(smafast)))]+1 #Calculate decay factor K #number of periods is j K - 2/(1+j) #Calculate recursively the EMA for the fast index (starting with second non-NA value) for (k in (which(is.na(smafast))[length(which(is.na(smafast)))]+2):length(smafast)) { smafast[k] - coredata(smafast[k-1])+K*(coredata(temp[k,1])-coredata(smafast[k-1])) } #PRODUCE SLOW MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smaslow - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/(j*4), (j*4)), sides=1)), order.by=time(temp)) K - 2/(1+j*4) #Calculate EMA for (k in (which(is.na(smaslow))[length(which(is.na(smaslow)))]+2):length(smaslow)) { smaslow[k] - coredata(smaslow[k-1])+K*(coredata(temp[k,1])-coredata(smaslow[k-1])) } #COMBINE DIFFERENCES OF FAST AND SLOW temp - merge(temp, ma=smafast-smaslow) } proc.time()-start.time -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 optimize a piece of code involving zoo objects
Dear Gabor and Jim I am not looking at the recursive method for filter() Recursive filter with lag 1 is specified in help files as: y[i] = x[i] + f[1]*y[i-1] My function looks like this: EMA[i] = K*(C[i] - EMA[i-1]) + EMA[i-1], that is: y[i]=EMA[i] y[i-1]=EMA[i-1] x[i]=C[i] So, I modified my function to look like the recursive filter in help files: EMA[i]/K = C[i] + EMA[i-1]*((1 - K)/K) I will then produce a time series object of EMA[i]/K an multiply by K to get back to EMA[i]. (I also need to get my time index correct, because init=SMA, so some initial values of C fall away). Do you think this is correct way to do this, or have I missed something? Regards, Sergey On Fri, Jun 19, 2009 at 15:23, jim holtmanjholt...@gmail.com wrote: check out 'filter' to see if it does what you want with the 'recursive' option. On Fri, Jun 19, 2009 at 3:33 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a long script that uses zoo objects. In this script I used simple moving averages and these I can very efficiently calculate with filter() functions. Now, I have to use special exponential moving averages, and the only way I could write the code was with a for-loop, which makes everything extremely slow. I don't know how to optimize the code, but I need to find a solution. I hope someone can help me. The special moving average is calculated in the following way: EMA = ( K x ( C - P ) ) + P where, C = Current Value P = Previous periods EMA (A SMA is used for the first period's calculation) K = Exponential smoothing constant K = 2 / ( 1 + Periods ) Below is the code with the for-loop. -temp contains C -Periods is variable j in the for loop (so K varies) - I first produce a vector of simple equally weighted moving average, and use the first non-NA value to initiate the second for-loop x.Date - as.Date(2003-02-01) + seq(1,1100) - 1 temp - zoo(rnorm(1100, 0, 10)+100, x.Date) start.time - proc.time() for(j in seq(5,100,by=5)){ #PRODUCE FAST MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smafast - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/j, j), sides=1)), order.by=time(temp)) #index of first non-NA value, which is the first SMA needed #which(is.na(smafast))[length(which(is.na(smafast)))]+1 #Calculate decay factor K #number of periods is j K - 2/(1+j) #Calculate recursively the EMA for the fast index (starting with second non-NA value) for (k in (which(is.na(smafast))[length(which(is.na(smafast)))]+2):length(smafast)) { smafast[k] - coredata(smafast[k-1])+K*(coredata(temp[k,1])-coredata(smafast[k-1])) } #PRODUCE SLOW MOVING AVERAGE #Create equally weighted MA vector (we need only the first value) smaslow - zoo(coredata(filter(coredata(temp[,1]), filter=rep(1/(j*4), (j*4)), sides=1)), order.by=time(temp)) K - 2/(1+j*4) #Calculate EMA for (k in (which(is.na(smaslow))[length(which(is.na(smaslow)))]+2):length(smaslow)) { smaslow[k] - coredata(smaslow[k-1])+K*(coredata(temp[k,1])-coredata(smaslow[k-1])) } #COMBINE DIFFERENCES OF FAST AND SLOW temp - merge(temp, ma=smafast-smaslow) } proc.time()-start.time -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Taking diff of character vectors
Hello, everybody Say I have nm1 - c(rep(1,10), rep(0,10)) then I can do: diff(nm1) to see where I have shift in value but what if I have nm2 - c(rep(SPZ8, 10), rep(SPX9, 10)) how can I produce the same ouput as diff(nm1) does, that is zeros everywhere except for one place where SPZ8 changes to SPX9 (there should be 1 there)? What if I have a matrix of characters like that: nm3 - c(rep(GLF9, 4), rep(GLF10, 16)) matr - cbind(nm2, nm3) How can I efficiently create two more columns that contain zeros everywhere except for place where there is shift in character values? Thanks for help! Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Taking diff of character vectors
Fabulous, guys! Let me try your suggestions. Thanks a lot! Best, SErgey On Fri, Mar 13, 2009 at 11:53, markle...@verizon.net wrote: Hi: For A, you can use head and tail but you have to add a zero the front. For B, you can use the same function, but put it inside an sapply and run over the columns and then cbind it back with the original dataframe. A) nm2 - c(rep(SPZ8, 10), rep(SPX9, 10)) -1.0*c(0,as.numeric((head(nm2,-1) != tail(nm2,-1 B) nm3 - c(rep(GLF9, 4), rep(GLF10, 16)) matr - cbind(nm2, nm3) temp-as.data.frame(sapply(1:ncol(matr), function(.col) { -1.0*c(0,as.numeric((head(matr[,.col],-1) != tail(matr[,.col],-1 })) cbind(matr,temp) On Fri, Mar 13, 2009 at 5:24 AM, Sergey Goriatchev wrote: Hello, everybody Say I have nm1 - c(rep(1,10), rep(0,10)) then I can do: diff(nm1) to see where I have shift in value but what if I have nm2 - c(rep(SPZ8, 10), rep(SPX9, 10)) how can I produce the same ouput as diff(nm1) does, that is zeros everywhere except for one place where SPZ8 changes to SPX9 (there should be 1 there)? What if I have a matrix of characters like that: nm3 - c(rep(GLF9, 4), rep(GLF10, 16)) matr - cbind(nm2, nm3) How can I efficiently create two more columns that contain zeros everywhere except for place where there is shift in character values? Thanks for help! Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ r-h...@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [R-SIG-Finance] Problem with RBloomberg (not the usual one)
Hi, Kent Thank you for that! I'll pass the info to our systems administrator, he is the only one who is allowed to do things like that. Have a nice weekend! Best, Serge On Fri, Mar 13, 2009 at 15:32, Voss, Kent vo...@kochind.com wrote: Sergey, Sorry I missed your post earlier. I had this same problem and I reinstalled the DDE Server and Excel Add-In, rebooted and it fixed it. I thought they were already installed as I could pull down data in Excel from Bloomberg, but reinstalling them fixed my R problem. You can find the downloads at http://about.bloomberg.com/contact_softwaresupport_upgr.html Good luck. Kent -Original Message- From: Sergey Goriatchev [mailto:serg...@gmail.com] Sent: Friday, February 27, 2009 1:54 AM To: r-sig-fina...@stat.math.ethz.ch; r-help@r-project.org Subject: Re: [R-SIG-Finance] Problem with RBloomberg (not the usual one) Hello, again, everyone I went through the code and narrowed down the problem in blpConnect: COMCreate(Bloomberg.Data.1) which then calls getCOMInstance does not work, because getCLSID(Bloomberg.Data.1) returns Fehler: Invalid class string What is this problem??? Best, Sergey On Fri, Feb 27, 2009 at 09:16, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone! I have a problem with RBloomberg and this is not the usual no administrator rights problem. I have R 2.7.2, RBloomberg 0.1-10, RDCOMclient 0.92-0 RDCOMClient, chron, zoo, stats: these packages load OK. Then, trying to connect, I get following error message: conn - blpConnect(show.days=week, na.action=previous.days, periodicity=daily) Warning messages: 1: In getCOMInstance(name, force = TRUE, silent = TRUE) : Couldn't get clsid from the string 2: In blpConnect(show.days = week, na.action = previous.days, periodicity = daily) : Seems like this is not a Bloomberg Workstation: Error : Invalid class string Anyone encountered this problem? What is wrong and how can I solve it? Online, I found just one instance of this problem discussed, and it was in Chinese: http://cos.name/bbs/read.php?tid=12821fpage=3 Thank you for your help! Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten EMF kochind.com made the following annotations. -- The information in this e-mail and any attachments is confidential and intended solely for the attention and use of the named addressee(s). It must not be disclosed to any person without proper authority. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, you are not authorized to and must not disclose, copy, distribute, or retain this message or any part of it. == -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about multiple plots of zoo objects
Hello, everyone I have a zoo object containing several time series of daily frequency. One of these timeseries is an indicator function with value (-1) at certain times, and (+1) at the other. I do a plot of several of the timeseries in one go (a multiple plot). I wonder if I can automatically in EACH plot color the area where indicator variable is (-1)? Of course, I could do it simply with layout() and then for each timeseries do plot and a color overlay, but I wonder if with plot.zoo() simething similar is possible to do automatically. Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about multiple plots of zoo objects
Hi, Gabor No, what I am trying to do is similar to: abline(v=time(spread)[spread[,Indicator]==(-1)], col=yellow), where spread is the multivariate zoo object (say, 5 timeseries). That is, I want to color parts of the plots where indicator==(-1), but do the coloring without using layout() and then repeating plot() and abline() for each of the 5 timeseries. Best, Sergey On Tue, Mar 3, 2009 at 14:22, Gabor Grothendieck ggrothendi...@gmail.com wrote: Are you trying to color the points themselves? This plots the first two series in frame 1 (they are the same but one is plotted as points and the other as a line) and the third series is shown in frame 2 and for the series of points it colors them green or red. The lines are all colored black: library(zoo) set.seed(1) x - zoo(rnorm(10)) s - sign(x) plot(cbind(x, x, s), screen = c(1, 1, 2), type = c(p, l, l), col = list(ifelse(s 0, green, red), 1, 1), pch = 20) On Tue, Mar 3, 2009 at 7:48 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a zoo object containing several time series of daily frequency. One of these timeseries is an indicator function with value (-1) at certain times, and (+1) at the other. I do a plot of several of the timeseries in one go (a multiple plot). I wonder if I can automatically in EACH plot color the area where indicator variable is (-1)? Of course, I could do it simply with layout() and then for each timeseries do plot and a color overlay, but I wonder if with plot.zoo() simething similar is possible to do automatically. Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about multiple plots of zoo objects
Gabor, yes, I want to color portions of EACH plot of the MULTIPLE plot done with plot.zoo() I tried to do: plot(multivariate zoo object) abline(v=...) but that does not work. I will check your suggestions of the examples. Thank you for your help, as always! Best, Sergey On Tue, Mar 3, 2009 at 14:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: Do you mean you want to shade a portion of the plot? There are two examples of that in the examples section of ?plot.zoo and a further example using xyplot.zoo in the examples section of ?xyplot.zoo On Tue, Mar 3, 2009 at 8:37 AM, Sergey Goriatchev serg...@gmail.com wrote: Hi, Gabor No, what I am trying to do is similar to: abline(v=time(spread)[spread[,Indicator]==(-1)], col=yellow), where spread is the multivariate zoo object (say, 5 timeseries). That is, I want to color parts of the plots where indicator==(-1), but do the coloring without using layout() and then repeating plot() and abline() for each of the 5 timeseries. Best, Sergey On Tue, Mar 3, 2009 at 14:22, Gabor Grothendieck ggrothendi...@gmail.com wrote: Are you trying to color the points themselves? This plots the first two series in frame 1 (they are the same but one is plotted as points and the other as a line) and the third series is shown in frame 2 and for the series of points it colors them green or red. The lines are all colored black: library(zoo) set.seed(1) x - zoo(rnorm(10)) s - sign(x) plot(cbind(x, x, s), screen = c(1, 1, 2), type = c(p, l, l), col = list(ifelse(s 0, green, red), 1, 1), pch = 20) On Tue, Mar 3, 2009 at 7:48 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a zoo object containing several time series of daily frequency. One of these timeseries is an indicator function with value (-1) at certain times, and (+1) at the other. I do a plot of several of the timeseries in one go (a multiple plot). I wonder if I can automatically in EACH plot color the area where indicator variable is (-1)? Of course, I could do it simply with layout() and then for each timeseries do plot and a color overlay, but I wonder if with plot.zoo() simething similar is possible to do automatically. Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about multiple plots of zoo objects
Supreme! Thanks Gabor! On Tue, Mar 3, 2009 at 15:20, Gabor Grothendieck ggrothendi...@gmail.com wrote: If you want to use abline on a multivariate plot it must be issued within a panel function like this: # same set up as in prior email p - function(x, y, ...) { points(x, y); lines(x, y); abline(v = time(s)[s 0], col = green) } plot(cbind(x, s), panel = p) There are further examples of panel functions in the examples section of ?plot.zoo in case what you want is a variation of the above. On Tue, Mar 3, 2009 at 9:04 AM, Sergey Goriatchev serg...@gmail.com wrote: Gabor, yes, I want to color portions of EACH plot of the MULTIPLE plot done with plot.zoo() I tried to do: plot(multivariate zoo object) abline(v=...) but that does not work. I will check your suggestions of the examples. Thank you for your help, as always! Best, Sergey On Tue, Mar 3, 2009 at 14:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: Do you mean you want to shade a portion of the plot? There are two examples of that in the examples section of ?plot.zoo and a further example using xyplot.zoo in the examples section of ?xyplot.zoo On Tue, Mar 3, 2009 at 8:37 AM, Sergey Goriatchev serg...@gmail.com wrote: Hi, Gabor No, what I am trying to do is similar to: abline(v=time(spread)[spread[,Indicator]==(-1)], col=yellow), where spread is the multivariate zoo object (say, 5 timeseries). That is, I want to color parts of the plots where indicator==(-1), but do the coloring without using layout() and then repeating plot() and abline() for each of the 5 timeseries. Best, Sergey On Tue, Mar 3, 2009 at 14:22, Gabor Grothendieck ggrothendi...@gmail.com wrote: Are you trying to color the points themselves? This plots the first two series in frame 1 (they are the same but one is plotted as points and the other as a line) and the third series is shown in frame 2 and for the series of points it colors them green or red. The lines are all colored black: library(zoo) set.seed(1) x - zoo(rnorm(10)) s - sign(x) plot(cbind(x, x, s), screen = c(1, 1, 2), type = c(p, l, l), col = list(ifelse(s 0, green, red), 1, 1), pch = 20) On Tue, Mar 3, 2009 at 7:48 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a zoo object containing several time series of daily frequency. One of these timeseries is an indicator function with value (-1) at certain times, and (+1) at the other. I do a plot of several of the timeseries in one go (a multiple plot). I wonder if I can automatically in EACH plot color the area where indicator variable is (-1)? Of course, I could do it simply with layout() and then for each timeseries do plot and a color overlay, but I wonder if with plot.zoo() simething similar is possible to do automatically. Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 RBloomberg (not the usual one)
Hello, everyone! I have a problem with RBloomberg and this is not the usual no administrator rights problem. I have R 2.7.2, RBloomberg 0.1-10, RDCOMclient 0.92-0 RDCOMClient, chron, zoo, stats: these packages load OK. Then, trying to connect, I get following error message: conn - blpConnect(show.days=week, na.action=previous.days, periodicity=daily) Warning messages: 1: In getCOMInstance(name, force = TRUE, silent = TRUE) : Couldn't get clsid from the string 2: In blpConnect(show.days = week, na.action = previous.days, periodicity = daily) : Seems like this is not a Bloomberg Workstation: Error : Invalid class string Anyone encountered this problem? What is wrong and how can I solve it? Online, I found just one instance of this problem discussed, and it was in Chinese: http://cos.name/bbs/read.php?tid=12821fpage=3 Thank you for your help! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about apply()
Hello, everyone! Assume you have this data: data - structure(c(66.609375, 67.09375, 66.40625, 66.734375, 67.109375, 66.875, 66.09375, 65.921875, 66.546875, 66.140625, 66.140625, 65.65625, 65.875, 65.59375, 65.515625, 66.09375, 66.015625, 66.140625, 66.109375, 66.421875, 1702.7, 1647.7, 1649.4, 1639.9, 1696.4, 1710.9, 1690.2, 1677.9, 1694.4, 1713.9, 1713.9, 1705.4, 1708.4, 1692.9, 1689.6, 1647.7, 1654.5, 1651.3, 1645.7, 1602.4, 453.7, 447.8, 446.2, 446.5, 447, 446.8, 448.5, 447.8, 449.2, 449, 449, 453.7, 454.4, 453.4, 453.8, 452.2, 450.7, 450.6, 451.4, 447.5, 18.34, 18.29, 17.65, 17.52, 16.96, 17.41, 18.51, 19.02, 19.43, 20.76, 20.76, 21.59, 22.28, 22.4, 22.63, 22.26, 22.71, 22.27, 21.75, 21.65), .Dim = c(20L, 4L), .Dimnames = list(NULL, c(TY1.lev, SP1.lev, GC1.lev, CL1.lev)), index = structure(c(10959, 10960, 10961, 10962, 10963, 10966, 10967, 10968, 10969, 10970, 10973, 10974, 10975, 10976, 10977, 10980, 10981, 10982, 10983, 10984), class = Date), class = zoo) What I want to do is to calculate simple return on each column (return=P1/P0-1) and put it in new columns. I've tried like this: #convenience function func - function(x,z){ a - embed(x[z, drop=FALSE], 2)[,1]/embed(x[z, drop=FALSE], 2)[,2] - 1 return(a) } data - merge(data, Bond.ret=zoo(apply(data, 2, func, z=TY1.lev), order.by=time(data)[-1])) and I get this: Error in embed(x[z, drop = FALSE], 2) : wrong embedding dimension What am I doing wrong? (Somehow I cannot understand how to work with columns in apply(), with rows, that is apply(,1,FUN) I have no problem. Thank you for help, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about apply()
Dear Gabor, Thank you as always. I will follow your suggestion. But, I still want to know what I do wrong in the above code (with embed(), apply(), and merge() functions). Why do I get that error message and how can I rewrite the code to make it run. Best Regards, Sergey On Wed, Feb 11, 2009 at 13:34, Gabor Grothendieck ggrothendi...@gmail.com wrote: See ?diff.zoo dif - diff(data, arithmetic = FALSE) - 1 cbind(data, dif) On Wed, Feb 11, 2009 at 6:21 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone! Assume you have this data: data - structure(c(66.609375, 67.09375, 66.40625, 66.734375, 67.109375, 66.875, 66.09375, 65.921875, 66.546875, 66.140625, 66.140625, 65.65625, 65.875, 65.59375, 65.515625, 66.09375, 66.015625, 66.140625, 66.109375, 66.421875, 1702.7, 1647.7, 1649.4, 1639.9, 1696.4, 1710.9, 1690.2, 1677.9, 1694.4, 1713.9, 1713.9, 1705.4, 1708.4, 1692.9, 1689.6, 1647.7, 1654.5, 1651.3, 1645.7, 1602.4, 453.7, 447.8, 446.2, 446.5, 447, 446.8, 448.5, 447.8, 449.2, 449, 449, 453.7, 454.4, 453.4, 453.8, 452.2, 450.7, 450.6, 451.4, 447.5, 18.34, 18.29, 17.65, 17.52, 16.96, 17.41, 18.51, 19.02, 19.43, 20.76, 20.76, 21.59, 22.28, 22.4, 22.63, 22.26, 22.71, 22.27, 21.75, 21.65), .Dim = c(20L, 4L), .Dimnames = list(NULL, c(TY1.lev, SP1.lev, GC1.lev, CL1.lev)), index = structure(c(10959, 10960, 10961, 10962, 10963, 10966, 10967, 10968, 10969, 10970, 10973, 10974, 10975, 10976, 10977, 10980, 10981, 10982, 10983, 10984), class = Date), class = zoo) What I want to do is to calculate simple return on each column (return=P1/P0-1) and put it in new columns. I've tried like this: #convenience function func - function(x,z){ a - embed(x[z, drop=FALSE], 2)[,1]/embed(x[z, drop=FALSE], 2)[,2] - 1 return(a) } data - merge(data, Bond.ret=zoo(apply(data, 2, func, z=TY1.lev), order.by=time(data)[-1])) and I get this: Error in embed(x[z, drop = FALSE], 2) : wrong embedding dimension What am I doing wrong? (Somehow I cannot understand how to work with columns in apply(), with rows, that is apply(,1,FUN) I have no problem. Thank you for help, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about apply()
Dear Peter, I tried with the comma already, it did not work. rets TY1.lev SP1.lev GC1.lev CL1.lev 2000-01-03 66.60938 1702.7 453.7 18.34 2000-01-04 67.09375 1647.7 447.8 18.29 2000-01-05 66.40625 1649.4 446.2 17.65 2000-01-06 66.73438 1639.9 446.5 17.52 2000-01-07 67.10938 1696.4 447.0 16.96 2000-01-10 66.87500 1710.9 446.8 17.41 2000-01-11 66.09375 1690.2 448.5 18.51 2000-01-12 65.92188 1677.9 447.8 19.02 2000-01-13 66.54688 1694.4 449.2 19.43 2000-01-14 66.14062 1713.9 449.0 20.76 2000-01-17 66.14062 1713.9 449.0 20.76 2000-01-18 65.65625 1705.4 453.7 21.59 2000-01-19 65.87500 1708.4 454.4 22.28 2000-01-20 65.59375 1692.9 453.4 22.40 2000-01-21 65.51562 1689.6 453.8 22.63 2000-01-24 66.09375 1647.7 452.2 22.26 2000-01-25 66.01562 1654.5 450.7 22.71 2000-01-26 66.14062 1651.3 450.6 22.27 2000-01-27 66.10938 1645.7 451.4 21.75 2000-01-28 66.42188 1602.4 447.5 21.65 func - function(x,z){ + + a - embed(x[,z, drop=FALSE], 2)[,1]/embed(x[,z, drop=FALSE], 2)[,2] - 1 + + return(a) + } merge(rets, Bond.ret=zoo(apply(rets,2,func,z=TY1.lev), order.by=time(rets)[-1])) Error in x[, z, drop = FALSE] : incorrect number of dimensions Nothing was wrong with your suggestion, except that I did not come up with it. :-) I've been using a lot of embed and apply(, 1, FUNC) functions lately and out of inertia I tried the same. I specify a new variable (with suffix .ret) within merge() function. Kind Regards, Sergey On Wed, Feb 11, 2009 at 13:47, Peter Dalgaard p.dalga...@biostat.ku.dk wrote: Sergey Goriatchev wrote: Hello, everyone! Assume you have this data: data - structure(c(66.609375, 67.09375, 66.40625, 66.734375, 67.109375, 66.875, 66.09375, 65.921875, 66.546875, 66.140625, 66.140625, 65.65625, 65.875, 65.59375, 65.515625, 66.09375, 66.015625, 66.140625, 66.109375, 66.421875, 1702.7, 1647.7, 1649.4, 1639.9, 1696.4, 1710.9, 1690.2, 1677.9, 1694.4, 1713.9, 1713.9, 1705.4, 1708.4, 1692.9, 1689.6, 1647.7, 1654.5, 1651.3, 1645.7, 1602.4, 453.7, 447.8, 446.2, 446.5, 447, 446.8, 448.5, 447.8, 449.2, 449, 449, 453.7, 454.4, 453.4, 453.8, 452.2, 450.7, 450.6, 451.4, 447.5, 18.34, 18.29, 17.65, 17.52, 16.96, 17.41, 18.51, 19.02, 19.43, 20.76, 20.76, 21.59, 22.28, 22.4, 22.63, 22.26, 22.71, 22.27, 21.75, 21.65), .Dim = c(20L, 4L), .Dimnames = list(NULL, c(TY1.lev, SP1.lev, GC1.lev, CL1.lev)), index = structure(c(10959, 10960, 10961, 10962, 10963, 10966, 10967, 10968, 10969, 10970, 10973, 10974, 10975, 10976, 10977, 10980, 10981, 10982, 10983, 10984), class = Date), class = zoo) What I want to do is to calculate simple return on each column (return=P1/P0-1) and put it in new columns. I've tried like this: #convenience function func - function(x,z){ a - embed(x[z, drop=FALSE], 2)[,1]/embed(x[z, drop=FALSE], 2)[,2] - 1 return(a) } data - merge(data, Bond.ret=zoo(apply(data, 2, func, z=TY1.lev), order.by=time(data)[-1])) and I get this: Error in embed(x[z, drop = FALSE], 2) : wrong embedding dimension What am I doing wrong? (Somehow I cannot understand how to work with columns in apply(), with rows, that is apply(,1,FUN) I have no problem. The immediate problem is that you are missing a comma in x[, z, drop = FALSE]. But what was wrong with cbind(data,data/lag(data,-1)-1, suffixes=c(,r)) (except that it adds a . to the orginal names, I see no way of NOT adding a suffix?) -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Question about apply()
Understood. Peter, Gabor, thank you for your time and help. Regards, Sergey On Wed, Feb 11, 2009 at 14:24, Gabor Grothendieck ggrothendi...@gmail.com wrote: There is no zoo method for embed. embed is implicit in rollapplly so its not normally needed in zoo and rollapply, lag or diff would normally be used instead. If you did want to use embed then unzoo data first but that will lose the time index so add it back in later: e - embed(coredata(data),2) dif - zoo(e[, 1:4] / e[, 5:8] - 1, time(data)[-1]) cbind(data, dif) On Wed, Feb 11, 2009 at 7:45 AM, Sergey Goriatchev serg...@gmail.com wrote: Dear Gabor, Thank you as always. I will follow your suggestion. But, I still want to know what I do wrong in the above code (with embed(), apply(), and merge() functions). Why do I get that error message and how can I rewrite the code to make it run. Best Regards, Sergey On Wed, Feb 11, 2009 at 13:34, Gabor Grothendieck ggrothendi...@gmail.com wrote: See ?diff.zoo dif - diff(data, arithmetic = FALSE) - 1 cbind(data, dif) On Wed, Feb 11, 2009 at 6:21 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone! Assume you have this data: data - structure(c(66.609375, 67.09375, 66.40625, 66.734375, 67.109375, 66.875, 66.09375, 65.921875, 66.546875, 66.140625, 66.140625, 65.65625, 65.875, 65.59375, 65.515625, 66.09375, 66.015625, 66.140625, 66.109375, 66.421875, 1702.7, 1647.7, 1649.4, 1639.9, 1696.4, 1710.9, 1690.2, 1677.9, 1694.4, 1713.9, 1713.9, 1705.4, 1708.4, 1692.9, 1689.6, 1647.7, 1654.5, 1651.3, 1645.7, 1602.4, 453.7, 447.8, 446.2, 446.5, 447, 446.8, 448.5, 447.8, 449.2, 449, 449, 453.7, 454.4, 453.4, 453.8, 452.2, 450.7, 450.6, 451.4, 447.5, 18.34, 18.29, 17.65, 17.52, 16.96, 17.41, 18.51, 19.02, 19.43, 20.76, 20.76, 21.59, 22.28, 22.4, 22.63, 22.26, 22.71, 22.27, 21.75, 21.65), .Dim = c(20L, 4L), .Dimnames = list(NULL, c(TY1.lev, SP1.lev, GC1.lev, CL1.lev)), index = structure(c(10959, 10960, 10961, 10962, 10963, 10966, 10967, 10968, 10969, 10970, 10973, 10974, 10975, 10976, 10977, 10980, 10981, 10982, 10983, 10984), class = Date), class = zoo) What I want to do is to calculate simple return on each column (return=P1/P0-1) and put it in new columns. I've tried like this: #convenience function func - function(x,z){ a - embed(x[z, drop=FALSE], 2)[,1]/embed(x[z, drop=FALSE], 2)[,2] - 1 return(a) } data - merge(data, Bond.ret=zoo(apply(data, 2, func, z=TY1.lev), order.by=time(data)[-1])) and I get this: Error in embed(x[z, drop = FALSE], 2) : wrong embedding dimension What am I doing wrong? (Somehow I cannot understand how to work with columns in apply(), with rows, that is apply(,1,FUN) I have no problem. Thank you for help, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Replacing dot with empty space
Hello, everyone How do I replace dot with empty space in ED4.Comdty? I need to get ED4 Comdty tried sub() in many different ways, like sub({.}, , ED4.Comdty) etc but could not do it. Thanks in advance, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing dot with empty space
OK, got it now: sub([.], , ED4.Comdty) On Tue, Feb 10, 2009 at 10:01, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone How do I replace dot with empty space in ED4.Comdty? I need to get ED4 Comdty tried sub() in many different ways, like sub({.}, , ED4.Comdty) etc but could not do it. Thanks in advance, Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing dot with empty space
Thanks, Gabor. You are always very helpful. On Tue, Feb 10, 2009 at 12:42, Gabor Grothendieck ggrothendi...@gmail.com wrote: Also, try these alternatives: sub(., , x, fixed = TRUE) chartr(., , x) On Tue, Feb 10, 2009 at 4:02 AM, Sergey Goriatchev serg...@gmail.com wrote: OK, got it now: sub([.], , ED4.Comdty) On Tue, Feb 10, 2009 at 10:01, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone How do I replace dot with empty space in ED4.Comdty? I need to get ED4 Comdty tried sub() in many different ways, like sub({.}, , ED4.Comdty) etc but could not do it. Thanks in advance, Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 creation of columns in zoo object
Hello, everyone I have a question. Assume I have the following zoo object: me.la - structure(c(1524.75, 1554.5, 1532.25, 1587.5, 1575.25, 1535.5, 1550, 1493.5, 1492.5, 1472.25, 1457.5, 1442.75, 1399, 1535.75, 1565.25, 1543.5, 1598.5, 1586.5, 1547, 1561.5, 1504.75, 1503.75, 1483.75, 1468.75, 1453.75, 1410, 1546.75, 1575.25, 1554, 1609, 1597.5, 1558.5, 1573, 1516.25, 1515.5, 1495, 1480, 1465, 1421.25, 1561.5, 1590, 1568.75, 1623.5, 1612, 1573, 1587.5, 1530.5, 1530, 1509.75, 1494.5, 1479.5, 1435.75, 1573.5, 1601.5, 1580.25, 1635, 1623.5, 1584.5, 1599, 1541.75, 1541.5, 1521.5, 1506, 1491, 1447.25, 1585.5, 1613, 1591.75, 1646, 1634.5, 1595.5, 1610, 1552.75, 1552.75, 1532.75, 1517, 1502, 1458.25, 1600, 1627.5, 1606.5, 1660, 1649, 1609.75, 1624.25, 1567, 1567, 1547, 1531, 1516, 1472.25, 1612, 1639.5, 1618.25, 1671.5, 1661, 1621.5, 1635.75, 1578, 1578, 1558, 1542, 1527, 1483.5), .Dim = c(13L, 8L), index = structure(c(14245, 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, 14259, 14260, 14263), format = m/d/y, origin = structure(c(1, 1, 1970), .Names = c(month, day, year)), class = c(dates, times)), class = zoo, .Dimnames = list(NULL, c(LA1 COMDTY, LA2 COMDTY, LA3 COMDTY, LA4 COMDTY, LA5 COMDTY, LA6 COMDTY, LA7 COMDTY, LA8 COMDTY))) I also have a following variable (used in RBloomberg) in my environment: me.la.tickers - c(LA1 Comdty, LA2 Comdty, LA3 Comdty, LA4 Comdty, LA5 Comdty, LA6 Comdty, LA7 Comdty, LA8 Comdty) What I need to do is to automate the following manual piece of code: me.la$LA2tr - 0 me.la$LA3tr - 0 me.la$LA4tr - 0 me.la$LA5tr - 0 me.la$LA6tr - 0 me.la$LA7tr - 0 me.la$LA8tr - 0 Basically, I need to automatically create new columns in futures object taking first part of names in me.la.tickers. I tried with paste() and assign() combination, but could not get. I could do the manual part, of course, it does not take much time, but: 1) I want to learn how to automatically create new columns in zoo objects 2) I have many more variables that have to be treated similarly (that is, I have me.lp with corresponding me.lp.tickers, me.qc with me.qc.tickers, etc. Then I have a bunch of variables starting with en, like en.co and en.cl, and corresponding ticker vectors, then ag variables and so variables, as well). I have all in all 24 zoo variables with 24 corresponding ticker vectors, and for each a corresponding ticker vector, and for each zoo variable I need to create 7 extra columns. That would take much time to do manually, and a lot of code. How would I do this automatically, please? Thank you in advance for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 creation of columns in zoo object
Dear Gabor, Yes, these extra columns are of value as I later write code that fills in those columns row by row conditional on some event taking place, and when there is no event there should be zero in a particular cell. BIG thanks for the quick reply. I will try the code out right away! Kind Regards, Sergey On Tue, Feb 3, 2009 at 15:05, Gabor Grothendieck ggrothendi...@gmail.com wrote: Not sure why you need to have all these extra columns if they are only zero anyways. Could you not append them when you get data for them? Are these placeholders really of any value? At any rate its done using cbind or merge like this: library(zoo) library(chron) nms - sub(.Comdty, tr, me.la.tickers) zeromat - matrix(0, nrow(me.la), length(nms), dimnames = list(NULL, nms)) cbind(me.la, zeromat) In this case zeromat and me.la have the same dimensions so we could alternately reduce it to this slightly briefer code: zeromat - 0 * me.la colnames(zeromat) - sub(.Comdty, tr, me.la.tickers) cbind(me.la, zeromat) On Tue, Feb 3, 2009 at 8:44 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a question. Assume I have the following zoo object: me.la - structure(c(1524.75, 1554.5, 1532.25, 1587.5, 1575.25, 1535.5, 1550, 1493.5, 1492.5, 1472.25, 1457.5, 1442.75, 1399, 1535.75, 1565.25, 1543.5, 1598.5, 1586.5, 1547, 1561.5, 1504.75, 1503.75, 1483.75, 1468.75, 1453.75, 1410, 1546.75, 1575.25, 1554, 1609, 1597.5, 1558.5, 1573, 1516.25, 1515.5, 1495, 1480, 1465, 1421.25, 1561.5, 1590, 1568.75, 1623.5, 1612, 1573, 1587.5, 1530.5, 1530, 1509.75, 1494.5, 1479.5, 1435.75, 1573.5, 1601.5, 1580.25, 1635, 1623.5, 1584.5, 1599, 1541.75, 1541.5, 1521.5, 1506, 1491, 1447.25, 1585.5, 1613, 1591.75, 1646, 1634.5, 1595.5, 1610, 1552.75, 1552.75, 1532.75, 1517, 1502, 1458.25, 1600, 1627.5, 1606.5, 1660, 1649, 1609.75, 1624.25, 1567, 1567, 1547, 1531, 1516, 1472.25, 1612, 1639.5, 1618.25, 1671.5, 1661, 1621.5, 1635.75, 1578, 1578, 1558, 1542, 1527, 1483.5), .Dim = c(13L, 8L), index = structure(c(14245, 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, 14259, 14260, 14263), format = m/d/y, origin = structure(c(1, 1, 1970), .Names = c(month, day, year)), class = c(dates, times)), class = zoo, .Dimnames = list(NULL, c(LA1 COMDTY, LA2 COMDTY, LA3 COMDTY, LA4 COMDTY, LA5 COMDTY, LA6 COMDTY, LA7 COMDTY, LA8 COMDTY))) I also have a following variable (used in RBloomberg) in my environment: me.la.tickers - c(LA1 Comdty, LA2 Comdty, LA3 Comdty, LA4 Comdty, LA5 Comdty, LA6 Comdty, LA7 Comdty, LA8 Comdty) What I need to do is to automate the following manual piece of code: me.la$LA2tr - 0 me.la$LA3tr - 0 me.la$LA4tr - 0 me.la$LA5tr - 0 me.la$LA6tr - 0 me.la$LA7tr - 0 me.la$LA8tr - 0 Basically, I need to automatically create new columns in futures object taking first part of names in me.la.tickers. I tried with paste() and assign() combination, but could not get. I could do the manual part, of course, it does not take much time, but: 1) I want to learn how to automatically create new columns in zoo objects 2) I have many more variables that have to be treated similarly (that is, I have me.lp with corresponding me.lp.tickers, me.qc with me.qc.tickers, etc. Then I have a bunch of variables starting with en, like en.co and en.cl, and corresponding ticker vectors, then ag variables and so variables, as well). I have all in all 24 zoo variables with 24 corresponding ticker vectors, and for each a corresponding ticker vector, and for each zoo variable I need to create 7 extra columns. That would take much time to do manually, and a lot of code. How would I do this automatically, please? Thank you in advance for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 creation of columns in zoo object
It works perfectly! Thank you, Gabor, for the quick solution and for letting me learn sub() function. Supreme! :-) Regards, Sergey On Tue, Feb 3, 2009 at 15:05, Gabor Grothendieck ggrothendi...@gmail.com wrote: Not sure why you need to have all these extra columns if they are only zero anyways. Could you not append them when you get data for them? Are these placeholders really of any value? At any rate its done using cbind or merge like this: library(zoo) library(chron) nms - sub(.Comdty, tr, me.la.tickers) zeromat - matrix(0, nrow(me.la), length(nms), dimnames = list(NULL, nms)) cbind(me.la, zeromat) In this case zeromat and me.la have the same dimensions so we could alternately reduce it to this slightly briefer code: zeromat - 0 * me.la colnames(zeromat) - sub(.Comdty, tr, me.la.tickers) cbind(me.la, zeromat) On Tue, Feb 3, 2009 at 8:44 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I have a question. Assume I have the following zoo object: me.la - structure(c(1524.75, 1554.5, 1532.25, 1587.5, 1575.25, 1535.5, 1550, 1493.5, 1492.5, 1472.25, 1457.5, 1442.75, 1399, 1535.75, 1565.25, 1543.5, 1598.5, 1586.5, 1547, 1561.5, 1504.75, 1503.75, 1483.75, 1468.75, 1453.75, 1410, 1546.75, 1575.25, 1554, 1609, 1597.5, 1558.5, 1573, 1516.25, 1515.5, 1495, 1480, 1465, 1421.25, 1561.5, 1590, 1568.75, 1623.5, 1612, 1573, 1587.5, 1530.5, 1530, 1509.75, 1494.5, 1479.5, 1435.75, 1573.5, 1601.5, 1580.25, 1635, 1623.5, 1584.5, 1599, 1541.75, 1541.5, 1521.5, 1506, 1491, 1447.25, 1585.5, 1613, 1591.75, 1646, 1634.5, 1595.5, 1610, 1552.75, 1552.75, 1532.75, 1517, 1502, 1458.25, 1600, 1627.5, 1606.5, 1660, 1649, 1609.75, 1624.25, 1567, 1567, 1547, 1531, 1516, 1472.25, 1612, 1639.5, 1618.25, 1671.5, 1661, 1621.5, 1635.75, 1578, 1578, 1558, 1542, 1527, 1483.5), .Dim = c(13L, 8L), index = structure(c(14245, 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, 14259, 14260, 14263), format = m/d/y, origin = structure(c(1, 1, 1970), .Names = c(month, day, year)), class = c(dates, times)), class = zoo, .Dimnames = list(NULL, c(LA1 COMDTY, LA2 COMDTY, LA3 COMDTY, LA4 COMDTY, LA5 COMDTY, LA6 COMDTY, LA7 COMDTY, LA8 COMDTY))) I also have a following variable (used in RBloomberg) in my environment: me.la.tickers - c(LA1 Comdty, LA2 Comdty, LA3 Comdty, LA4 Comdty, LA5 Comdty, LA6 Comdty, LA7 Comdty, LA8 Comdty) What I need to do is to automate the following manual piece of code: me.la$LA2tr - 0 me.la$LA3tr - 0 me.la$LA4tr - 0 me.la$LA5tr - 0 me.la$LA6tr - 0 me.la$LA7tr - 0 me.la$LA8tr - 0 Basically, I need to automatically create new columns in futures object taking first part of names in me.la.tickers. I tried with paste() and assign() combination, but could not get. I could do the manual part, of course, it does not take much time, but: 1) I want to learn how to automatically create new columns in zoo objects 2) I have many more variables that have to be treated similarly (that is, I have me.lp with corresponding me.lp.tickers, me.qc with me.qc.tickers, etc. Then I have a bunch of variables starting with en, like en.co and en.cl, and corresponding ticker vectors, then ag variables and so variables, as well). I have all in all 24 zoo variables with 24 corresponding ticker vectors, and for each a corresponding ticker vector, and for each zoo variable I need to create 7 extra columns. That would take much time to do manually, and a lot of code. How would I do this automatically, please? Thank you in advance for your help! Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Looking for a special date function in R
Hello, everyone I wonder if R has something similar to Excel function EDATE(start_date; months) which returns a serial number of the date that is the indicated number of months before of after the start date. Example (the second column EDATE(first_column; -6)): 01.01.1999 01.07.1998 02.02.1999 02.08.1998 06.03.1999 06.09.1998 I am working with a zoo object where the row names are dates and for particular rows I need to find values that were recorded 6 months before (or return NA if the date is before the timeseries start). Maybe someone knows a passable R function for that kind of operation? Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Looking for a special date function in R
Dear Gabor, Thanks for that! Still, it is not really similar to how EDATE works. With julian(Sys.Date(), Sys.Date() - 10) one moves 10 days back. The problem is that I need to move by months, not by days, as months have different number of days. I need to come to the same day when I move backward or forward in time, for example going back one month from today (21.01.2009) I need to come to 21.12.2008. I've read through your article in RNews 4/1 but still do not know how to do what I need to do. Regards, Sergey On Wed, Jan 21, 2009 at 10:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: See ?julian julian(Sys.Date(), Sys.Date() - 10) # 10 [1] 10 attr(,origin) [1] 2009-01-11 and R News 4/1. On Wed, Jan 21, 2009 at 4:37 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I wonder if R has something similar to Excel function EDATE(start_date; months) which returns a serial number of the date that is the indicated number of months before of after the start date. Example (the second column EDATE(first_column; -6)): 01.01.1999 01.07.1998 02.02.1999 02.08.1998 06.03.1999 06.09.1998 I am working with a zoo object where the row names are dates and for particular rows I need to find values that were recorded 6 months before (or return NA if the date is before the timeseries start). Maybe someone knows a passable R function for that kind of operation? Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Looking for a special date function in R
Dear Prof. Ripley, Thank you for help. Yes, that is an interesting question you pose. I already thought myself how February should be handled, as EDATE(31.08.2008; -6) returns 29.02.2008. In Excel it is not a problem, since this nonexisting date is then used in VLOOKUP function where one can have Range.Lookup argument set to TRUE and then it finds the closest value. Example from Excel (value for 29.02.2008 is recovered from the sorted table with VLOOKUP function): 21.07.2008 10.00 31.08.2008 20.00 28.02.2008 30.00 01.03.2008 40.00 02.03.2008 50.00 03.03.2008 60.00 29.02.2008 30 I wonder if that kind of functionality is available in R, or one has to write a piece of code oneself to acheive it. Thank you for your time and help! Regards, Sergey On Wed, Jan 21, 2009 at 12:44, Prof Brian Ripley rip...@stats.ox.ac.uk wrote: On Wed, 21 Jan 2009, Sergey Goriatchev wrote: Dear Gabor, Thanks for that! Still, it is not really similar to how EDATE works. With julian(Sys.Date(), Sys.Date() - 10) one moves 10 days back. The problem is that I need to move by months, not by days, as months have different number of days. I need to come to the same day when I move backward or forward in time, for example going back one month from today (21.01.2009) I need to come to 21.12.2008. I've read through your article in RNews 4/1 but still do not know how to do what I need to do. The trick is to use the POSIXlt class. E.g. x - Sys.Date() xx - as.POSIXlt(x) xx$mon - xx$mon - 6 as.Date(xx) [1] 2008-07-21 Now, the issue is what date is 6 months before 2008-08-31, and I'll leave you to ponder what that means. Regards, Sergey On Wed, Jan 21, 2009 at 10:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: See ?julian julian(Sys.Date(), Sys.Date() - 10) # 10 [1] 10 attr(,origin) [1] 2009-01-11 and R News 4/1. On Wed, Jan 21, 2009 at 4:37 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I wonder if R has something similar to Excel function EDATE(start_date; months) which returns a serial number of the date that is the indicated number of months before of after the start date. Example (the second column EDATE(first_column; -6)): 01.01.1999 01.07.1998 02.02.1999 02.08.1998 06.03.1999 06.09.1998 I am working with a zoo object where the row names are dates and for particular rows I need to find values that were recorded 6 months before (or return NA if the date is before the timeseries start). Maybe someone knows a passable R function for that kind of operation? Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Looking for a special date function in R
Thank you all for your help! Sergey On Wed, Jan 21, 2009 at 13:25, Gabor Grothendieck ggrothendi...@gmail.com wrote: This will give you 6 months after today. Use negative numbers to move backwards: d - Sys.Date() seq(d, length = 2, by = paste(6, months))[2] [1] 2009-07-21 On Wed, Jan 21, 2009 at 6:27 AM, Sergey Goriatchev serg...@gmail.com wrote: Dear Gabor, Thanks for that! Still, it is not really similar to how EDATE works. With julian(Sys.Date(), Sys.Date() - 10) one moves 10 days back. The problem is that I need to move by months, not by days, as months have different number of days. I need to come to the same day when I move backward or forward in time, for example going back one month from today (21.01.2009) I need to come to 21.12.2008. I've read through your article in RNews 4/1 but still do not know how to do what I need to do. Regards, Sergey On Wed, Jan 21, 2009 at 10:44, Gabor Grothendieck ggrothendi...@gmail.com wrote: See ?julian julian(Sys.Date(), Sys.Date() - 10) # 10 [1] 10 attr(,origin) [1] 2009-01-11 and R News 4/1. On Wed, Jan 21, 2009 at 4:37 AM, Sergey Goriatchev serg...@gmail.com wrote: Hello, everyone I wonder if R has something similar to Excel function EDATE(start_date; months) which returns a serial number of the date that is the indicated number of months before of after the start date. Example (the second column EDATE(first_column; -6)): 01.01.1999 01.07.1998 02.02.1999 02.08.1998 06.03.1999 06.09.1998 I am working with a zoo object where the row names are dates and for particular rows I need to find values that were recorded 6 months before (or return NA if the date is before the timeseries start). Maybe someone knows a passable R function for that kind of operation? Thanks in advance for help! Best, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Two similar zoo objects with different structures, how to get same structure?
Dear all, I have a zoo object that has following structure: str(bldata) zoo [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, index)=Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... .. ..- attr(*, format)= chr m/d/y .. ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. .. ..- attr(*, names)= chr [1:3] month day year - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... I also have a vector of dates of same length as zoo object (5219): str(bldates) chr [1:5219] 01/01/90 01/02/90 01/03/90 01/04/90 01/05/90 01/08/90 01/09/90 01/10/90 01/11/90 01/12/90 ... I do the following: attributes(bldata)[[2]] - as.Date(bldates, %m/%d/%y) and get the following: str(bldata) 'zoo' series from 1990-01-01 to 2009-12-31 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Class 'Date' num [1:5219] 7305 7306 7307 7308 7309 ... But what I really want to get is the following: str(bldata) 'zoo' series from 01/01/90 to 12/31/09 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... ..- attr(*, format)= chr m/d/y ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. ..- attr(*, names)= chr [1:3] month day year The array bldata is the same, only the last one is on a machine running RBloomberg, and then I tried to save bldata (and rownames of bldata in bldates) and transfer bldata to the other machine which is not a Bloomberg terminal, and the structure of bldata changes. What should I do in this case to adjust the structure? I guess I have to somehow change the class of Index and set attributes of Index and attribute names of attribute origin of attribute Index? Thank you for your help in advance! Regards, Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Two similar zoo objects with different structures, how to get same structure?
Dear Dr. Grothendieck, First of all, I realized I did not load zoo package before I tried the first str(bldata). If I load zoo and then do str(bldata) I get the following: 'zoo' series from 7305 to 14609 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... ..- attr(*, format)= chr m/d/y ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. ..- attr(*, names)= chr [1:3] month day year Now, I've done what you said and here is the ASCII representation of the data (I don't know if one can attach files here, and anyway I cannot attach files where I am, but the following would reproduce exactly): a - (structure(c(98.585, 98.355, 98.48, 98.585, 98.67, 98.695, 98.81, 98.865, 98.865, 98.865, 98.735, 98.805, 98.805, 97.435, 97.18, 97.165, 97.265, 97.34, 97.415, 97.445, 97.505, 97.525, 97.635, 97.625, 97.53, 97.53, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 2, 2, 2, 2, 2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5), .Dim = c(13L, 4L), index = structure(c(14245, 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, 14259, 14260, 14263), format = m/d/y, origin = structure(c(1, 1, 1970), .Names = c(month, day, year)), class = c(dates, times)), class = zoo, .Dimnames = list(NULL, c(ED4 COMDTY, ED12 COMDTY, FDTR INDEX, UKBRBASE INDEX Copy-paste to R prompt shows following: ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX 14245 98.585 97.435 0.252.0 14246 98.355 97.180 0.252.0 14249 98.480 97.165 0.252.0 14250 98.585 97.265 0.252.0 14251 98.670 97.340 0.252.0 14252 98.695 97.415 0.251.5 14253 98.810 97.445 0.251.5 14256 98.865 97.505 0.251.5 14257 98.865 97.525 0.251.5 14258 98.865 97.635 0.251.5 14259 98.735 97.625 0.251.5 14260 98.805 97.530 0.251.5 14263 98.805 97.530 0.251.5 On my Bloomberg machine, in R console, the rownames look like: ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX 01/01/09 98.585 97.435 0.252.0 01/02/09 98.355 97.180 0.252.0 01/05/09 98.480 97.165 0.252.0 01/06/09 98.585 97.265 0.252.0 01/07/09 98.670 97.340 0.252.0 and that is what I try to get as well. Doing: attributes(a)[[2]] - format(as.Date(attributes(a)[[2]]), %m/%d/%y) changes structure of a to: 'zoo' series from 01/01/09 to 01/19/09 Data: num [1:13, 1:4] 98.6 98.4 98.5 98.6 98.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:4] ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX Index: chr [1:13] 01/01/09 01/02/09 01/05/09 01/06/09 01/07/09 01/08/09 01/09/09 01/12/09 01/13/09 01/14/09 01/15/09 01/16/09 01/19/09 That is not the same structure, attributes disappeared. What am I doing wrong? Thank you in advance for your help. Regards, Sergey On Wed, Jan 21, 2009 at 15:56, Gabor Grothendieck ggrothendi...@gmail.com wrote: Please reduce your examples down to small amounts of data and use dput so that they are reproducible. On Wed, Jan 21, 2009 at 9:32 AM, Sergey Goriatchev serg...@gmail.com wrote: Dear all, I have a zoo object that has following structure: str(bldata) zoo [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, index)=Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... .. ..- attr(*, format)= chr m/d/y .. ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. .. ..- attr(*, names)= chr [1:3] month day year - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... I also have a vector of dates of same length as zoo object (5219): str(bldates) chr [1:5219] 01/01/90 01/02/90 01/03/90 01/04/90 01/05/90 01/08/90 01/09/90 01/10/90 01/11/90 01/12/90 ... I do the following: attributes(bldata)[[2]] - as.Date(bldates, %m/%d/%y) and get the following: str(bldata) 'zoo' series from 1990-01-01 to 2009-12-31 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Class 'Date' num [1:5219] 7305 7306 7307 7308 7309 ... But what I really want to get is the following: str(bldata) 'zoo' series from 01/01/90 to 12/31/09 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307
Re: [R] Two similar zoo objects with different structures, how to get same structure?
Dear Dr. Grothendieck, My purpose in this case it to have the structure of the object on non-Bloomberg machine the same as that of the object on the Bloomberg machine. That way I am sure that whatever code I write away from Bloomberg machine will work on it when I copy the code to it. Also, I am very curious how I could change the structure of an object, in this case a zoo object. Why did the attributes disappear? How can I restore them? Kind Regards, Sergey On Wed, Jan 21, 2009 at 17:01, Gabor Grothendieck ggrothendi...@gmail.com wrote: Is your purpose to change the times in some way? If z is a zoo series you can change the times like this: library(zoo) library(chron) time(z) - ...whatever... e.g. z - zoo(1:3, 11:13) z 11 12 13 1 2 3 time(z) - as.Date(11:13) z 1970-01-12 1970-01-13 1970-01-14 1 2 3 On Wed, Jan 21, 2009 at 10:51 AM, Sergey Goriatchev serg...@gmail.com wrote: Dear Dr. Grothendieck, First of all, I realized I did not load zoo package before I tried the first str(bldata). If I load zoo and then do str(bldata) I get the following: 'zoo' series from 7305 to 14609 Data: num [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:12] ED4 COMDTY ED12 COMDTY ER4 COMDTY ER12 COMDTY ... Index: Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... ..- attr(*, format)= chr m/d/y ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. ..- attr(*, names)= chr [1:3] month day year Now, I've done what you said and here is the ASCII representation of the data (I don't know if one can attach files here, and anyway I cannot attach files where I am, but the following would reproduce exactly): a - (structure(c(98.585, 98.355, 98.48, 98.585, 98.67, 98.695, 98.81, 98.865, 98.865, 98.865, 98.735, 98.805, 98.805, 97.435, 97.18, 97.165, 97.265, 97.34, 97.415, 97.445, 97.505, 97.525, 97.635, 97.625, 97.53, 97.53, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 2, 2, 2, 2, 2, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5), .Dim = c(13L, 4L), index = structure(c(14245, 14246, 14249, 14250, 14251, 14252, 14253, 14256, 14257, 14258, 14259, 14260, 14263), format = m/d/y, origin = structure(c(1, 1, 1970), .Names = c(month, day, year)), class = c(dates, times)), class = zoo, .Dimnames = list(NULL, c(ED4 COMDTY, ED12 COMDTY, FDTR INDEX, UKBRBASE INDEX Copy-paste to R prompt shows following: ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX 14245 98.585 97.435 0.252.0 14246 98.355 97.180 0.252.0 14249 98.480 97.165 0.252.0 14250 98.585 97.265 0.252.0 14251 98.670 97.340 0.252.0 14252 98.695 97.415 0.251.5 14253 98.810 97.445 0.251.5 14256 98.865 97.505 0.251.5 14257 98.865 97.525 0.251.5 14258 98.865 97.635 0.251.5 14259 98.735 97.625 0.251.5 14260 98.805 97.530 0.251.5 14263 98.805 97.530 0.251.5 On my Bloomberg machine, in R console, the rownames look like: ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX 01/01/09 98.585 97.435 0.252.0 01/02/09 98.355 97.180 0.252.0 01/05/09 98.480 97.165 0.252.0 01/06/09 98.585 97.265 0.252.0 01/07/09 98.670 97.340 0.252.0 and that is what I try to get as well. Doing: attributes(a)[[2]] - format(as.Date(attributes(a)[[2]]), %m/%d/%y) changes structure of a to: 'zoo' series from 01/01/09 to 01/19/09 Data: num [1:13, 1:4] 98.6 98.4 98.5 98.6 98.7 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:4] ED4 COMDTY ED12 COMDTY FDTR INDEX UKBRBASE INDEX Index: chr [1:13] 01/01/09 01/02/09 01/05/09 01/06/09 01/07/09 01/08/09 01/09/09 01/12/09 01/13/09 01/14/09 01/15/09 01/16/09 01/19/09 That is not the same structure, attributes disappeared. What am I doing wrong? Thank you in advance for your help. Regards, Sergey On Wed, Jan 21, 2009 at 15:56, Gabor Grothendieck ggrothendi...@gmail.com wrote: Please reduce your examples down to small amounts of data and use dput so that they are reproducible. On Wed, Jan 21, 2009 at 9:32 AM, Sergey Goriatchev serg...@gmail.com wrote: Dear all, I have a zoo object that has following structure: str(bldata) zoo [1:5219, 1:12] 91.9 91.8 91.7 91.8 91.7 ... - attr(*, index)=Classes 'dates', 'times' atomic [1:5219] 7305 7306 7307 7308 7309 ... .. ..- attr(*, format)= chr m/d/y .. ..- attr(*, origin)= Named num [1:3] 1 1 1970 .. .. ..- attr(*, names)= chr [1:3] month day year - attr(*, dimnames)=List of 2
Re: [R] Eclipse and R
I tried with Sys.setenv() in Eclipse environment in the following manner: Sys.getenv(LANGUAGE) LANGUAGE Sys.setenv(LANGUAGE=en) Sys.getenv(LANGUAGE) LANGUAGE en Once I've done this at the beginning of the session, warnings are in English. When I exit Eclipse and then restart, it is back to German and LANGUAGE environment variable is back to . I am running R-2.7.1 on a Vista SP1 machine. I have Eclipse 3.4.0 with StatET 0.6.0 I just tried to put a modified .RProfile file into Eclipse working directory (I got that by typing getwd() at Eclipse prompt) and it loads up (because I have fortunes() specified in .RProfile). In .RProfile I put Sys.setenv(LANGUAGE=en) and it works to the extent that warning messages are now in English, but at startup I still see German text, like that: -- R version 2.7.1 (2008-06-23) Copyright (C) 2008 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R ist freie Software und kommt OHNE JEGLICHE GARANTIE. Sie sind eingeladen, es unter bestimmten Bedingungen weiter zu verbreiten. Tippen Sie 'license()' or 'licence()' für Details dazu. R ist ein Gemeinschaftsprojekt mit vielen Beitragenden. Tippen Sie 'contributors()' für mehr Information und 'citation()', um zu erfahren, wie R oder R packages in Publikationen zitiert werden können. Tippen Sie 'demo()' für einige Demos, 'help()' für on-line Hilfe, oder 'help.start()' für eine HTML Browserschnittstelle zur Hilfe. Tippen Sie 'q()', um R zu verlassen. R is the lingua franca of statistical research. Work in all other languages should be discouraged. -- Jan de Leeuw (as quoted by Matt Pocernich on R-help) JSM 2003, San Francisco (August 2003) Welcome to R! Is there a way to make everything show in English? Thank you all (especially Prof. Ripley) in advance for your help. Sergey On Tue, Aug 12, 2008 at 4:33 PM, Prof Brian Ripley [EMAIL PROTECTED] wrote: Most internationalized programs (including R) respond to the LANGUAGE environent variable: have you tried setting it to en? We would need to know your OS to help more (it looks like it might be Windows or possibly Mac OS: the terms used are not right for either). On Tue, 12 Aug 2008, Sergey Goriatchev wrote: Hello, I am running R in Eclipse, and when I start Eclipse or when I get error messages, they are in German. (My computer's regional language settings are German.) Is there a way to switch to English in Eclipse without changing my global regional language settings? In basic R GUI this is possible in RGUI configuration editor. Anyone can help? Thank you in advance! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] What is filter() function doing?
Hello, I cannot understand what filter() function in package stat is doing. For example, what does filter(1:100, c(1,1,1)) mean? Could someone please explain? Help file is not enough for me. Thanks in advance. Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Again question about filter()
Hello, I thought I understood filter() with the help from Prof. Grothendieck, but I guess I did not. For example, how does this work: filter(1:10, c(0.1, 0.5, 1, 0.5), recursive, init=c(1,2,3,4)) Time Series: Start = 1 End = 10 Frequency = 1 [1] 7.1 6.71000 9.22100 15.87710 21.45821 28.66037 41.08274 55.83522 74.51437 100.78197 If I understand it correctly, the time series, together with initial values, looks like 1,2,3,4,1,2,3,4,5,6,7,8,9,10 The first value is calculated as 1*0.1+2*0.5+3*1+4*0.5+1=7.1, where the first four arguments are initial values times coefficients, and the last argument is the first value of the timeseries, that is 1. But how are the consecutive values calculated? Totally at a loss. Please, help. /Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin Tell me and I forget, teach me and I remember, involve me and I learn. /Benjamin Franklin Luck is where preparation meets opportunity. /George Patten __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 does do.call() work??
Dear members of R forum, Say I have a list: L - list(1:3, 1:3, 1:3) that I want to turn into a matrix. I wonder why if I do: do.call(cbind, L) I get the matrix I want, but if I do cbind(L) I get something different from what I want. Why is that? How does do.call() actually work? I've read in do.call() help file this sentence: The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change. Thanks for help! Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 implementing adapt()
Hello, I am trying to compute rectangle probability of bivariate normal distribution with the following function: bvnrectangle - function(mu, Sig, xmin, xmax, ymin, ymax){ library(adapt) Siginv - solve(Sig) detSig - det(Sig) areal - adapt(ndim=2, lower=c(xmin,ymin), upper=c(xmax,ymax), minpts=100, maxpts=1000, functn=bvnpdf, mu, Siginv, detSig)$value areal } bvnpdf - function(xy, mu, Siginv, detSig){ f-numeric() for(xloop in 1:length(xy[1])){ x - xy[[1]][xloop] if(xy[[1]]xy[[2]] (1==2)) { f[xloop] - 0} else { v - rbind(xy[1], xy[2]) e - t(v-mu) %*% Siginv %*% (v-mu) f[xloop] - exp(-e/2)/(2*pi)/sqrt(detSig) } } f } bvnpdf works correctly (tested), ex.: bvnpdf(c(0,0), c(0,0), solve( matrix(c(1, 0.5, 0.5, 1), nrow=2, byrow=FALSE)), det( matrix(c(1, 0.5, 0.5, 1), nrow=2, byrow=FALSE))) but when I run bvnrectangle, ex.: bvnrectangle(c(0,0), matrix(c(1, 0.5, 0.5, 1), nrow=2, byrow=FALSE), 0, 8, 0, 8) I get the following error message: Error in v - mu : non-conformable arrays Why do I get this error message. I just can't understand what I am doing wrong... Please, help. Thanks in advance, Regards, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 implementing adapt()
that's the bvnpdf() code: bvnpdf - function(xy, mu, Siginv, detSig){ f-numeric() x - xy[[1]] if(xy[[1]]xy[[2]] (1==2)) { f - 0} else { v - rbind(xy[1], xy[2]) e - t(v-mu) %*% Siginv %*% (v-mu) f - as.numeric(exp(-e/2)/(2*pi)/sqrt(detSig)) } f } __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 optimizing Poisson log-likelihood with L-BFGS-B
Dear R users, I have following code, estimating Poisson log-likelihood a number of times: poisson.loglik - function(mu, y){ n - NROW(y) logl - sum(y)*log(mu)-n*mu return(-logl) } estimates - numeric(1e5) for(i in seq_along(estimates)){ estimates[i] - optim(par=1, poisson.loglik, method=L-BFGS-B, lower=0, y=rpois(10,lambda=2))$par } I cannot run this because I always get an error message: Error in optim(par = 1, poisson.loglik, lower = 0, method = L-BFGS-B, : non-finite finite-difference value [1] Could someone enlighten me what that means and why this happens? THanks in advance, Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Strange dataframe behavior
Hello, I have a question regarding the following output: database - read.delim(file=path.input.file, header=TRUE, dec=., sep=\t, na.strings = #NV) str(database) 'data.frame': 314 obs. of 13 variables: $ S : Factor w/ 314 levels 307073,400212,..: 147 72 299 137 162 62 189 236 134 307 ... $ A : Factor w/ 314 levels Alfa,...: 285 258 197 3 81 162 183 272 73 301 ... $ M: Factor w/ 19 levels @NA,A,..: 18 10 11 6 7 12 17 17 11 6 ... $ W : num 0 0 0 0 0 ... $ T : num 0.0467 0.1095 0.0252 0.0821 -0.0275 ... $ C : num 0 0 0 0 0 ... $ MF : num -0.658 0.261 0.922 -1.897 -1.884 ... $ V: num 0.0585 -1.0852 -0.3156 -1.0592 0.2810 ... $ G : num -0.568 -1.302 0.225 -1.473 -0.541 ... $ Mo : num 0.34967 0.42807 -0.41407 -0.18216 -0.00305 ... $ R : num -0.5413 -2. 0.5353 -1.1437 -0.0776 ... $ Tr: num -0.12816 1.04148 0.00647 -0.02424 -1.66834 ... $ Su: num -1.611 1.160 -0.528 -0.091 -1.148 ... which(is.na(database)) [1] 675 704 774 887 So, I have 314 observations, but there are unknown NA observations! I remove one observation (for certain reasons), and remove the corresponding factor level, then: str(database) 'data.frame': 313 obs. of 13 variables: which(is.na(database)) [1] 673 702 772 885 The removal of ONE observation moves NAs by two positions. Maybe someone have an idea what these NA observations mean Thanks in advance for your time and help! Sergey University of Zurich __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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: Side-by-side named barplot bars
Hello, Let me describe what I have and what I want to do: I have two (7x6) matrices (call them A and B) that have same row and column names (rows=species, columns=variables) but contain numerical values that differ (same experiment done twice and values calculated and put in different matrices). Up to now I was producing two set of barplots, each set for each matrix of values. On each page I presented two barplots: For each row of matrix A I plotted the values ofvariables as vertical bars (6 bars), the names under the bars were column names from the matrix, and I also put the value as text at the top of the bar (with text() function). The second barplot was for the same row in B matrix. By putting them on the same page in a pdf document I can look at the bars for each variable and the values texted on the bars and compare the differences between two experiments. But this is not very effective. What I want to do is to combine the two similar barplots for each experiment into one. So, for each species I create just one barplot where I plot 6 clusters each consisting of 2 bars, one bar for same varibale but different experiment. I want to put the values as text at the top of each bar and below each cluster I want to put the name of the variable (they are the same for each experiment, remember). I remembered that Michael Crawley did a side-by-side barplot in his Introduction to R book, I used his idea before but now I have two matrices and his method is not applicable. Could someone please advise me how to do these side-by-side barplots? A little bit of code showing the procedure would be appreciated. Thank you in advance! Best, Sergey G University of Zurich P.S. I do not grasp how to post replies to my own posts (want to thank the people that take their time to help out. Want to thank Gregory Warnes for letting me know of gplots package, now my plots are very informative!). Do I just put in the subject of email the name of my previous post? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 to mode numeric within a matrix
Hello, I create a matrix: best - matrix(0, ncol=2, nrow=num.selected, dimnames=list(the.best$.Name, c(Probability(%), Inside))) best[,1] - as.numeric(the.best$Total*100) best[,2] - ifelse(the.best$Weight==0, No, Yes) What I want is the second column of mode numeric, but it is of mode character, despite the attempt to convert it to numeric with as.numeric(). It must have something to do with the second column of the matrix, where I write No/Yes. What should I do to have the first column of mode numeric? Also, how can I output No/Yes in the second column without citation marks around them? Thanks in advance Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.