Re: [R] passing a modified argument to an S3 method

2024-04-20 Thread Berry, Charles via R-help


> On Apr 19, 2024, at 1:36 PM, CRAN.r via R-help  wrote:
> 
> Is there a way to pass a modified argument from an S3 generic to a method?  
> Here's a non-working example that I want to return "abcd".
> 

grep-ing the source code is often a good way to answer a question like this.

Particularly, with NextMethod there seem to be plenty of instances in which the 
original arguments are modified before a call to a method.

One such is `t.data.frame` and there seem to be other methods for "data.frame" 
objects that modify the objects before calling NextMethod.

HTH,

Chuck


>  test <- function(x, y = NULL){
>y <- "abcd"
>UseMethod("test")
>  }
>  test.default <- function(x, y = NULL) y
>  test(x = 3)
> 
> Is that possible? I've looked around a lot, but can't find any examples or 
> discussion.
> 
> Jay
> 
> From nobody Sat Apr 20 10:07:59 2024
> Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
> From: "CRAN.r via R-help" 
> Newsgroups: gmane.comp.lang.r.general
> Subject: passing a modified argument to an S3 method
> Date: Fri, 19 Apr 2024 20:36:22 +
> Approved: n...@gmane.org
> Message-ID: 
> <4pRzEDv__NGDIT68Q6LBK0OcyD9iQs3g6SkZemots3x4noMUQIV8UAfjDEe2UOq6-D4FF6z0T-76bUG0UxqFccqyFKVKw31OB_Iyh0kbzFs=@proton.me>
> Reply-To: "CRAN.r" 
> Mime-Version: 1.0
> Content-Type: text/plain; charset="us-ascii"
> Content-Transfer-Encoding: 7bit
> Injection-Info: ciao.gmane.io; 
> posting-host="blaine.gmane.org:116.202.254.214";
> logging-data="8351"; mail-complaints-to="use...@ciao.gmane.io"
> To: "r-help@r-project.org" 
> Original-X-From: r-help-boun...@r-project.org Sat Apr 20 13:23:27 2024
> Return-path: 
> Envelope-to: gclrg-r-h...@m.gmane-mx.org
> Original-Received: from hypatia.math.ethz.ch ([129.132.119.208])
> by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
> (Exim 4.92)
> (envelope-from )
> id 1ry8oY-0001sq-Bn
> for gclrg-r-h...@m.gmane-mx.org; Sat, 20 Apr 2024 13:23:26 +0200
> Original-Received: from localhost (localhost [127.0.0.1])
> by hypatia.math.ethz.ch (Postfix) with ESMTP id 439F319B4;
> Sat, 20 Apr 2024 13:23:25 +0200 (CEST)
> Authentication-Results: hypatia.math.ethz.ch (amavisd-new);
> dkim=pass (1024-bit key) reason="pass (just generated, assumed good)"
> header.d=r-project.org
> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=r-project.org; h=
> content-transfer-encoding:content-type:content-type:reply-to
> :from:from:list-subscribe:list-help:list-post:list-archive
> :list-unsubscribe:list-id:precedence:subject:subject
> :mime-version:message-id:date:date:received:received:received
> :received:received:received; s=dkim20211220; t=1713612201; x=
> 1716204202; bh=Z6v15ZPk1rn+11Ef9rNBtKr6kCXFKaj/SGL/HXarBWc=; b=H
> O9aWKsrJKBwRK33ySDQjcif0WL28cjIvg6O3iIC0c1aB+fihnMC/BTLjCyUGIorY
> Yx7Um4sp4+sw0nnmvWWBKL1e1YVr5+QfUCkIyDU6NawYEeC8m3VjEQa7KMALgJN5
> awIR6NsAbCK1ewPN2QPNWMxpIftLzbaQOWMWPCqOPU=
> X-Virus-Scanned: amavisd-new at r-project.org
> Original-Received: from hypatia.math.ethz.ch ([127.0.0.1])
> by localhost (hypatia.math.ethz.ch [127.0.0.1]) (amavisd-new, port 10024)
> with LMTP id O5euluMcNO0X; Sat, 20 Apr 2024 13:23:21 +0200 (CEST)
> Original-Received: from hypatia.math.ethz.ch (localhost [127.0.0.1])
> by hypatia.math.ethz.ch (Postfix) with ESMTP id 8C1D44D3;
> Sat, 20 Apr 2024 13:23:21 +0200 (CEST)
> Original-Received: from localhost (localhost [127.0.0.1])
> by hypatia.math.ethz.ch (Postfix) with ESMTP id 1378F1C23
> for ; Fri, 19 Apr 2024 22:36:39 +0200 (CEST)
> X-Virus-Scanned: amavisd-new at r-project.org
> Original-Received: from hypatia.math.ethz.ch ([127.0.0.1])
> by localhost (hypatia.math.ethz.ch [127.0.0.1]) (amavisd-new, port 10024)
> with LMTP id y-CERIWOjFBy for ;
> Fri, 19 Apr 2024 22:36:34 +0200 (CEST)
> Original-Received: from ethz.ch (mc3.ethz.ch [129.132.198.195])
> (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
> (No client certificate requested)
> by hypatia.math.ethz.ch (Postfix) with ESMTPS id 87F5E1B8C
> for ; Fri, 19 Apr 2024 22:36:34 +0200 (CEST)
> Original-Received: from mail-4322.protonmail.ch ([185.70.43.22])
> by ethz.ch stage1 with esmtps (Exim MailCleaner) 
> id 1rxv0v-000253-1n for  
> from ; Fri, 19 Apr 2024 22:39:17 +0200
> X-MailCleaner-SPF: pass
> Feedback-ID: 10121:user:proton
> X-Pm-Message-ID: 1d72f1b23cf9e2d0142aaa1edb9ef8ebccac926b
> X-Newsl: is not newsletter (0.0/5.0) position : 0, not decisive
> X-NiceBayes: is not spam (52.38%) position : 3, not decisive
> X-Spamc: is not spam (0.0/5.0) position : 7, ham decisive
> X-MailCleaner-Information: Please contact serviced...@id.ethz.ch for more
> information
> X-MailCleaner-ID: 1rxv0v-000257-24
> X-MailCleaner: Found to be clean
> X-MailCleaner-SpamCheck: not spam, Newsl (score=0.0, required=5.0, NONE,
> position : 0, not decisive), NiceBayes (52.38%, position : 3,
> not decisive), Spamc (score=0.0, required=5.0,
> RCVD_IN_MSPIKE_H4 -0.0, DKIM_VALID -0.3, RCVD_IN_MSPIKE_WL -0.0,
> MC_SPF_PASS -0.0, BOTNET_IPINHOSTNAME 1.0,
> 

[R] fortune nomination WAS: Re: How long does it take to learn the R programming language?

2022-09-28 Thread Berry, Charles
Aha!
CCB

> On Sep 27, 2022, at 6:08 PM, Rolf Turner  wrote:
> 
> 
> On Mon, 26 Sep 2022 11:14:57 +0800
> Turritopsis Dohrnii Teo En Ming  wrote:
> 
>> Subject: How long does it take to learn the R programming language?
>> 
>> Good day from Singapore,
>> 
>> How long does it take to learn the R programming language?
> 
> How long is a piece of string? :-)
> 
> cheers,
> 
> Rolf Turner
> 
> -- 
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [ESS] emacs-ess and the dreaded dotemacs file

2021-10-20 Thread Berry, Charles via ESS-help
Hi Mark,

I hope you are well.

As Tyler points out the attachment didn't make it.

However, I do see this bit:

 Reading at buffer position 432
 load-with-code-conversion("/home/markleeds/.emacs "/home/markleeds/.emacs" t t)
 load("~/.emacs" t t)

If the latter two lines appear literally in a file you are trying to load, then 
you have fallen into the trap of using R like syntax where lisp is needed. (I 
do know emacs-lisp and do this myself more than I want to admit.) 

So move the open parens BEFORE the function names, viz.

(load-with-code-conversion "/home/markleeds/.emacs
"/home/markleeds/.emacs" t t )
 (load "~/.emacs" t t)


HTH,
Chuck


> On Oct 19, 2021, at 2:03 AM, Mark Leeds via ESS-help  
> wrote:
> 
> Hi All: Would anyone mind doing me a big favor and fixing any ( even one is
> great )  problems in my current dotemacs file. I was able to get some of it
> to run but when it hits the auto-complete section, I start having all kinds
> of problems. For most of it, ( from the auto-complete section and on ),  I
> just copied the dotemacs file used at the link below ( the www.code.rd.com
> link ) . But things either changed dramatically since that link was created
> or the author is maybe not showing the whole file ?
> 
> I'm lisp illiterate which obviously doesn't help. I use ubuntu but I doubt
> that matters.
> 
> This all sounded like a good idea when I started it  but now its  been
> three days and I'm still not terribly far. Thanks to Rich Heiberger who was
> influential in getting the top part to work before it hits the
> auto-complete section. The dotemacsfile is
> attached. Note that the code in the dotemacsfile with all the semi-colons
> is the code from the link here. I don't see any typos but I could be
> mistaken.  Or
> maybe the link itself has typos ? Thanks so much for help in any way.
> 
> https://www.codetd.com/en/article/8181443
> 
> #===
> # ERROR THAT emacs --debug-init temp.R GIVES BUT I DOUBT THAT'S  THE ONLY
> ONE
> #===
> Debugger entered--Lisp error: (invalid-read-syntax ")")
>  eval-buffer(# nil "/home/markleeds/.emacs" nil t)  ;
> Reading at buffer position 432
>  load-with-code-conversion("/home/markleeds/.emacs"
> "/home/markleeds/.emacs" t t)
>  load("~/.emacs" t t)
>  #[0 " \205\262
> __
> ESS-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help
> 

__
ESS-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help


Re: [R] Cacheing of functions from libraries other than the base in Rmarkdown

2021-09-19 Thread Berry, Charles
Chris,


> On Sep 18, 2021, at 12:26 PM, Chris Evans  wrote:
> 
> This question may belong somewhere else, if so, please signpost me and accept 
> apologies.
> 
> What is happening is that I have a large (for me, > 3k lines) Rmarkdown file 
> with many R code blocks (no other code or 
> engine is used) working on some large datasets.  I have some inline r like 
> 
>   There are `r n_distinct(tibDat$ID)` participants and `r nrow(tibDat)` rows 
> of data.
> 
> What I am finding is that even if one knit has worked fine and I change 
> something somewhere and knit again, the second
> knit is often failing with an error like
> 
>   n_distinct(tibDat$ID) : could not find function "n_distinct"
> 
> This is not happening for functions like nrow() from base R and it mostly 
> seems to happen to functions from the tidyverse.
> 
> I think what is happening is some sort of cache corruption presumably caused 
> by the memory demands.  I am pretty sure I've
> seen this before but a long time ago and dealt with it by deleting the files 
> and cache folders created by the knit. 

Caching things that depend on libraries is known to be tricky.

Specifically, it is advised that "loading packages via library() in a cached 
chunk and these packages will be used by uncached chunks" is something you 
should not do.  I suspect that this is the problem with your inline chunk.

I have to reread things like:

https://yihui.org/knitr/demo/cache/

and relevant parts of the manual to be sure I didn't mess something up and 
maybe you should look at that and the manual yet another time. 

HTH,

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] concatenating columns in data.frame

2021-07-01 Thread Berry, Charles



> On Jul 1, 2021, at 11:24 AM, Bert Gunter  wrote:
> 
> Why not simply:
> 
> ## reprex
> set.seed(123)
> df = data.frame("A"=sample(letters, 10), "B"=sample(letters, 10),
>"C"=sample(letters,10), "D"=sample(letters, 10))
> df
> use_columns = c("D", "B")
> 
> ## one liner
> df$combo_col <- do.call(paste,c(df[,use_columns], sep = "_"))
> df
> 
> In case you are wondering, this works because by definition *a date
> frame **is** a list*, so the concatenation is list concatenation.
> 

Why not? 

Because I erroneously thought that there is a "data.frame' method for `c` and 
that this would cause a problem.

But I was wrong, so your solution wins.

Best,
Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] concatenating columns in data.frame

2021-07-01 Thread Berry, Charles



> On Jul 1, 2021, at 7:36 AM, Micha Silver  wrote:
> 
> I need to create a new data.frame column as a concatenation of existing 
> character columns. But the number and name of the columns to concatenate 
> needs to be passed in dynamically. The code below does what I want, but seems 
> very clumsy. Any suggestions how to improve?
> 
> 
> df = data.frame("A"=sample(letters, 10), "B"=sample(letters, 10), 
> "C"=sample(letters,10), "D"=sample(letters, 10))
> 
> # Which columns to concat:
> 
> use_columns = c("D", "B")
> 
> 
> UpdateCombo = function(df, use_columns) {
> use_df = df[, use_columns]
> combo_list = lapply(1:nrow(use_df), function(r) {
> r_combo = paste(use_df[r,], collapse="_")
> return(data.frame("Combo" = r_combo))
> })
> combo = do.call(rbind, combo_list)
> 
> names(combo) = "Combo"
> 
> return(combo)
> 
> }
> 
> 
> combo_col = UpdateCombo(df, use_columns)
> 
> df_combo = do.call(cbind, list(df, combo_col))
> 
> 


I'd do this:


UpdateCombo <-
  function(df, use_columns){
pasteUnder <- function(...) paste(..., sep="_")
do.call(pasteUnder, df[, use_columns])
  }

df_combo <- cbind(df, Combo=UpdateCombo(df, use_columns))

HTH,
Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] if statement and for loop question

