Re: [R] How to Calculate the Mean by Multiple Groups in R

2023-10-24 Thread Gabor Grothendieck
A variation is to remove Well and then we can use dot to refer to the remaining columns. aggregate(cbind(OD, ODnorm) ~ . , subset(df, select = - Well), mean) On Tue, Oct 24, 2023 at 8:32 AM Luigi Marongiu wrote: > > Hello, > I have a data frame with different groups (Time, Target, Conc) and

Re: [R] group consecutive dates in a row

2023-08-07 Thread Gabor Grothendieck
It is best to use Date, rather than POSIXct, class if there are no times. Use the cumsum expression shown to group the dates and then summarize each group. We assume that the dates are already sorted in ascending order. library(dplyr) mydf <- data.frame(date = as.Date(c("2012-02-05",

Re: [R] Could not read time series data using read.zoo()

2023-08-03 Thread Gabor Grothendieck
The header has white space in it so skip over it, use header = FALSE and specify the column headers yourself. Also use fill=TRUE since the first row does not have 3 entries. # generate test file cat("Date Adj Close lret 02-01-1997 737.01 03-01-1997 748.03 1.48416235 06-01-1997 747.65

Re: [R] preserve class in apply function

2023-02-08 Thread Gabor Grothendieck
Also try apply(Filter(is.numeric, mydf), 1, sum) On Tue, Feb 7, 2023 at 8:42 AM PIKAL Petr wrote: > > Hi Naresh > > If you wanted to automate the function a bit you can use sapply to find > numeric columns > ind <- sapply(mydf, is.numeric) > > and use it in apply construct > apply(mydf[,ind],

Re: [R] R emulation of FindRoot in Mathematica

2023-01-19 Thread Gabor Grothendieck
If the equations are in the form shown in your post then take the log of both sides, expand the logs and replace log(whatever) with new variables so now the equations are in linear form and are easy to solve. __ R-help@r-project.org mailing list -- To

Re: [R] Function for Distribution Fitting

2022-10-26 Thread Gabor Grothendieck
A Cullen & Frey graph (fitdistrplus::descdist) can be used to compare certain common distributions. On Wed, Oct 26, 2022 at 9:42 AM Paul Bernal wrote: > > Dear friends from the R community, > > Hope you are all doing great. So far, whenever I need to perform > distribution fitting on a

Re: [R] Adding page numbers to existing PDFs

2022-10-21 Thread Gabor Grothendieck
Create a pdf using latex that has only page numbers and then superimpose that with your pdf using the free utility pdftk. The animation R package has an interface to pdftk. Google to locate pdftk and again to locate instructions. There are also freeware GUI Windows programs that are easy to

Re: [R] Unintended behaviour of stats::time not returning integers for the first cycle

2022-10-18 Thread Gabor Grothendieck
The zoo package implements tolerances internally. Converting the ts object to zoo: library(zoo) identical(as.integer(time(as.zoo(x))), true.year) ## [1] TRUE On Sat, Oct 15, 2022 at 3:26 AM Andreï V. Kostyrka wrote: > > Dear all, > > > > I was using stats::time to obtain the year as a

Re: [R] rmoving dates from an xts object...

2022-07-10 Thread Gabor Grothendieck
Look at the examples at the end of ?xts for more info. library(quantmod) getSymbols("AAPL") class(AAPL) ## [1] "xts" "zoo" range(time(AAPL)) ## [1] "2007-01-03" "2022-07-08" # everything up to indicated date a1 <- AAPL["/2018-02-01"] # remove non consecutive dates d <-

Re: [R] Restoration of "rite" package of R as R-script editor or the like

2022-05-24 Thread Gabor Grothendieck
Soren Hojsgaard had also written a tcltk based editor R package and although it is deprecated too it may be simpler than rite in which case it is more likely to work. Contact him (he has other packages on CRAN) and see if he would send it to you and if it fits your needs. On Sun, May 22, 2022 at

Re: [R] Find "undirected" duplicates in a tibble

2021-08-20 Thread Gabor Grothendieck
Since you are dealing with graphs you could consider using the igraph package. This is more involved than needed for what you are asking but it might be useful for other follow on calculations. We first define a 2 column matrix of edges, then convert it to an igraph and simplify it to remove

Re: [R] Testing optimization solvers with equality constraints

2021-05-27 Thread Gabor Grothendieck
In case it is of interest this problem can be solved with an unconstrained optimizer, here optim, like this: proj <- function(x) x / sqrt(sum(x * x)) opt <- optim(c(0, 0, 1), function(x) f(proj(x))) proj(opt$par) ## [1] 5.388907e-09 7.071068e-01 7.071068e-01 On Fri, May 21, 2021

Re: [R] xyplot.zoo trouble with the index when I use microseconds (type POSIXct)

2021-02-22 Thread Gabor Grothendieck
Also, it might be better to simply use seconds as the time. In that case both plot and xyplot run without error. time(DF.w) <- as.POSIXlt(time(DF.w))$sec # now run plot or xyplot as before On Mon, Feb 22, 2021 at 11:56 AM Gabor Grothendieck wrote: > > I assume that this is a lattic

Re: [R] xyplot.zoo trouble with the index when I use microseconds (type POSIXct)

2021-02-22 Thread Gabor Grothendieck
I assume that this is a lattice problem. Replacing xyplot with plot and using all the same arguments there is no error. On Mon, Feb 22, 2021 at 11:26 AM Laurent Rhelp wrote: > > Dear R-Help-List, > > I have to process time series with a sampling frequency of 1 MHz. > I use the POSIXct

Re: [R] Finance & R

2020-12-24 Thread Gabor Grothendieck
I personally use the packages you mention and would describe them as well thought out and relatively bug free through widespread use and years of improvement and maintenance. There are literally dozens of other packages that depend on these packages so if you use them you will also be able to

Re: [R] nls() syntax

2020-12-11 Thread Gabor Grothendieck
The start= argument should be as follows: nls(y ~ x/(x - a[z]),start=list(a = strt),data=xxx) On Fri, Dec 11, 2020 at 6:51 PM Rolf Turner wrote: > > > > I want to fit a model y = x/(x-a) where the value of a depends > on the level of a factor z. I cannot figure out an appropriate > syntax for

Re: [R] formula mungeing

2020-10-23 Thread Gabor Grothendieck
Recursively walk the formula performing the replacement: g <- function(e, ...) { if (length(e) > 1) { if (identical(e[[2]], as.name(names(list(...) { e <- eval(e, list(...)) } if (length(e) > 1) for (i in 1:length(e)) e[[i]] <- Recall(e[[i]], ...) }

Re: [R] Help with read.csv.sql()

2020-07-30 Thread Gabor Grothendieck
Probably simplest to assign the names afterwards as others have suggested but it could be done like this: library(sqldf) write.csv(BOD, "BOD.csv", quote = FALSE, row.names = FALSE) # test data read.csv.sql("BOD.csv", "select Time as Time2, demand as demand2 from file") giving the column

Re: [R] sqldf and number of records affected

2020-06-11 Thread Gabor Grothendieck
seem to work ... > > con <- data.frame(V1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) > sqldf() > sqldf(c("pragma count_changes = 1", "update con set V1 = 0 where V1 > 5 ")) > ans <- sqldf("select * from main.con") > sqldf() > > -Original

Re: [R] sqldf and number of records affected

2020-06-11 Thread Gabor Grothendieck
Here is an example. Ignore the warning or use the workaround discussed here https://github.com/ggrothendieck/sqldf/issues/40 to avoid the warning. library(sqldf) sqldf() # use same connection until next sqldf() sqldf(c("pragma count_changes = 1", "update BOD set demand = 99 where Time >

Re: [R] How to find a split point in a curve?

2020-05-14 Thread Gabor Grothendieck
We can use nls2 to try each value in 10:100 as a possible split point picking the one with lowest residual sum of squares: library(nls2) fm <- nls2(Y ~ cbind(1, pmin(X, X0)), start = data.frame(X0 = 10:100), algorithm = "plinear-brute") plot(Y ~ X) lines(fitted(fm) ~ X, col = "red") > fm

Re: [R] Relatively Simple Maximization Using Optim Doesnt Optimize

2020-03-14 Thread Gabor Grothendieck
It seems CG is having problems with the cube root. This converges while still using CG: S1 <- optim(1001,function(x) (production1(x)^3), method = "CG", control = list(fnscale=-1)) On Thu, Mar 12, 2020 at 9:34 AM Skyler Saleebyan wrote: > > I am trying to familiarize myself with optim() with

Re: [R] Converting irregular time series data into ts object

2020-02-19 Thread Gabor Grothendieck
That is use the 1st 2nd, 3rd, 5th, etc. point in each year from the 305 series. This aligns them by throwing away 305-256=49 points per year in the 305 series so that both series can be set up with a frequency of 256 points per year. On Wed, Feb 19, 2020 at 10:37 AM Gabor Grothendieck wrote

Re: [R] Converting irregular time series data into ts object

2020-02-19 Thread Gabor Grothendieck
Assuming that they both cover the same period of time then if you are willing to throw away some points then consider using only these 256 elements from the 305 series round(seq(1, 305, length = 50)) ## [1] 1 7 13 20 26 32 38 44 ...etc... That is use the 1st ,7th, 13th, etc. point

Re: [R] Strange behaviour of R?

2020-01-20 Thread Gabor Grothendieck
Normally one uses match.fun to avoid such problems. This will give the error shown even if FUN is defined in the global environment. test <- function(FUN, args) { FUN <- match.fun(FUN) print(FUN) FUN(args) } test(NULL, 1:10) ## Error in match.fun(FUN) : 'NULL' is not a

Re: [R] outer join of xts's

2020-01-02 Thread Gabor Grothendieck
oin = "left")) > > The second argument to do.call is a list which becomes the arguments to > the function being called. Your time series should be unnamed entries > in the list, while other arguments to merge() should be named. > > Duncan Murdoch > > > > >

Re: [R] outer join of xts's

2020-01-02 Thread Gabor Grothendieck
t; > join="left" > > parameter to the do.call() command but I could not get the syntax to work > > (assuming it's even possible). > > > > Thanks, > > Eric > > > > > > On Thu, Jan 2, 2020 at 3:23 PM Gabor Grothendieck > > wrote: &

Re: [R] outer join of xts's

2020-01-02 Thread Gabor Grothendieck
orks in this case. > Suppose that instead of the default "outer" join I wanted to use, say, a > "left" join. > Is that possible? I tried a few ways of adding the > join="left" > parameter to the do.call() command but I could not get the syntax to work > (as

Re: [R] outer join of xts's

2020-01-02 Thread Gabor Grothendieck
You don't need Reduce as xts already supports mutliway merges. This perfroms one multiway merge rather than k-1 two way merges. do.call("merge", L) On Thu, Jan 2, 2020 at 6:13 AM Eric Berger wrote: > > Hi, > I have a list L of about 2,600 xts's. > Each xts has a single numeric column.

Re: [R] giving priority to stats package

2019-11-25 Thread Gabor Grothendieck
package. That would give priority for > any functions in the stats package over the newly loaded package (but > also give priority for any other packages earlier on the search path). > > On Sat, Nov 23, 2019 at 6:25 AM Gabor Grothendieck > wrote: > > > > library and re

[R] giving priority to stats package

2019-11-23 Thread Gabor Grothendieck
library and require have new args in 3.6 giving additional control over conflicts. This seems very useful but I was wondering if there were some, preferabley simple, way to give existing loaded packages priority without knowing the actual conflicts in advance. For example library(dplyr,

Re: [R] [r] Applying rollapply in VR test

2019-08-27 Thread Gabor Grothendieck
If I run this (which is identical to your code except I supplied some random input since your post did not include any) I get no error: library(vrtest) library(zoo) set.seed(123) x <- rnorm(100) y <- zoo(x) z <- rollapply(y,50, function(x) AutoBoot.test(x,nboot=30,

Re: [R] Multiple Lags with Dplyr

2019-04-23 Thread Gabor Grothendieck
lag.zoo supports vector-based lags on zoo objects. A few caveats: - dplyr's lag clobbers the base R lag (which you need to invoke lag's methods) so if you have dplyr loaded be sure to refer to stats::lag. - dplyr's lag works backwards relative to the standard set in base R so dplyr::lag(x, 1)

Re: [R] Newbie Question on R versus Matlab/Octave versus C

2019-01-29 Thread Gabor Grothendieck
which is motivated to some degree by Octave but is actually quite different and is particularly suitable in terms of performance for iterative computations where one iteration depends on the prior one. On Mon, Jan 28, 2019 at 6:32 PM Gabor Grothendieck wrote: > > R has many similarities to

Re: [R] [FORGED] Newbie Question on R versus Matlab/Octave versus C

2019-01-28 Thread Gabor Grothendieck
This would be a suitable application for NetLogo. The R package RNetLogo provides an interface. In a few lines of code you get a simulation with graphics. On Mon, Jan 28, 2019 at 7:00 PM Alan Feuerbacher wrote: > > On 1/28/2019 4:20 PM, Rolf Turner wrote: > > > > On 1/29/19 10:05 AM, Alan

Re: [R] Newbie Question on R versus Matlab/Octave versus C

2019-01-28 Thread Gabor Grothendieck
R has many similarities to Octave. Have a look at: https://cran.r-project.org/doc/contrib/R-and-octave.txt https://CRAN.R-project.org/package=matconv On Mon, Jan 28, 2019 at 4:58 PM Alan Feuerbacher wrote: > > Hi, > > I recently learned of the existence of R through a physicist friend who >

Re: [R] R code for if-then-do code blocks

2018-12-17 Thread Gabor Grothendieck
There is some discussion of approaches to this here: https://stackoverflow.com/questions/34096162/dplyr-mutate-replace-on-a-subset-of-rows/34096575#34096575 On Mon, Dec 17, 2018 at 10:30 AM Paul Miller via R-help wrote: > > Hello All, > > Season's greetings! > > Am trying to replicate some

Re: [R] year and week to date - before 1/1 and after 12/31

2018-10-18 Thread Gabor Grothendieck
Replace the week in the date with week 2, say -- a week in which nothing will go wrong and then add or subtract the appropriate number of weeks. d <- c('2016 00 Sun', '2017 53 Sun', '2017 53 Mon') # test data as.Date(sub(" .. ", "02", d), "%Y %U %a") + 7 * (as.numeric(sub(" (..)

Re: [R] looking for formula parser that allows coefficients

2018-08-24 Thread Gabor Grothendieck
The isChar function used in Parse is: isChar <- function(e, ch) identical(e, as.symbol(ch)) On Fri, Aug 24, 2018 at 10:06 PM Gabor Grothendieck wrote: > > Also here is a solution that uses formula processing rather than > string processing. > No packages are used. > >

Re: [R] looking for formula parser that allows coefficients

2018-08-24 Thread Gabor Grothendieck
11:50 AM Paul Johnson wrote: > > Thanks as usual. I owe you more KU decorations soon. > On Wed, Aug 22, 2018 at 2:34 AM Gabor Grothendieck > wrote: > > > > Some string manipulation can convert the formula to a named vector such as > > the one shown at the end of your

Re: [R] looking for formula parser that allows coefficients

2018-08-22 Thread Gabor Grothendieck
Some string manipulation can convert the formula to a named vector such as the one shown at the end of your post. library(gsubfn) # input fo <- y ~ 2 - 1.1 * x1 + x3 - x1:x3 + 0.2 * x2:x2 pat <- "([+-])? *(\\d\\S*)? *\\*? *([[:alpha:]]\\S*)?" ch <- format(fo[[3]]) m <- matrix(strapplyc(ch,

Re: [R] plotmath and logical operators?

2018-08-21 Thread Gabor Grothendieck
Try this: plot(1) tmp <- x >= 3 ~ "&" ~ y <= 3 mtext(tmp) On Mon, Aug 20, 2018 at 5:00 PM MacQueen, Don via R-help wrote: > > I would like to use plotmath to annotate a plot with an expression that > includes a logical operator. > > ## works well > tmp <- expression(x >= 3) > plot(1) >

Re: [R] Combine by columns a vector with another vector that is constant across rows

2018-07-03 Thread Gabor Grothendieck
or this variation if you don't want the first column to be named init: Reduce(cbind2, vec, 1:5) On Tue, Jul 3, 2018 at 10:46 AM, Gabor Grothendieck wrote: > Try Reduce: > > Reduce(cbind, vec, 1:5) > > On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP) > wrote:

Re: [R] Combine by columns a vector with another vector that is constant across rows

2018-07-03 Thread Gabor Grothendieck
Try Reduce: Reduce(cbind, vec, 1:5) On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP) wrote: > Hi All, > > I have one vector that I want to combine with another vector and that other > vector should be the same for every row in the combined matrix. This > obviously does not work: >

Re: [R] Extract function parameters from a R expression

2018-06-20 Thread Gabor Grothendieck
If you specifically want to know which packages were loaded by the script then using a vanilla version of R (i.e. one where only base packages are loaded): vanilla_search <- search() source("myRprg.R") setdiff(search(), vanilla_search) On Wed, Jun 20, 2018 at 4:08 AM, Sigbert Klinke

Re: [R] Take average of previous weeks

2018-03-25 Thread Gabor Grothendieck
There is no `value` column in the `dput` output shown in the question so using `tmin` instead note that the `width=` argument of `rollapply` can be a list containing a vector of offsets (-1 is prior value, -2 is value before that, etc.) and that we can use `rollapplyr` with an `r` on the end to

Re: [R] Equivalent of gtools::mixedsort in R base

2018-03-12 Thread Gabor Grothendieck
split any mixed columns into letter and number columns and then order can be used on that: DF <- data.frame(x = c("a10", "a2", "a1")) o <- do.call("order", transform(DF, let = gsub("\\d", "", x), no = as.numeric(gsub("\\D", "", x)),

Re: [R] Plotting quarterly time series

2018-01-28 Thread Gabor Grothendieck
Using Achim's d this also works to generate z where FUN is a function used to transform the index column and format is also passed to FUN. z <- read.zoo(d, index = "time", FUN = as.yearqtr, format = "Q%q %Y") On Sun, Jan 28, 2018 at 4:53 PM, Achim Zeileis wrote: > On

Re: [R] Portable R in zip file for Windows

2018-01-25 Thread Gabor Grothendieck
I had that in mind, but can't execute the exe due to security > restrictions. > Geez, really, treating people who ask questions this way just makes you > don't want to ask a single one. > > > On Thu, Jan 25, 2018, 11:19 Gabor Grothendieck <ggrothendi...@gmail.com> > wro

Re: [R] Portable R in zip file for Windows

2018-01-25 Thread Gabor Grothendieck
I believe that the ordinary Windows installer for R can produce a portable result by choosing the appropriate configuration options from the offered screens when you run the installer Be sure to enter the desired path in the Select Destination Location screen, choose Yes on the Startup options

Re: [R] run r script in r-fiddle

2017-10-31 Thread Gabor Grothendieck
Try that source statement here -- it is running R 3.4.1: https://www.tutorialspoint.com/execute_r_online.php On Mon, Oct 30, 2017 at 11:14 AM, Suzen, Mehmet wrote: > Note that, looks like r-fiddle runs R 3.1.2. > > __ >

Re: [R] Time series: xts/zoo object at annual (yearly) frequency

2017-10-06 Thread Gabor Grothendieck
Maybe one of these are close enough: xts(c(2, 4, 5), yearqtr(1991:1993)) as.xts(ts(c(2, 4, 5), 1991)) of if you want only a plain year as the index then then use zoo, zooreg or ts class: library(zoo) zoo(c(2, 4, 5), 1991:1993) zooreg(c(2, 4, 5), 1991) ts(c(2, 4, 5), 1991) On

Re: [R] require help

2017-09-21 Thread Gabor Grothendieck
Assuming the input data.frame, DF, is of the form shown reproducibly in the Note below, to convert the series to zoo or ts: library(zoo) # convert to zoo z <- read.zoo(DF) # convert to ts as.ts(z) # Note: DF <- structure(list(year = c(1980, 1981, 1982, 1983, 1984), cnsm = c(174, 175, 175,

Re: [R] symbolic computing example with Ryacas

2017-09-19 Thread Gabor Grothendieck
Here are some more examples: library(Ryacas) x <- Sym("x") yacas("x:=2") Eval(x*x) ## [1] 4 # vignette has similar example y <- Sym("y") Eval(Subst(y*y, y, 3)) ## [1] 9 # demo("Ryacas-Function") has similar example to this f <- function(z) {} body(f) <- yacas(expression(z*z))[[1]] f(4) ## [1]

Re: [R] Convert data into zoo object using Performance analytics package

2017-09-18 Thread Gabor Grothendieck
Depending on how you created df maybe your code has the column names wrong. In any case these 4 alternatives all work. Start a fresh R session and then copy and paste this into it. library(zoo) u <- "https://faculty.washington.edu/ezivot/econ424/sbuxPrices.csv; fmt <- "%m/%d/%Y" # 1 sbux1.z

Re: [R] Case statement in sqldf

2017-09-11 Thread Gabor Grothendieck
2018-03-3 in your code should be 2018-03-31. The line then'201415' needs to be fixed. When posting please provide minimal self-contained examples. There was no input provided and library statements not relevant to the posted code were included. Fixing the invalid date and bad line, getting

Re: [R] Package sqldf in R and dates manipulation

2017-08-11 Thread Gabor Grothendieck
See FAQ #4 on the sqldf github home page. On Fri, Aug 11, 2017 at 9:21 AM, Mangalani Peter Makananisa wrote: > Dear all, > > I recently read the book " R data preperation and manipulation using sqldf > package" by Djoni Darmawikarta > However, I have a problem with

Re: [R] rollapply() produces NAs

2017-05-28 Thread Gabor Grothendieck
Maybe you want this.It computes VaRfun(r[c(i-500, i-1)] for each i for which the argument to r makes sense. rollapply(r, width = list(c(-500, -1)), FUN = VaRfun), On Sat, May 27, 2017 at 5:29 PM, Sepp via R-help wrote: > Hello, > I am fairly new to R and trying to

Re: [R] How to calculate Rolling mean for a List object

2017-05-14 Thread Gabor Grothendieck
Try this code which does not use rollapply: w <- 3 Mean <- function(L) Reduce("+", L) / length(L) lapply(w:length(Data), function(i) Mean(Data[seq(to = i, length = w)])) On Sun, May 14, 2017 at 6:44 PM, Christofer Bogaso wrote: > Hi again, > > I am looking to find a

Re: [R] nlmrt problems - No confInt, NA StdErr, t-, or p-values

2017-03-21 Thread Gabor Grothendieck
You can use wrapnls from the nlmrt package to get an nls object. Run it instead of nlxb. It runs nlxb followed by nls so that the output is an nls object.. Then you can use all of nls' methods. On occiasion that fails even if nlxb succeeds since the nls optimization can fail independently of

Re: [R] Matrix

2017-03-07 Thread Gabor Grothendieck
Assuming that the input is x <- 1:4, try this one-liner: > embed(c(0*x[-1], x, 0*x[-1]), 4) [,1] [,2] [,3] [,4] [1,]1000 [2,]2100 [3,]3210 [4,]4321 [5,]0432 [6,]0043 [7,]0004 On

Re: [R] abline with zoo series

2016-11-24 Thread Gabor Grothendieck
Recessions are typically shown by shading. The zoo package has xblocks for this purpose. If app1 is your zoo object then: plot(app1) tt <- time(app1) xblocks(tt, tt >= "1990-07-01" & tt <= "1991-03-31", col = rgb(0.7, 0.7, 0.7, 0.5)) # transparent grey See ?xblocks for more info. On Thu,

Re: [R] Split strings based on multiple patterns

2016-10-15 Thread Gabor Grothendieck
Replace newlines and colons with a space since they seem to be junk, generate a pattern to replace the attributes with a comma and do the replacement and finally read in what is left into a data frame using the attributes as column names. (I have indented each line of code below by 2 spaces so if

Re: [R] Finding starting values for the parameters using nls() or nls2()

2016-10-09 Thread Gabor Grothendieck
If you are not tied to that model the SSasymp() model in R could be considered and is easy to fit: # to plot points in order o <- order(cl$Area) cl.o <- cl[o, ] fm <- nls(Retention ~ SSasymp(Area, Asym, R0, lrc), cl.o) summary(fm) plot(Retention ~ Area, cl.o)

[R] Fwd: stfrtime function not returning proper results through sqldf package in R

2016-09-16 Thread Gabor Grothendieck
1. Convert the date from R's origin to the origin used by SQLite's strftime function and then be sure you are using the correct SQLite strftime syntax: library(sqldf) sqldf("select strftime('%m', Date + 2440588.5) month from log") 2. Alternately use the H2 backend which actually supports

Re: [R] stfrtime function not returning proper results through sqldf package in R

2016-09-16 Thread Gabor Grothendieck
To be precise it's SQLite that does not have date and time data types. If you use an sqldf backend such as H2 that does have such types then sqldf will pass them as such. In the case of R's "Date" class such objects are passed to SQLite as numbers since that is what SQLite can understand but they

Re: [R] C/C++/Fortran Rolling Window Regressions

2016-07-21 Thread Gabor Grothendieck
I would be careful about making assumptions regarding what is faster. Performance tends to be nonintuitive. When I ran rollapply/lm, rollapply/fastLm and roll_lm on the example you provided rollapply/fastLm was three times faster than roll_lm. Of course this could change with data of different

Re: [R] C/C++/Fortran Rolling Window Regressions

2016-07-21 Thread Gabor Grothendieck
Just replacing lm with a faster version would speed it up. Try lm.fit or even faster is fastLm in the RcppArmadillo package. On Thu, Jul 21, 2016 at 2:02 PM, jeremiah rounds wrote: > Hi, > > A not unusual task is performing a multiple regression in a rolling window >

Re: [R] Concatenate two lists replacing elements with the same name.

2016-07-19 Thread Gabor Grothendieck
Try this: Reduce(modifyList, list(x, y, z)) On Tue, Jul 19, 2016 at 12:34 PM, Luca Cerone wrote: > Dear all, > I would like to know if there is a function to concatenate two lists > while replacing elements with the same name. > > For example: > > x <-

Re: [R] break string at specified possitions

2016-05-17 Thread Gabor Grothendieck
Here are two ways that do not use any packages: s <- paste(letters, collapse = "") # test input substring(s, first, last) ## [1] "abcde" "fghij" "klmnopqrs" read.fwf(textConnection(s), last - first + 1) ## V1V2V3 ## 1 abcde fghij klmnopqrs On Wed, May 11, 2016 at

Re: [R] Clean method to convert date and time between time zones keeping it in POSIXct format

2016-05-09 Thread Gabor Grothendieck
This involves mucking with the internals as well but it is short: structure(T1, tzone = "UTC") On Mon, May 9, 2016 at 9:24 AM, Arnaud Mosnier wrote: > Dear UseRs, > > I know two ways to convert dates and time from on time zone to another but > I am pretty sure that there

Re: [R] Linear Regressions with constraint coefficients

2016-04-28 Thread Gabor Grothendieck
osa Aleksandrovic, FRM, CAIA > Quantitative Analyst - Convertibles > aljosa.aleksandro...@man.com > Tel +41 55 417 76 03 > > Man Investments (CH) AG > Huobstrasse 3 | 8808 Pfäffikon SZ | Switzerland > > -Original Message- > From: Gabor Grothendieck [mailto:ggrothendi...@gm

Re: [R] Approximate taylor series

2016-04-27 Thread Gabor Grothendieck
Regress on a multivariate polynomial: lm(y ~ polym(x1, x2, x3, x4, degree = 3)) See ?polym __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide

Re: [R] Linear Regressions with constraint coefficients

2016-04-26 Thread Gabor Grothendieck
This is a quadratic programming problem that you can solve using either a quadratic programming solver with constraints or a general nonlinear solver with constraints. See https://cran.r-project.org/web/views/Optimization.html for more info on what is available. Here is an example using a

Re: [R] Functional programming?

2016-03-02 Thread Gabor Grothendieck
This manufactures the functions without using eval by using substitute to substitute i-1 and a[i] into an expression for the body which is then assigned to the body of the function: hh <- vector("list", 5) hh[[1]] <- f(a[1]) for(i in 2:5) { hh[[i]] <- hh[[1]] body(hh[[i]]) <-

Re: [R] sqldf --Warning message:

2016-02-19 Thread Gabor Grothendieck
sqldf does not use Tk so you can ignore this. On Fri, Feb 19, 2016 at 12:32 PM, Divakar Reddy wrote: > Dear R users, > > I'm getting Waring message while trying to load "sqldf" package in R3.2.3 > and assuming that we can ignore this as it's WARNING Message and not an >

Re: [R] Create macro_var in R

2016-02-03 Thread Gabor Grothendieck
See Example 5. Insert Variables on the sqldf home page. https://github.com/ggrothendieck/sqldf On Wed, Feb 3, 2016 at 2:16 PM, Amoy Yang via R-help wrote: > First, MVAR<-c("population) should be the same as "population'". Correct? > You use tab[[MVAR]] to refer to

Re: [R] fancy linear model and grouping

2016-02-02 Thread Gabor Grothendieck
Try the mclust package: library(mclust) temp.na <- na.omit(temp) fm <- Mclust(temp.na) g <- fm$classification plot(temp.na, pch = g, col = g) On Tue, Feb 2, 2016 at 6:35 AM, PIKAL Petr wrote: > Dear all > > I have data like this > >> dput(temp) > > temp <-

Re: [R] fancy linear model and grouping

2016-02-02 Thread Gabor Grothendieck
signed for slightly different > problem. > > Here is the result with whole data. > > fm <- Mclust(temp) > g <- fm$classification > plot(1/temp[,1], temp[,2], pch = g, col = g) > > I will go through the docs more thoroughly, to be 100% sure I did not miss >

Re: [R] Optim() and Instability

2015-11-16 Thread Gabor Grothendieck
] 0.6305 -0.1247 -0.0032 $ value : num 473 <= $ counts : Named int [1:2] 182 NA ..- attr(*, "names")= chr [1:2] "function" "gradient" $ convergence: int 0 $ message: NULL On Sat, Nov 14, 2015 at 10:32 AM, Gabor Grothendieck <ggrothendi...@g

Re: [R] Optim() and Instability

2015-11-14 Thread Gabor Grothendieck
Tyipcally the parameters being optimized should be the same order of magnitude or else you can expect numerical problems. That is what the fnscale control parameter is for. On Sat, Nov 14, 2015 at 10:15 AM, Lorenzo Isella wrote: > Dear All, > I am using optim() for a

Re: [R] Optim() and Instability

2015-11-14 Thread Gabor Grothendieck
I meant the parscale parameter. On Sat, Nov 14, 2015 at 10:30 AM, Gabor Grothendieck <ggrothendi...@gmail.com> wrote: > Tyipcally the parameters being optimized should be the same order of > magnitude or else you can expect numerical problems. That is what the > fnscale co

Re: [R] Linear regression with a rounded response variable

2015-10-21 Thread Gabor Grothendieck
This could be modeled directly using Bayesian techniques. Consider the Bayesian version of the following model where we only observe y and X. y0 is not observed. y0 <- X b + error y <- round(y0) The following code is based on modifying the code in the README of the CRAN rcppbugs R

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Correction. Yes, it's the projection of S onto the subspace orthogonal to B which is: X <- S - (B%o%B) %*% S/ sum(B*B) and is also implied by Duncan's solution since that is what the residuals of linear regression are. On Tue, Oct 20, 2015 at 1:11 PM, Gabor Grothendieck <ggro

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Yes, it's the projection of S onto the subspace orthogonal to B which is: X <- S - B%*%B / sum(B*B) and is also implied by Duncan's solution since that is what the residuals of linear regression are. On Tue, Oct 20, 2015 at 1:00 PM, Paul Smith wrote: > On Tue, Oct 20, 2015

Re: [R] flatten a list

2015-09-29 Thread Gabor Grothendieck
> do.call(c, lapply(temp, function(x) if (is.list(x)) x else list(x))) [[1]] [1] 1 2 3 [[2]] [1] "a" "b" "c" $duh [1] 5 6 7 8 $zed [1] 15 16 17 On Tue, Sep 29, 2015 at 11:00 AM, Therneau, Terry M., Ph.D. < thern...@mayo.edu> wrote: > I'd like to flatten a list from 2 levels to 1 level. This

Re: [R] How to coerce a parameter in nls?

2015-09-22 Thread Gabor Grothendieck
You may have to do without masking and switch back to nls. dproot2 and fo are from prior post. # to mask Rm6 omit it from start and set it explicitly st <- c(Rm1=1.01, Rm2=1.01, Rm3=1.01, Rm4=6.65, Rm5=1.01, d50=20, c=-1) Rm6 <- 1 fm.nls <- nls(fo, dproot2, start = st) AIC(fm.nls)

Re: [R] How to coerce a parameter in nls?

2015-09-22 Thread Gabor Grothendieck
ng with my function. I tried some > times but am not sure how to improve it because I am quite new to R. > > Could anyone please give me some suggestion. > > Thanks a lot! > > > Jianling > > > On 22 September 2015 at 00:43, Gabor Grothendieck > <ggrothendi...@

Re: [R] How to coerce a parameter in nls?

2015-09-22 Thread Gabor Grothendieck
1.01, Rm2=1.01, Rm3=1.01, Rm4=6.65, Rm5=1.01, Rm6=1, d50=20, c=-1)) On Tue, Sep 22, 2015 at 7:04 AM, Gabor Grothendieck <ggrothendi...@gmail.com > wrote: > Just write out the 20 terms. > > On Mon, Sep 21, 2015 at 10:26 PM, Jianling Fan <fanjianl...@gmail.com> > wrote: > >>

Re: [R] How to coerce a parameter in nls?

2015-09-21 Thread Gabor Grothendieck
Express the formula in terms of simple operations like this: # add 0/1 columns ref.1, ref.2, ..., ref.6 dproot2 <- do.call(data.frame, transform(dproot, ref = outer(dproot$ref, seq(6), "==") + 0)) # now express the formula in terms of the new columns library(nlmrt) fitdp1<-nlxb(den ~ (Rm1 *

Re: [R] How to read CSV from web?

2015-09-14 Thread Gabor Grothendieck
Your read.csv call works for me under Windows on "R version 3.2.2 Patched (2015-08-25 r69180)" but not on "R version 3.1.3 Patched (2015-03-16 r68169)". Suggest you upgrade your R installation and try again. If you are on Windowsw and don't want to upgrade right now an alternative is to issue

Re: [R] extracting every nth character from a string...

2015-09-06 Thread Gabor Grothendieck
This uses a regular expression but is shorter: > gsub("(.).", "\\1", "ABCDEFG") [1] "ACEG" It replaces each successive pair of characters with the first of that pair. If there is an odd number of characters then the last character is not matched and therefore kept -- thus it works properly for

Re: [R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R

2015-07-04 Thread Gabor Grothendieck
You can do that with bc if you pass the entire expression to bc(...) in quotes but in that case you will have to use bc notation, not R notation so, for example, exp is e and atan is a. library(bc) bc(e(sqrt(163)*4*a(1))) [1]

Re: [R] Spline Graphs

2015-06-29 Thread Gabor Grothendieck
Sorry if this appears twice but I am not sure the first attempt got through. This is not much to go on but here is a short self contained example which creates a longitudinal data frame L in long form from the built in data frame BOD and then plots it as points and splines using lattice: L -

Re: [R] help for lay person assisting R user with disability

2015-06-18 Thread Gabor Grothendieck
On Thu, Jun 18, 2015 at 10:32 AM, Courtney Bryant cbry...@andrew.cmu.edu wrote: Good Morning, I am currently working with a disabled R user who is a student here at CMU. The student has both sight and mobility issues. The student has asked for an assistant who is well versed in R to enter

Re: [R] Toronto CRAN mirror 403 error?

2015-05-29 Thread Gabor Grothendieck
On Fri, May 29, 2015 at 10:12 PM, Mark Drummond m...@markdrummond.ca wrote: I've been getting a 403 when I try pulling from the Toronto CRAN mirror today. http://cran.utstat.utoronto.ca/ Is there a contact list for mirror managers? See the cran_mirrors.csv file in R.home(doc) of your R

Re: [R] Extract month and year as one column

2015-03-13 Thread Gabor Grothendieck
On Fri, Mar 13, 2015 at 11:36 AM, Kumsaa waddee...@gmail.com wrote: How could I extract both month and year from a date? I know how to separately extract both using lubridate package: df$month - month(df$date) df$year- year(df$date) I wish to extract year and month as one column

Re: [R] Substring replacement in string

2015-02-28 Thread Gabor Grothendieck
On Fri, Feb 27, 2015 at 5:19 PM, Alrik Thiem alrik.th...@gmail.com wrote: I would like to replace all lower-case letters in a string that are not part of certain fixed expressions. For example, I have the string: pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1)) Where I would like to

Re: [R] Substring replacement in string

2015-02-28 Thread Gabor Grothendieck
there're no integers following the components in the pmin/pmax functions. Could this be generalized to handle both cases? Best wishes, Alrik -Ursprüngliche Nachricht- Von: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Gesendet: Samstag, 28. Februar 2015 13:35 An: Alrik Thiem Cc

Re: [R] sqldf() difference between R 3.1.2 and 3.0.1

2015-02-11 Thread Gabor Grothendieck
On Wed, Feb 11, 2015 at 9:45 AM, Doran, Harold hdo...@air.org wrote: I have a function written and tested using R 3.0.1 and sqldf_0.4-7.1 that works perfectly. However, using this same code with R 3.1.2 and sqldf_0.4-10 yields the error below that I am having a difficult time deciphering.

  1   2   3   4   5   6   7   8   9   10   >