2021-05-30 Thread Berry, Charles
Kai,

You have made a simple mistake. And now you cannot see it.  I believe this is 
not uncommon among programmers.  It has happened to me more times than I want 
to recall.

> On May 30, 2021, at 9:28 AM, Kai Yang via R-help  wrote:
> 
> Hello List,I have a data frame which having the character columns:
> 
> | a1 | b1 | c1 | d1 |
> | a2 | b2 | c2 | d2 |
> | a3 | b3 | c3 | d3 |
> | a4 | b4 | c4 | d4 |
> | a5 | b5 | c5 | d5 |
> 
> 
> 
> I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 
> and d1 will be zero out. And do the same thing for the a2 to a5 series.
> I write the code below to do this. But it doesn't work. Would you please 
> correct my code?
> Thank you,
> Kai
> 
> 
> for (i in 1:5) 
> {
>   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>   {
> try$b[i]== ''

What is the above line intended to do?

I think you need to read the line out loud stating the effect of each operator.

If you still do not see what gives, copy and paste each line to the R prompt 
after the loop has run (i.e. with `i' having value 5) paying attention to any 
results that are printed out.

> try$c[i] == ''
> try$d[i]== ''
>   }
> }
> 
> 
>   [[alternative HTML version deleted]]
> 

HTH,

Chuck

p.s. It is highly recommended to use `<-' as the assignment operator.

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dependent nested for loops in R

2021-01-31 Thread Berry, Charles



> On Jan 30, 2021, at 9:32 PM, Shaami  wrote:
> 
> Hi
> I have made the sample code again. Could you please guide how to use
> vectorization for variables whose next value depends on the previous one?
> 


Glad to help.

First, it could help you to trace your code.  I suspect that the results are 
not at all what you want and tracing would help you see that.

I suggest running this revision and printing out x, z, and w.

#+begin_src R
  w = NULL
  for(j in 1:2)
  {
z = NULL
x = rnorm(10)
z[1] = x[1]
for(i in 2:10)
{
  z[i] = x[i]+5*z[i-1]
  if(z[i]>4 | z[i]<1) {
w[j]=i
  } else {
w[j] = 0
  }
}
  }
#+end_src


You should be able to see that the value of w can easily be obtained outside of 
the `i' loop.

-- 

If inspecting those results did not make you go back and rethink your approach, 
try writing the expression for 

z[n] = x[n] + ... + k * x[1]

If you are not good with algebra write out each of z[1], z[2], z[3] and z[4] in 
terms of x[1:4] and then look at what you have.

Perhaps the value of `k' will surprise you.

In any case, if the code is truly what you intended, you only need x[1:25] to 
get z[n] to double precision for any n.  So, 

Also, you will hardly ever be able to compute the value of z[n] for n > 450. 

If these last two statements are puzzling, see 
https://en.wikipedia.org/wiki/Floating-point_arithmetic

HTH,
Chuck

> w = NULL
> 
> for(j in 1:1000)
> 
> {
> 
>  z = NULL
> 
>  x = rnorm(2000)
> 
>  z[1] = x[1]
> 
>  for(i in 2:2000)
> 
>  {
> 
>z[i] = x[i]+5*z[i-1]
> 
>if(z[i]>4 | z[i]<1) {
> 
>  w[j]=i
> 
>} else {
> 
>  w[j] = 0
> 
>}
> 
>  }
> 
> }
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] text on curve

2020-09-22 Thread Berry, Charles



> On Sep 22, 2020, at 1:10 AM, Jinsong Zhao  wrote:
> 
> Hi there,
> 
> I write a simple function that could place text along a curve. Since I am not 
> familiar with the operation of rotating graphical elements, e.g., text, 
> rectangle, etc., I hope you could give suggestions or hints on how to improve 
> it. Thanks in advance.
> 
> # Here is the code:
> 


[code deleted]

For this kind of operation you might want to use tikz. 

R has the ability to produce tikz directives and to insert raw tikz into a 
'tikzDevice'.

If you search rseek.org for 'tikz' you will get plenty of good hits. 

The tikz/pgf manual has examples of flowing text, IIRC.

HTH,

Chuck

p.s. this is a plain text list. Do not submit html.
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Nonlinear logistic regression fitting

2020-07-28 Thread Berry, Charles
Did you try searching for "Michaelis" on rseek.org?

It seems like there are many hits that might be pertinent to your query. 

If none is pertinent, maybe saying  why they are not sufficient will help 
others see how their "expert opinions" can help you.

HTH,

Chuck

> On Jul 28, 2020, at 8:19 AM, Sebastien Bihorel via R-help 
>  wrote:
> 
> Thank you for your subtle input, Bert... as usual!
> 
> This is literally the search I conducted and spent 2 hours on before posting 
> to R-help. I was asking for expert opinions, not for search engine FAQ!
> 
> Thank anyways
> 
> 
> From: Bert Gunter 
> Sent: Tuesday, July 28, 2020 11:12
> To: Sebastien Bihorel 
> Cc: r-help@r-project.org 
> Subject: Re: [R] Nonlinear logistic regression fitting
> 
> Search!
> ... for "nonlinear logistic regression" at rseek.org.
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and 
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Tue, Jul 28, 2020 at 7:25 AM Sebastien Bihorel via R-help 
> mailto:r-help@r-project.org>> wrote:
> Hi
> 
> I need to fit a logistic regression model using a saturable Michaelis-Menten 
> function of my predictor x. The likelihood could be expressed as:
> 
> L = intercept + emax * x / (EC50+x)
> 
> Which I guess could be expressed as the following R model
> 
> ~ emax*x/(ec50+x)
> 
> As far as I know (please, correct me if I am wrong), fitting such a model is 
> to not doable with glm, since the function is not linear.
> 
> A Stackoverflow post recommends the bnlr function from the gnlm 
> (https://stackoverflow.com/questions/45362548/nonlinear-logistic-regression-package-in-r)...
>  I would be grateful for any opinion on this package or for any alternative 
> recommendation of package/function.
> __
> 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 http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>   [[alternative HTML version deleted]]
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] consecutive n values

2020-06-04 Thread Berry, Charles
Catalin,

> On Jun 4, 2020, at 6:06 AM, Catalin Roibu  wrote:
> 
> Dear R users,
> 
> Please help me to detect consecutive n values in R and their interval.
> 
> 
> rle.seq1<-rle(reco$extr)
> cbind(rle.seq1$values)
> index<-any(rle.seq1$values=="DRY"$lengths>=3)
> cumsum(rle.seq1$lengths)[index]
> 
> reco is a data frame with 2 columns (year and values (DRY, WET).
> 
> I want to have something like this:
> 1799-1800 - WET - 2
> 1803-1805 - WET - 3
> 
> Thank you very much!


Something like:

  wd.rle <- rle(reco$extr)
  is.wet <- wd.rle[["values"]]=="WET"
  wd.rle[["values"]] <- ifelse(is.wet, cumsum( is.wet ), 0)
  wet.list <- split( reco$year, inverse.rle( wd.rle ) )[ -1 ]
  sapply( wet.list[ lengths(wet.list) > 1 ], range)

should get you started.

The last line returns:

: 23678   12   15   20   21   23
: [1,] 1799 1803 1822 1829 1843 1880 1911 1969 1974 1988
: [2,] 1800 1805 1825 1832 1844 1881 1914 1970 1975 1990

You can use `apply' to further process this to get the desired format for your 
result.  

I assume here that reco$years are in groups of consecutive 'WET' years. 

If there are gaps or other oddities you will need to replace `range' with a 
function that handles that.

HTH,

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to read a file containing two types of rows - (for the Netflix challenge data format)

2020-01-31 Thread Berry, Charles



> On Jan 31, 2020, at 1:04 AM, Emmanuel Levy  wrote:
> 
> Hi,
> 
> I'd like to use the Netflix challenge data and just can't figure out how to
> efficiently "scan" the files.
> https://www.kaggle.com/netflix-inc/netflix-prize-data
> 
> The files have two types of row, either an *ID* e.g., "1:" , "2:", etc. or
> 3 values associated to each ID:
> 
> The format is as follows:
> *1:*
> value1,value2, value3
> value1,value2, value3
> value1,value2, value3
> value1,value2, value3
> *2:*
> value1,value2, value3
> value1,value2, value3
> *3:*
> value1,value2, value3
> value1,value2, value3
> value1,value2, value3
> *4:*
> etc ...
> 
> And I want to create a matrix where each line is of the form:
> 
> ID value1, value2, value3
> 
> Si "ID" needs to be duplicated - I could write a Perl script to convert
> this format to CSV, but I'm sure there's a simple R trick.
> 

I'd be tempted to use pipe() to separately read the ID lines and the value 
lines, but in R you can do this:

fc <- count.fields( "yourfile.txt", sep = ",")
rlines <- split( readLines( "yourfile.txt" ), fc)
mat <-
  cbind( rlines[[1]],
do.call( rbind, strsplit( rlines[[2]], ",")))


This assumes that there are exactly 1 or 3 fields in each row of 
"yourfile.txt", if not, some incantation of grepl() applied to the text of 
readLines() should suffice.

HTH,

Chuck 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Identifying presence of Java

2019-11-09 Thread Berry, Charles


> On Nov 9, 2019, at 8:51 AM, Dennis Fisher  wrote:
> 
> R 3.6.3
> OSX and Windows
> 
> Colleagues
> 
> I want to identify if Java is installed on a particular computer.
> 


[...]

> I execute something like:
>   CAPTURE <- system("Java -version", intern=TRUE, ignore.stderr=FALSE, 
> ignore.stdout=FALSE)   
> (with various permutations of TRUE/FALSE for the options) but I cannot 
> capture what is displayed on the console.
> 
> I have also tried “system2” with various TRUE/FALSE permutations without 
> success.
> 
> Any clever ideas?
> 

?Sys.which

HTH,

CCB

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Creating a histogram from a frequency vector

2019-10-10 Thread Berry, Charles


> On Oct 9, 2019, at 9:58 AM, Rui Barradas  wrote:
> 
> Hello,
> 
> Here are 3 ways.
> 

For a large number of bars, sometimes this:

 plot( y, type='h') # maybe use lwd=5

Chuck


> The first are almost the same, they use base graphics.
> 
> x <- 1:6
> y <- c(73,53,42,67,41,50)
> 
> barplot(setNames(y, x))
> 
> Or
> 
> names(y) <- x
> barplot(y)
> 
> 
> And 3:
> 
> library(ggplot2)
> ggplot(data.frame(x, y), aes(x, y)) +
>  geom_col()
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> 
> Às 16:12 de 09/10/19, Nick Wray via R-help escreveu:
>> I have a vector like say 73,53,42,67,41,50 where these numbers are the 
>> number of occurrences of the data values 1,2,3,4,5,6 - so in essence I have 
>> the frequency bit from the hist() function.  I can't see an elegant way 
>> (there are clearly messy workarounds like generating a vector of 73 1's, 53 
>> 2's etc) of creating a histogram from this data set.  Is there one?
>> Thanks Nick Wray
>>  [[alternative HTML version deleted]]
>> __
>> 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 http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Efficient way to update a survival model

2019-08-31 Thread Berry, Charles
The i^th model is included in the Cox[[ i ]] object.

You can extract the formula objects with:

frms <- lapply(Cox, formula)

then if you want the existing and incremental terms:

indeps <- lapply(frms, function(x) as.list( x[[ 3 ]] ))

oldTerms <- lapply(indeps, "[[", 2)

newTerms <- lapply(indeps, "[[", 3)

> oldTerms[3:4]
[[1]]
v + cos(1 * v) + cos(2 * v)

[[2]]
v + cos(1 * v) + cos(2 * v) + cos(3 * v)

> newTerms[ 3:4 ]
[[1]]
cos(3 * v)

[[2]]
cos(4 * v)

> 

HTH,

Chuck

> On Aug 30, 2019, at 3:36 PM, Frank S.  wrote:
> 
> Chris, thank you for your elegant solution!
> 
> Just one minor question:
> I wonder how to include within the loop of your solution the 10 models, that 
> is, writing
> for (k in 1:10) so that you can get {Cox[[1]], ..., Cox[[10]]}. However, I'm 
> aware that some
> change has to be done due to the fact that, when computing Cox[[1]], the term 
> Cox[[k -1]]
> does not exist. Is it possible to perform some "trick" (e.g. re-indexing) in 
> order to achieve this?
> 
> Best,
> 
> Frank
> 
> De: Andrews, Chris 
> Enviado: viernes, 30 de agosto de 2019 15:08
> Para: Frank S. ; Vito Michele Rosario Muggeo 
> 
> Cc: r-help@r-project.org 
> Asunto: RE: [R] Efficient way to update a survival model
> 
> The updated formula needs to have a different term rather than cos(k * v) 
> every time.  Here is one way to explicitly change the formula.
> 
> library("survival")
> set.seed(1)
> v <- runif(nrow(pbc), min = 0, max = 2)
> Cox0 <- coxph(Surv(pbc$time,pbc$status == 2) ~ v, data =  pbc)
> 
> Cox <- vector("list", 10)
> Cox[[1]] <- update(Cox0, . ~ . + cos(1 * v))
> for (k in 2:10) {
>form <- as.formula(sprintf(". ~ . + cos(%d * v)", k))
>Cox[[k]] <- update(Cox[[k-1]], form)
> }
> 
> Cox
> 
> -Original Message-
> From: Frank S. [mailto:f_j_...@hotmail.com]
> Sent: Friday, August 30, 2019 5:54 AM
> To: Vito Michele Rosario Muggeo
> Cc: r-help@r-project.org
> Subject: Re: [R] Efficient way to update a survival model
> 
> Hi everyone,
> 
> Vito, perhaps my previous mail was not clear.  It is true that I used a loop, 
> but the key point is that such a loop
> cannot compute the desired result. For example, for k = 3 the following loop
> 
> Cox <- list()
> Cox[[1]] <- coxph(Surv(time,status == 2) ~ v + cos(v), data =  pbc)
> for (k in 2:10) {
>  Cox[[k]] <- update(Cox[[k-1]], . ~ . + cos(k * v), data =  pbc)
> }
> 
> leads to a model Cox[[3]] which accounts for terms {v, cos(v), cos(3*v)}, but 
> does not include the term cos(2*v).
> I think that this could be one way to solve my question:
> 
> library("survival")
> set.seed(1)
> v <- runif(nrow(pbc), min = 0, max = 2)
> Cox0 <- coxph(Surv(pbc$time,pbc$status == 2) ~ v, data =  pbc)
> k.max <- 9
> Z <- outer(v, 1:k.max, function (x, y) {sin(x * y)})  # Matrix with the outer 
> product of the two arrays
> 
> Cox <- list()
> for (k in 1:k.max){
> Cox[[k]] <-
>   update(Cox0, substitute(. ~ . + Z[, 1:k]), data =  pbc)
>   attr(Cox[[k]]$coefficients, "names")[2:(k+1)] <- paste0("sin(", 1:k, "* v)")
> }
> Cox
> 
> Best,
> 
> Frank
> 
> _

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Open a file which name contains a tilde

2019-06-07 Thread Berry, Charles



> On Jun 6, 2019, at 2:04 PM, Richard O'Keefe  wrote:
> 
> How can expanding tildes anywhere but the beginning of a file name NOT be
> considered a bug?
> 
> 

I think that that IS what libreadline is doing if one allows a whitespace 
separated list of file names.

As reported in R-help, 

https://www.mail-archive.com/r-help@r-project.org/msg254116.html

path.expand seems to expand tildes beginning whitespace separated strings that 
could be filenames, but not other tildes.

Thus,

> path.expand("~/.newsrc ~/.R/*")  # expands both tildes
[1] "/Users/cberry/.newsrc /Users/cberry/.R/*"
> path.expand("~/.newsrc~/.R/*")   # expands only the first
[1] "/Users/cberry/.newsrc~/.R/*"
> 

This could be a feature if what one wanted was to pass the result to a system 
command that will process multiple file arguments, e.g. 

> system(paste( "wc", path.expand("~/.newsrc ~/.R/*"))) # run wc on some files

I doubt that this was intended by R-core, but perhaps the readline devs had 
this in mind. 

Chuck


> On Thu, 6 Jun 2019 at 23:04, Ivan Krylov  wrote:
> 
>> On Wed, 5 Jun 2019 18:07:15 +0200
>> Frank Schwidom  wrote:
>> 
>>> +> path.expand("a ~ b")
>>> [1] "a /home/user b"
>> 
>>> How can I switch off any file crippling activity?
>> 
>> It doesn't seem to be possible if readline is enabled and works
>> correctly.
>> 
>> Calls to path.expand [1] end up [2] in R_ExpandFileName [3], which
>> calls R_ExpandFileName_readline [4], which uses libreadline function
>> tilde_expand [5]. tilde_expand seems to be designed to expand '~'
>> anywhere in the string it is handed, i.e. operate on whole command
>> lines, not file paths.
>> 
>> I am taking the liberty of Cc-ing R-devel in case this can be
>> considered a bug.
>> 
>> --
>> Best regards,
>> Ivan
>> 
>> [1]
>> 
>> https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807
>> 
>> [2]
>> 
>> https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915
>> 
>> [3]
>> 
>> https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147
>> 
>> [4]
>> 
>> https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494
>> 
>> [5]
>> https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187
>> 
>> __
>> 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
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 
>   [[alternative HTML version deleted]]
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] Open a file which name contains a tilde

2019-06-06 Thread Berry, Charles



> On Jun 6, 2019, at 3:59 AM, Ivan Krylov  wrote:
> 
> On Wed, 5 Jun 2019 18:07:15 +0200
> Frank Schwidom  wrote:
> 
>> +> path.expand("a ~ b")  
>> [1] "a /home/user b"
> 
>> How can I switch off any file crippling activity?
> 
> It doesn't seem to be possible if readline is enabled and works
> correctly.
> 
> Calls to path.expand [1] end up [2] in R_ExpandFileName [3], which
> calls R_ExpandFileName_readline [4], which uses libreadline function
> tilde_expand [5]. tilde_expand seems to be designed to expand '~'
> anywhere in the string it is handed, i.e. operate on whole command
> lines, not file paths.
> 

FWIW, the tilde does not always trigger this behavior. 

It seems that the tilde needs to be first in the string or preceded by 
whitespace and succeeded by whitespace or a path separator so the result looks 
like a path. 

> path.expand("~/abc/~/def")
[1] "/Users/cberry/abc/~/def"
> 

> path.expand("~/abc/ ~/def")
[1] "/Users/cberry/abc/ /Users/cberry/def"

> path.expand("~/abc/ ~tilde~/def")
[1] "/Users/cberry/abc/ ~tilde~/def"
> 
> path.expand("~/abc/ ~tilde ~/def")
[1] "/Users/cberry/abc/ ~tilde /Users/cberry/def"

An inelegant workaround could be built by adding escapes to the initial path 
and stripping them from the result.

Perhaps, the intent (in GNU deadline) was to allow a string with multiple paths 
separated by whitespace to be processed.

Maybe this is seen as a feature in GNU readline even though it is not helpful 
here.

HTH,

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Tying to underdressed the magic of lm redux

2019-06-01 Thread Berry, Charles
John,

I believe the pieces you are missing are filed under 'computing on the 
language', 'passing unevaluated objects', and 'language objects'.

Forgive me if I belabor things you already know.  

lm, transform, and many other functions do their "magic" by operating on 
language objects.

You might find a read of the `Computing on the Language' section in Venables 
and Pipley's `S Programmming' useful aa background. There are probably many 
blogs and other on-line resources, too.

The unquoted things you want to operate on are language objects. deparse-ing 
those objects and trying to operate on character strings is one way to deal 
with such objects, but it can get messy.  

You might find that `transform.data.frame' provides a useful example of how to 
deal with language objects provided as arguments to a function.

I suggest you try `debugonce' on lm and on `transform.data.frame'. Run an 
example of each and inspect each object created along the way. 

For lm, you should aim to understand everything through this line: 

 mf <- eval(mf, parent.frame()) 

Getting familiar with `substitute', `match.call', `eval', and friends will help.

HTH,

Chuck

p.s. It sounds like what you want to do is to extend `transform.data.frame' in 
some way. ??

> On Jun 1, 2019, at 6:43 PM, Sorkin, John  wrote:
> 
> Colleagues,
> 
> Despite Bert having tried to help me, I am still unable to perform a simple 
> act with a function. I want to pass the names of the columns of a dataframe 
> along with the name of the dataframe, and use the parameters to allow the 
> function to access the dataframe and modify its contents.
> 
> I apologize multiple postings regarding this question, but it is a 
> fundamental concept that one who wants to program in R needs to know.
> 
> T

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] lm fails on some large input

2019-04-18 Thread Berry, Charles


> On Apr 18, 2019, at 8:24 AM, Michael Dewey  wrote:
> 
> Perhaps subtract 1506705766 from y?

Good advice. Some further notes follow.

One can specify `tol` to have a smaller than default value

e.g.

  m2 <- lm(x ~ y, tol=1e-12)

which is accurate:

  plot(y,x)
  abline(coef=coef(m2))
 

Users of numerical procedures need to be mindful of the default settings of the 
algorithms they use.

As is well known, the use of a too large default for convergence of an 
optimization algorithm can lead to seriously wrong results. There is an example 
described here:

https://science.sciencemag.org/content/296/5575/1945/tab-pdf

One might quibble with the choice of tol=1e-7 (the default in lm.fit), and 64 
bit floating point will support much smaller values. However, there are usually 
statistical issues surrounding fitting highly collinear variables.

So,  `tol = 1e-07` seems more like a feature than a bug.

HTH,

Chuck

> 
> Saying some other software does it well implies you know what the _correct_ 
> answer is here but I would question what that means with this sort of 
> data-set.
> 
> On 17/04/2019 07:26, Dingyuan Wang wrote:
>> Hi,
>> This input doesn't have any interesting properties except y is unix time. 
>> Spreadsheets can do this well.
>> Is this a bug that lm can't do x ~ y?
>> R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
>> Copyright (C) 2018 The R Foundation for Statistical Computing
>> Platform: x86_64-pc-linux-gnu (64-bit)
>> > x = c(79.744, 123.904, 87.29601, 116.352, 67.71201, 72.96001, 101.632, 
>> > 108.928, 94.08)
>> > y = c(1506705739.385, 1506705766.895, 1506705746.293, 1506705761.873, 
>> > 1506705734.743, 1506705735.351, 1506705756.26, 1506705761.307, 
>> > 1506705747.372)
>> > m = lm(x ~ y)
>> > summary(m)
>> Call:
>> lm(formula = x ~ y)
>> Residuals:
>>  Min   1Q   Median   3Q  Max
>> -27.0222 -14.9902  -0.6542  14.1938  29.1698
>> Coefficients: (1 not defined because of singularities)
>> Estimate Std. Error t value Pr(>|t|)
>> (Intercept)   94.734  6.511   14.55 4.88e-07 ***
>> y NA NA  NA   NA
>> ---
>> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>> Residual standard error: 19.53 on 8 degrees of freedom
>> > summary(lm(y ~ x))
>> Call:
>> lm(formula = y ~ x)
>> Residuals:
>> Min  1Q  Median  3Q Max
>> -2.1687 -1.3345 -0.9466  1.3826  2.6551
>> Coefficients:
>>  Estimate Std. Error   t value Pr(>|t|)
>> (Intercept) 1.507e+09  3.294e+00 4.574e+08  < 2e-16 ***
>> x   6.136e-01  3.413e-02 1.798e+01 4.07e-07 ***
>> ---
>> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>> Residual standard error: 1.885 on 7 degrees of freedom
>> Multiple R-squared:  0.9788,Adjusted R-squared:  0.9758
>> F-statistic: 323.3 on 1 and 7 DF,  p-value: 4.068e-07
>> __
>> 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 http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> ---
>> This email has been checked for viruses by AVG.
>> https://www.avg.com
> 
> -- 
> Michael
> http://www.dewey.myzen.co.uk/home.html
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Alternative to lops

2019-04-04 Thread Berry, Charles
Comments inline, but first:

Please review the posting guide and follow the instructions there, especially:

1) "No HTML posting..."

2) "When providing examples, it is best to give an R command that constructs 
the data,..."

> On Apr 4, 2019, at 9:41 AM, Ek Esawi  wrote:
> 
> Hi All--
> 
> Sorry i sent the one inadvertently
> 
> Her is a sample of my data. A data frame (MyDF) and a list (MyList).  My
> own data frame has over 10,000 rows. I want to find out which elements of
> MyDF$B contain any element(s) of MYList; then change MyDF$C to the name of
> the vector of the list that has match.
> 
> I solved this via loops and if statements, using &  but I am hoping for
> a better solution using the apply family functions. I tried something like
> this but did not work.
> 
> lapply(strsplit(MyDF$B," "),function(x) lapply(MyList,function(y)  if(sum(y
> %in% x)>0,x$Code==y[[1]]))
> 
> Thanks in advance--EK
> 
> My Sample data
> 
>> MyDF
> 
>A BC
> 1 1 aa ab ac  0
> 2 2 bb bc bd  0
> 3 3cc cf 0
> 4 4   dd 0
> 5 5   ee 0


Note: You did not tell us if myDF$B is a factor, in which case strsplit needs 
to accommodate multiple blanks:

 levels(MyDF$B)
[1] "  dd" "  ee" "   cc cf" "aa ab ac" "bb bc bd"
> 

> 
>> MyList
> 
> $X
> [1] "a"  "ba" "cc"
> 
> $Y
> [1] "abs" "aa"  "BA"  "BB"
> 
> $z
> [1] "ab" "bb" "xy" "zy" "gh"
> 
> 
> 
> Desired results.
> 
> 
> 
>> MyDF
> 
> AB   C
> 1 1 aa ab ac Y

'aa' matches Y, 'ab' matches z, 'cc' does not match

> 2 2 bb bc bd Y

Huh? 'bb' matches z, 'bc' and 'bd' do not match, 

> 3 3cc cfX

'cc' matches X, 'cf' does not match

> 4 4   dd 0
> 5 5   ee 0
> 

Neither match.


You need to clarify what it is you seek. The example is hard to penetrate.

Maybe this helps you:

> queries <- strsplit(as.character(MyDF$B), "[ ]+")
> matches <- match( unlist(queries), unlist(MyList), 0)
> hits <- findInterval( matches, 1+cumsum(c(0,lengths(MyList
> hitList <- relist(hits, queries)
> hitList
[[1]]
[1] 2 3 0

[[2]]
[1] 3 0 0

[[3]]
[1] 0 1 0

[[4]]
[1] 0 0

[[5]]
[1] 0 0

You can now process hitList to get the desired vector.

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Vectorizing a for-loop for cross-validation in R

2019-01-23 Thread Berry, Charles
See inline.

> On Jan 23, 2019, at 2:17 AM, Aleksandre Gavashelishvili 
>  wrote:
> 
> I'm trying to speed up a script that otherwise takes days to handle larger
> data sets. So, is there a way to completely vectorize or paralellize the
> following script:
> 
>*# k-fold cross validation*
> 
> df <- trees # a data frame 'trees' from R.
> df <- df[sample(nrow(df)), ] # randomly shuffles the data.
> k <- 10 # Number of folds. Note k=nrow(df) in the leave-one-out cross
> validation.
> folds <- cut(seq(from=1, to=nrow(df)), breaks=k, labels=FALSE) # creates
> unique numbers for k equally size folds.
> df$ID <- folds # adds fold IDs.
> df[paste("pred", 1:3, sep="")] <- NA # adds multiple columns "pred1"
> "pred2" "pred3" to speed up the following loop.
> 
> library(mgcv)
> 

Rprof()

replicate(100, {


> for(i in 1:k) {
>  # looping for different models:
>  m1 <- gam(Volume ~ s(Height), data=df, subset=(ID != i))
>  m2 <- gam(Volume ~ s(Girth), data=df, subset=(ID != i))
>  m3 <- gam(Volume ~ s(Girth) + s(Height), data=df, subset=(ID != i))
> 
>  # looping for predictions:
>  df[df$ID==i, "pred1"] <- predict(m1, df[df$ID==i, ], type="response")
>  df[df$ID==i, "pred2"] <- predict(m2, df[df$ID==i, ], type="response")
>  df[df$ID==i, "pred3"] <- predict(m3, df[df$ID==i, ], type="response")
> }
> 

})

Rprof(NULL)

summaryRprof()

## read ?Rprof to get a sense of what it does

## read the summary to determine where time is being spent.

## the result was surprising to me. YMMV.

## there may be redundancies that you can eliminate by 
##  - doing the setup within gam() one time and saving it
##  - calling the worker functions by modifying the setup 
##in a loop or function and saving the results


> # calculating residuals:
> df$res1 <- with(df, Volume - pred1)
> df$res2 <- with(df, Volume - pred2)
> df$res3 <- with(df, Volume - pred3)
> 
> Model <- paste("m", 1:3, sep="") # creates a vector of model names.
> 
> # creating a vector of mean-square errors (MSE):
> MSE <- with(df, c(
>  sum(res1^2) / nrow(df),
>  sum(res2^2) / nrow(df),
>  sum(res3^2) / nrow(df)
> ))
> 
> model.mse <- data.frame(Model, MSE) # creates a data frame of model names
> and mean-square errors.
> model.mse <- model.mse[order(model.mse$MSE), ] # rearranges the previous
> data frame in order of increasing mean-square errors.
> 
> I'd appreciate any help. This code takes several days if run on >=30,000
> different GAM models and 3 predictors. Could you please help with
> re-writing the script into sapply() or foreach()/doParallel format?
> 

This is something you should learn to do. It is pretty standard practice. Use 
the body of your for loop as the body of a function, add arguments, and create 
a suitable return value. The something like

lapply( 1:k, your.loop.body.function, other.arg1, other.arg2, ...)

should work.  If it does, then parallel::mclapply(...) should also work.

HTH,

Chuck

 
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Accessing Data Frame

2019-01-03 Thread Berry, Charles
See below.

> On Jan 3, 2019, at 6:50 AM, Benoit Galarneau  
> wrote:
> 
> Hi everyone,
> I'm new to the R world.
> Probably a newbie question but I am stuck with some concept with data frame.
> I am following some examples in the "Hands-On Programming with R".
> 
> In short, how can I access/filter items in a data frame using a variable.
> 
> One example consists of manipulating elements from a deck of card:
> 
>> deck
>face suit value
> 1   king   spades13
> 2  queen   spades12
> 3   jack   spades11
> 4ten   spades10
> etc.
> 
> Let's say I want to remove or filter out the first card. I know I could do 
> deck[-1].
> 
> But let's say I have: topCard <- deck[1,]
> 
> topCard is then a list of 3 elements
>> topCard
>  face   suit value
> 1 king spades13
> 
> My question is the following, how can I remove or filter out the deck using 
> the topCard variable.
> 
> In my programmer's head, something similar to this should "work":
>> deck[10,]
>   face   suit value
> 10 four spades 4
>> aCard <- deck[10,]
>> aCard
>   face   suit value
> 10 four spades 4
>> deck[aCard]
> Error in `[.default`(deck, aCard) : invalid subscript type 'list'
> 
> Wihout having to specify all elements in the logical tests.
> 
> deck[deck$face == aCard$face & deck$suit == aCard$suit & deck$value == 
> aCard$value,]
>   face   suit value
> 10 four spades 4

matchRow <- Reduce( "&", mapply("==", deck, aCard, SIMPLIFY=FALSE))

deck[ !matchRow, ]

There is a little `trick' here. A "data.frame" object is a "list" object and 
both `deck` and `aCard` are "data.frame" objects. So using `mapply` as above 
operates on the successive columns of each.  


You will find many useful idioms using the *apply functions. 

As you are new to R, I recommend running the example() for each of them as well 
as browsing the help page.

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] fortune nomination WAS:Re: help

2018-12-13 Thread Berry, Charles


"You need Santa Claus not r-help."

in response to an unrealistic and poorly posed request for help.

Best,

Chuck

> On Dec 13, 2018, at 11:12 AM, David L Carlson  wrote:
> 
> You need Santa Claus not r-help. You haven't given us a fraction of the 
> information we would need to help. You don't show us your code. You don't 
> tell us where the information is coming from except "today's date." You don't 
> tell us what data you want. You don't seem to know the difference between R 
> and R-Studio.
> 
> 
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77843-4352
> 
> 
> -Original Message-
> From: R-help  On Behalf Of Miller, Shawn
> Sent: Thursday, December 13, 2018 11:29 AM
> To: R-help@r-project.org
> Subject: [R] help
> 
> Need help with R studio. Code is to pull data from todays date plus 5 years 4 
> months from now. I am missing the last 3 months of data. Can you please help?
> 
> Shawn Miller | Aquatic Biologist II | Assessment Section
> Environmental Protection | Clean Water
> Rachel Carson State Office Building
> 400 Market Street | Harrisburg, PA 17101
> Phone: 717.772.2185 | Fax: 717.772.3249
> www.depweb.state.pa.us
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> 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 http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] For loop with multiple iteration indexes

2018-09-10 Thread Berry, Charles
I have a sense of deja vu:

https://www.mail-archive.com/r-help@r-project.org/msg250494.html

There is some good advice there.

> On Sep 9, 2018, at 3:49 PM, David Disabato  wrote:
> 
> Hi R-help,
> 
> I am trying to create a for loop with multiple iteration indexes. I don't
> want to use two different for loops nested together because I don't need
> the full matrix of the two indexes, just the diagonal elements (e.g., i[1]
> & j[1] and i[2] & j[2], but not i[1] & j[2]). Is there a way to specify
> both i and j in a single for loop? Here is a simplified example of
> pseudo-code where x and y are equally sized character vectors with column
> names and dat is their dataframe (obviously this code doesn't run in R, but
> hopefully you perceive my goal):
> 
> r <- list()
> n <- 0
> for (i in x; j in y) {
>   n <- n + 1
>   r[[n]] <- cor(x = dat[, i], y = dat[, j])
> }
> print(r)
> 
> I realize there are other solutions to this particular correlation example,
> but my actual problem is much more complicated, so I am hoping for a
> solution that generalizes across any code within the for loop.

A more aRtful way (than a for loop) to approach this is with mapply:


i <- head(colnames(mtcars))
j <- tail(colnames(mtcars))

r <- mapply(function(i, j, dat) cor( x = dat[, i], y = dat[, j]),
   i=i , j=j , MoreArgs = list( dat = mtcars), 
   SIMPLIFY = FALSE, USE.NAMES = FALSE)


and if you want, maybe USE.NAMES = paste(i, j, sep="_")

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles



> On Aug 4, 2018, at 12:59 PM, Jeff Newmiller  wrote:
> 
...

> Slick work on the vectorizing, but for the future reference it was slightly 
> buggy:
> 

Thanks for catching that!

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles



> On Aug 4, 2018, at 11:43 AM, Jeff Newmiller  wrote:
> 
> Sometimes a good old for loop performs best, even if it doesn't look sexy:
> 
> 

Fair enough, but a vectorized solution beats them all (see below).

Also,

[SNIP]


> # Charles
> ans1b <- function( a, b )
> {

The lapply you put here was from Eric's solution:

>  xxx <- lapply( seq.int( nrow( A ) )
>   , function( i ) {
>A[ i, ] %o% B[ i, ]
> }


This is what I had in mind:

ans1b.corrected <- function( a, b ) {
  yyy <- sapply( seq.int( nrow( a ) )
 , function( i ) a[ i, ] %o% b[ i, ]
 , simplify = "array"
  )
  zzz <- aperm( yyy, c( 3, 1, 2 ) )
  zzz
}

On my system it is slower than a for loop but a lot faster than your benchmark 
showed with the superfluous code from Eric's solution.

For speed, a vectorized solution is faster than a for loop by a factor of 3 on 
my laptop:

ans0 <- function(A,B){
  nca <- ncol(A)
  ncb <- ncol(B)
  j.index <- rep(1:nca, times = ncb)
  k.index <- rep(1:nca, each = ncb)
  res <- array(A[, j.index] * B[, k.index], c(nrow(A), nca, ncb))
  res
  }


> microbenchmark(
+   res0 <- ans0(A, B),
+   res1 <- ans1(A, B),
+   res1b <- ans1b.corrected(A, B),
+   res2 <- ans2(A, B),
+   res3 <- ans3(A, B)
+ )
Unit: microseconds
   expr min   lq  mean   median   uq
 max neval   cld
 res0 <- ans0(A, B)  13.281  18.4960  21.52723  19.9905  23.4750  
61.556   100 a
 res1 <- ans1(A, B) 353.121 369.8635 409.77788 381.5840 444.3290 
701.256   100 e
 res1b <- ans1b.corrected(A, B)  82.816  89.4185 101.52321  95.4275 107.1700 
217.357   100   c  
 res2 <- ans2(A, B)  49.674  54.4825  61.78278  58.7540  65.5265 
172.625   100  b   
 res3 <- ans3(A, B) 317.772 342.4220 392.25065 360.4675 436.2125 
602.346   100d 
> 


FWIW, if there are dimnames on A and B, sapply( row names(A), ..., 
simplify="array") preserves them without further ado.

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles



> On Aug 4, 2018, at 10:01 AM, Eric Berger  wrote:
> 
> Hi Rolf,
> A few edits because (i) nrow(a) should be nrow(A) and (ii) you have
> calculated C[j,k,i] = A[i,j]*B[i,k], (iii) minor style change on lapply.
> 
> library(abind)
> xxx <- lapply(1:nrow(A),function(i){A[i,]%o%B[i,]})
> yyy <- do.call(abind,c(xxx,list(along=3)))

Or use the simplify="array" gambit in sapply:

yyy <- sapply(1:nrow(A), function(i) A[i,] %o% B[i,], simplify="array")

> zzz <- aperm(yyy,c(3,1,2))
> 

HTH, 

Chuck

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Suggestions for scatter plot of many data

2018-07-19 Thread Berry, Charles



> On Jul 18, 2018, at 1:55 PM, Rich Shepard  wrote:
> 
>  I have daily precipitation data for 58 locations from 2005-01-01 through
> 2018-06-18.

So roughly 5000 observations of latitiude, longitude, elevation(?), and amount.

Maybe something dynamic like Hans Rosling does:

https://towardsdatascience.com/how-to-build-animated-charts-like-hans-rosling-doing-it-all-in-r-570efc6ba382

possibly smoothing temporally.

Googling `Hans Rosling R' and 'Dynamic Graphics in R' should get you some other 
hits including the Graphics Task View.

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Generate random Bernoulli draws

2018-07-06 Thread Berry, Charles
Sorry about the last incomplete post. Accidentally hit send.

Meant to say that I was hoping that a correct, but  obscure response from me 
would motivate David to step back and think about his problem long enough to 
see that it has an easy solution.

Sorry if that was out-of-line.

Chuck

> On Jul 6, 2018, at 4:27 PM, Charles Berry  wrote:
> 
>> On Jul 6, 2018, at 3:31 PM, Duncan Murdoch  wrote:
>> 
>> On 06/07/2018 1:18 PM, Berry, Charles wrote:
>>> A liitle math goes along way. See below.
>>>> On Jul 5, 2018, at 10:35 PM, Marino David  wrote:
>>>> 
>>>> Dear Bert,
>>>> 
>>>> I know it is a simple question. But for me, at current, I fail to implement
>>>> it. So, I ask for help here.
>>>> 
>>>> It is not homework.
>>>> 
>>>> Best,
>>>> 
>>>> David
>>>> 
>>>> 2018-07-06 13:32 GMT+08:00 Bert Gunter :
>>>> 
>>>>> Is this homework?
>>>>> 
>>>>> (There is an informal no-homework policy on this list).
>>>>> 
>>>>> Cheers,
>>>>> Bert
>>>>> 
>>>>> 
>>>>> 
>>>>> Bert Gunter
>>>>> 
>>>>> "The trouble with having an open mind is that people keep coming along and
>>>>> sticking things into it."
>>>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>>>> 
>>>>> On Thu, Jul 5, 2018 at 10:23 PM, Marino David 
>>>>> wrote:
>>>>> 
>>>>>> Dear All,
>>>>>> 
>>>>>> I would like to generate N random Bernoulli draws given a probability
>>>>>> function F(x)=1-exp(-2.5*x) in which x follows  uniform distribution, say
>>>>>> x~U(0,2).
>>> If each Bernoulli draw is based on its own draw of x, then
>>> rbinom( N, 1, 0.8013476 )
>>> is what you want.
>>> It is left as an exercise for the reader to verify that the constant 
>>> 0.8013476 is correct up to approximation error, and to prove that such a 
>>> Bernoulli mixture is also Bernoulli. Perhaps,
>>> ?integrate
>>> will help.
>>> But if the x's are shared you need to use runif, expm1, and (possibly) rep 
>>> to produce a vector to be used in place of the prob argument.
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Generate random Bernoulli draws

2018-07-06 Thread Berry, Charles



> On Jul 6, 2018, at 3:31 PM, Duncan Murdoch  wrote:
> 
> On 06/07/2018 1:18 PM, Berry, Charles wrote:
>> A liitle math goes along way. See below.
>>> On Jul 5, 2018, at 10:35 PM, Marino David  wrote:
>>> 
>>> Dear Bert,
>>> 
>>> I know it is a simple question. But for me, at current, I fail to implement
>>> it. So, I ask for help here.
>>> 
>>> It is not homework.
>>> 
>>> Best,
>>> 
>>> David
>>> 
>>> 2018-07-06 13:32 GMT+08:00 Bert Gunter :
>>> 
>>>> Is this homework?
>>>> 
>>>> (There is an informal no-homework policy on this list).
>>>> 
>>>> Cheers,
>>>> Bert
>>>> 
>>>> 
>>>> 
>>>> Bert Gunter
>>>> 
>>>> "The trouble with having an open mind is that people keep coming along and
>>>> sticking things into it."
>>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>>> 
>>>> On Thu, Jul 5, 2018 at 10:23 PM, Marino David 
>>>> wrote:
>>>> 
>>>>> Dear All,
>>>>> 
>>>>> I would like to generate N random Bernoulli draws given a probability
>>>>> function F(x)=1-exp(-2.5*x) in which x follows  uniform distribution, say
>>>>> x~U(0,2).
>> If each Bernoulli draw is based on its own draw of x, then
>>  rbinom( N, 1, 0.8013476 )
>> is what you want.
>> It is left as an exercise for the reader to verify that the constant 
>> 0.8013476 is correct up to approximation error, and to prove that such a 
>> Bernoulli mixture is also Bernoulli. Perhaps,
>>  ?integrate
>> will help.
>> But if the x's are shared you need to use runif, expm1, and (possibly) rep 
>> to produce a vector to be used in place of the prob argument.
> 
> That may be correct from a mathematical perspective (I haven't checked), but 
> seems like a *really* bad idea from a programming perspective.  It would be 
> much better to write

Well of course it would.  I was hoping that my somewhat obscure one-liner would 
suggest


> 
> x <- runif(N, 0, 2)
> rbinom(N, 1, 1 - exp(-2.5*x))
> 
> because it is so much more clearly related to the original problem statement. 
>  Perhaps it would be a few microseconds slower, but that would be saved many 
> times over when any aspect of the problem statement was modified.
> 
> Duncan Murdoch
> 
>> HTH,
>> Chuck
>>>>> 
>>>>> Can some one leave me some code lines for implementing this?
>>>>> 
>>>>> Thanks in advance.
>>>>> 
>>>>> David
>>>>> 
>>>>>[[alternative HTML version deleted]]
>>>>> 
>>>>> __
>>>>> 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 http://www.R-project.org/posti
>>>>> ng-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>> 
>>>> 
>>>> 
>>> 
>>> [[alternative HTML version deleted]]
>>> 
>> __
>> 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 http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Generate random Bernoulli draws

2018-07-06 Thread Berry, Charles
A liitle math goes along way. See below.

> On Jul 5, 2018, at 10:35 PM, Marino David  wrote:
> 
> Dear Bert,
> 
> I know it is a simple question. But for me, at current, I fail to implement
> it. So, I ask for help here.
> 
> It is not homework.
> 
> Best,
> 
> David
> 
> 2018-07-06 13:32 GMT+08:00 Bert Gunter :
> 
>> Is this homework?
>> 
>> (There is an informal no-homework policy on this list).
>> 
>> Cheers,
>> Bert
>> 
>> 
>> 
>> Bert Gunter
>> 
>> "The trouble with having an open mind is that people keep coming along and
>> sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>> 
>> On Thu, Jul 5, 2018 at 10:23 PM, Marino David 
>> wrote:
>> 
>>> Dear All,
>>> 
>>> I would like to generate N random Bernoulli draws given a probability
>>> function F(x)=1-exp(-2.5*x) in which x follows  uniform distribution, say
>>> x~U(0,2).

If each Bernoulli draw is based on its own draw of x, then

rbinom( N, 1, 0.8013476 )

is what you want.

It is left as an exercise for the reader to verify that the constant 0.8013476 
is correct up to approximation error, and to prove that such a Bernoulli 
mixture is also Bernoulli. Perhaps, 

?integrate

will help.

But if the x's are shared you need to use runif, expm1, and (possibly) rep to 
produce a vector to be used in place of  the prob argument.

HTH,

Chuck


>>> 
>>> Can some one leave me some code lines for implementing this?
>>> 
>>> Thanks in advance.
>>> 
>>> David
>>> 
>>>[[alternative HTML version deleted]]
>>> 
>>> __
>>> 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 http://www.R-project.org/posti
>>> ng-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>> 
>> 
>> 
> 
>   [[alternative HTML version deleted]]
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R maintains old values

2018-07-03 Thread Berry, Charles



> On Jul 3, 2018, at 6:25 AM, J C Nash  wrote:
> 
> Now, to add to the controversy, how do you set a computer on fire?

>From the bash prompt:

stuxnet --overload=cpu,disk,network,gpu --fan=off --no-warnings 


HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] subsetting lists....

2018-06-18 Thread Berry, Charles



> On Jun 18, 2018, at 4:15 AM, akshay kulkarni  wrote:
> 
> correctionI want the method without a for loop

Here are two. The first is more readable, but the second is 5 times faster.

mapply("[", YH, iuhV)

unlist(YH, recursive = FALSE, use.names = FALSE)[cumsum( lengths(YH)) - 
lengths(YH) + iuhV]

HTH,

Chuck

> 
> From: akshay kulkarni 
> Sent: Monday, June 18, 2018 4:25 PM
> To: R help Mailing list
> Subject: subsetting lists
> 
> dear members,
>I have list YH and index vector iuhV. I want to 
> select iuhV[1] from YH[[1]], iuhv[2] from YH[[2]], iuhv[3] from 
> YH[[3]]..iuhv[n] from YH[[n]]...
> 
> How to do this?
> I searched SO and the internet but was bootless
> 
> Very many thanks for your time and effort.
> Yours sincerely,
> AKSHAY M KULKARNI
> 
>   [[alternative HTML version deleted]]
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] printing an arbitrary-length character vector in columns on a page of a pdf report

2018-06-05 Thread Berry, Charles



> On Jun 5, 2018, at 9:45 AM, Christopher W Ryan  wrote:
> 
> I'm writing code for a recurring report, using an R --> Sweave --> pdflatex
> workflow. It includes a character vector of short words that I would like
> to display compactly, in columns on a page, rather than one word per line,
> which would waste a lot of space. The vector of words will increase
> unpredictably over time, with future versions of the report.
> 
> I thought I would go about it by turning the character vector into a
> matrix, as follows:
> 
> dd <- LETTERS
> ## set number of columns. Three for now
> nc <- 3
> ## have to pad the character vector to a length that is multiple of nc
> add <- nc - (length(dd) %% nc)
> dd2 <- c(dd, rep("", add))
> ddm <- matrix(dd2, ncol = nc)
> library(Hmisc)
> latex(ddm, file = "")
> 
> Any ideas for a more elegant way to do this?
> 

Use a LaTeX longtable environment (and add \usepackage{longtable} to the header 
of your document). Put something like this in your *.Rnw file:

% longtable header goes here

<>=
cat( paste(dd, ifelse( seq(along = dd) %% 3 == 0, "\n", "&")) )
@

% longtable footer goes here

should do it. But if there are exactly 3*k elements, you might skip the 
trailing `\\'.

If you are unclear what the header/footer ought to look like try this:

HMisc::latex(ddm, file="", longtable=TRUE)

and you should figure it out.

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] to R Core T: mle function in 32bits not respecting the constrain

2018-05-28 Thread Berry, Charles


> On May 27, 2018, at 10:31 PM, francesc badia roca  wrote:
> 
> I have an issue using mle in versions of 32 bits.
> 
> I am writing a package which I want to submit to the CRAN.
> When doing the check, there is an example that has an error running in the
> 32 bits version.
> 
> The problem comes from the mle function, using it with a lower constrain.
> In 64 bits version it works fine but when I put it in the R 32 bits it
> fails. (same numbers, all equal!)
> 
> The call is:
> *mle(minuslogl = p.est,start = beta,method =
> "L-BFGS-B",lower=llim*reduction)*
> lower  = -0.01570427
> 
> The optimizer (optim function in 32 bits) display:
> -0.015704 -loglik 48.690236
> -0.015704 -loglik 48.690236
> -0.017704 -loglik 1.#QNAN0
> 
> And it is not respecting the lower constrain.
> 
> Could anyone explain me why this?

FAQ 7.31 is a likely suspect.

You might need a `safe' lower bound. Perhaps, adding a small positive constant 
to it will fix it.

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] is there any method to defer the execution of code in r?

2018-05-13 Thread Berry, Charles


> On May 13, 2018, at 9:24 AM, Jeff Newmiller  wrote:
> 
> Not when I click on that link.
> 
> 

Nor me, but what I get is actually

https://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r

note the number is *different* than 50314015 - the OPs posting

Seems like a bug - perhaps in SO's server?

HTH,

Chuck

p.s. the OP's question was cross-referenced on the page I got.
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to define mutualy exclusive parameters of a function

2018-04-26 Thread Berry, Charles


> On Apr 26, 2018, at 6:46 AM, Polychronis Kostoulas 
>  wrote:
> 
> Dear All,
> apologies if this is basic: I am writing a function:
> 
> fb<-function(mean, median, mode, a, b=0.95, lower=F)
> {}
> 
> The arguments mean, median and mode are mutually exclusive (i.e. the user
> should define only one of these). How do I code this within the function?
> 

See

?missing

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] getting all circular arrangements without accounting for order

2018-03-30 Thread Berry, Charles


> On Mar 29, 2018, at 6:48 PM, Ranjan Maitra  wrote:
> 
> Dear friends,
> 
> I would like to get all possible arrangements of n objects listed 1:n on a 
> circle.
> 
> Now this is easy to do in R. Keep the last spot fixed at n and fill in the 
> rest using permuations(n-1, n-1) from the gtools package.
> 
> However, what if clockwise or counterclockwise arrangements are the same? I 
> know that half of the above (n - 1)! arrangements are redundant.
> 
> Is there an easy way to list these (n-1)!/2 arrangements? 
> 

Well half of these arrangements will be of the form 

`k, ... , j, n' 

and half will be of the form 

`j, ...,  k, n'

So fix n in position n, select (k,j), and require that the first position is 
min(k,j) and position n-1 is max(k,j). 

There are choose(n-1,2) choices for {(k,j):k

Re: [R] Gaussian Process Classification R packages

2017-12-11 Thread Berry, Charles

> On Dec 11, 2017, at 8:06 AM, Damjan Krstajic  wrote:
> 
> I have kindly asked for help and I am sad to receive such a reply from some 
> on the r-help list.
> 
> 

Well, you only said you were `struggling' to find a package.

Bert may well have done the Google search himself and found numerous resources 
on such models including links to R (as I did, see below).  If so, his response 
seems quite natural.

Perhaps, you need to say what is wrong with the hits you got and the packages 
that they describe to keep a potential response from running in the wrong 
direction. Perhaps, you have misunderstood the capabilities of a package or 
failed to grasp an inobvious way to use the package to reach your goal.  In any 
case, providing some background of why you think the obvious leads do not work 
in your case can be helpful.

Doing that search myself I see links to R packages, R functions, and to "The 
Gaussian Processes Web Site" which has a table of possibly relevant softwares.  
It seems like there is a lot there to digest.

HTH,

Chuck

> I did google it prior to sending my request, and I could not find any R 
> package which provides GP classification model which produces prediction 
> intervals for each sample. I would be grateful if anybody could inform me 
> about it. Thank you.
> 
> 
> 
> From: Bert Gunter 
> Sent: 11 December 2017 15:50
> To: Damjan Krstajic
> Cc: r-help@r-project.org
> Subject: Re: [R] Gaussian Process Classification R packages
> 
> Google it!
> 
> "R Gaussian process model binary classification."
> 
> Cheers,
> Bert
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] libPaths displays truncated path?

2017-11-23 Thread Berry, Charles

> On Nov 23, 2017, at 4:34 AM, Loris Bennett  wrote:
> 
> Hi,
> 
> TL;DR
> -
> 
>  I define the path
> 
>/cm/shared/apps/R/site-library/3.4.2
> 
>  and add it to libPath.  Why does libPath then display it as
> 
>/cm/shared/apps/R/site-library/3.4
> 
>  ?
> 

Because it is a symbolic link.

?.libPaths says

"For consistency, the paths are always normalized by normalizePath(winslash = 
"/")."

and ?normalizePath says

"...the Unix-alike platform ... attempts to turn paths into absolute paths in 
their canonical form (no ./, ../ nor symbolic links)."  

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] draw a circle with a gradient fill

2017-10-24 Thread Berry, Charles

> On Oct 24, 2017, at 2:56 AM, Alaios via R-help  wrote:
> 
> Hi all,I would like to draw a simple circle where the color gradient follows 
> the rule color = 1/(r^2) where r is the distance from the circle.

This is called a radial gradient fill in SVG speak.

[snip]

> but this package does not give easily color gradients and so my solutions 
> contains 5 same colors filled rings.


I would look at the gridSVG package. Examples of various gradient fills are 
here: 

https://www.stat.auckland.ac.nz/~paul/Reports/leaf/leaf.html#idp625472

HTH,

Chuck
__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] "Improvement with the R code"

2017-08-28 Thread Berry, Charles

> On Aug 28, 2017, at 9:26 AM, Elie Canonici Merle 
>  wrote:
> 
> Chuck (Is it fine to call you Chuck?)

In this forum, yes please.


> I don't know much about pmin and factor but it might worth looking into if
> you want to manipulate states by names (I assume this is why one might want
> to use it?)
> 

Actually is it because the OP had states 1-4 in his data. In the pre-state only 
1-3 get counted. In the post-state, 4 gets rolled into 3. 

So,

pre <- factor(data, 1:3) # 4 and higher are NA 
post <- pmin(3, data)# 4 and higher become 3

implements those rules, and table(pre, post) gives a 3 x 3 table that one need 
not subset.

Chuck

> generate_transition_matrix <-  function(data, states)
>prop.table(table(head(data, -1), tail(data, -1)), 1)[states,]
> 
> 
> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,1,3,2,1,1,3,2,1,3,2))
> 
> states=c(2,3)
> 
> transition_matrix= generate_transition_matrix(checkdf$clusterNum, states)
> transition_matrix
> 

__
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] "Improvement with the R code"

2017-08-28 Thread Berry, Charles
All of this can be done without for loops. 

Use head(..., -1), tail(..., -1) to get the pre and post states.

Use factor or pmin to recode them as necessary

Use table(pre, post) to get the transition counts.

Use prop.table(table_of_counts,1) to get the probabilities.

HTH,

Chuck


> On Aug 28, 2017, at 8:31 AM, Elie Canonici Merle 
>  wrote:
> 
> Hi,
> 
> I think you overthought this one a little bit, I don't know if this is the
> kind of code you are expecting but I came up with something like that:
> 
> generate_transition_matrix <-  function(data, n_states) {
> 
>#To be sure I imagine you should check n_states is right at this point
> 
>transitions <- matrix(0, n_states, n_states)
> 
>#we could improve a little bit here because at step N+1 source is dest
> from step N
>#but it would not be as readable
>for (k in 1:(length(data) - 1)) {
>source_state <- data[k]
>dest_state <- data[k+1]
>transitions[source_state, dest_state] <- transitions[source_state,
> dest_state] + 1
>}
> 
>for (k in 1:n_states)
>transitions[k,] <- transitions[k,] / sum(transitions[k,])
>transitions
> }
> 
> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2))
> no_of_state=4
> 
> transition_matrix= generate_transition_matrix(checkdf$clusterNum,
> no_of_state)
> transition_matrix
> 
> 
> 
> 2017-08-28 16:37 GMT+02:00 niharika singhal :
> 
>> Hello,
>> 
>> I am trying to implement a formula
>> 
>> aij= transition from state S_i to S_j/no of transition at state S_i
>> 
>> 
>> 
>> Code I have written is working with three state {1,2,3 }, but if the number
>> of states become={1,2,3,4,..n} then the code will not work, so can some
>> help me with this.
>> 
>> For   and some rows of my  data frame look like
>> 
>> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2)
>> no_of_state=3
>> transition_matrix=matrix(NA,nrow=no_of_state, ncol=no_of_state)
>> for(k in 1: no_of_state)
>> {
>>  count1=0
>>  count2=0
>>  count3=0
>>  #For last point no transition takes place
>>  for(j in 1: (nrow(checkdf)-1))
>>  {
>> 
>>if(checkdf$clusterNum[j]==k)
>>{
>>  if(checkdf$clusterNum[j+1]==1){
>>count1=count1+1
>>  }
>>  else if(checkdf$clusterNum[j+1]==2){
>>count2=count2+1
>>   }
>>  else {
>>count3=count3+1
>>   }
>>}
>>  }
>> 
>>  no_of_points=(count1+count2+count3)
>>  s1=count1/no_of_points
>>  s2=count2/no_of_points
>>  s3=count3/no_of_points
>>  transition_matrix[k,]=c(s1, s2, s3)
>> 
>> }
>> 
>> I know the code is not written nicely and I want to improve it.
>> 
>> Thanks in advance
>> Niharika
>> 
>>[[alternative HTML version deleted]]
>> 
>> __
>> 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 http://www.R-project.org/
>> posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 
>   [[alternative HTML version deleted]]
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.