Re: [R] How to tweak genomic plot with genoPlotR?

2024-04-02 Thread Eric Berger
According to https://cran.r-project.org/web/packages/genoPlotR/index.html
the maintainer of genoPlotR is

Lionel Guy 

Send your question also to him.

On Tue, Apr 2, 2024 at 11:27 AM Luigi Marongiu  wrote:
>
> I would like to use your genoPlotR package
> (doi:10.1093/bioinformatics/btq413) to compare the genomes of two
> isolates of E. coli K-12 that I have. One is a K-12 that was in my
> lab's fridge; the other is a derivative of K-12 bought some time ago,
> HB101.
> I tried to use genoPlotR, but I could not understand some functions
> from your vignette. I would like to ask you whether you could help me
> with this.
>
> I aligned the genomes (reference K-12 plus my isolates) with
> `progressiveMauve --weight=15 --output=./K12_Aln.fa K12_multi.fa`,
> where K12_multi.fa contains the fasta sequences of the reference and
> the consensuses I obtained from my isolates after Illumina NGS. I then
> ran this script:
>
> ```
> ## get data
> bbone_file = "./K12_Aln.backbone"
> bbone = read_mauve_backbone(bbone_file, ref=2)
> names(bbone$dna_segs) = c("K-12 ref.", "K-12 Ho", "HB101 Ho")
>
> ## calculate lengths
> for (i in 1:length(bbone$comparisons)) {
>   cmp = bbone$comparisons[[i]]
>   bbone$comparisons[[i]]$length = abs(cmp$end1 - cmp$end1) +
>   abs(cmp$end2 - cmp$end2)
> }
>
> ## plot
> plot_gene_map(dna_segs = bbone$dna_segs,
>   comparisons = bbone$comparisons,
>   global_color_scheme = c("length", "increasing", "red_blue", 
> 0.7),
>   override_color_schemes = TRUE)
> ```
> I got the following plot: https://u.cubeupload.com/Gigiux/Rplot.png
> My questions are:
> - How can I load the annotations? I have the K-12 annotations in gff3
> and genebank formats, but how do I load them in the system so that I
> plot it here?
> - Is it possible to zoom in?
> - Is it possible to change the color scheme?
> Thank you
>
>
>
>
> --
> Best regards,
> Luigi
>
> __
> 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] Help request: Parsing docx files for key words and appending to a spreadsheet

2023-12-30 Thread Eric Berger
Sorry, I was being too quick.
You have to pay attention to the pipe operator

You were advised to do the following

content <- read_docx(full_filename) |>
docx_summary()

which should have worked but I think you left out the |> operator.

Alternatively

tmp <- read_docx(full_filename)
content <-  docx_summary(tmp)



On Sat, Dec 30, 2023 at 2:37 PM Andy  wrote:

> An update: Running this block of code:
>
> # Load libraries
> library(tcltk)
> library(tidyverse)
> library(officer)
>
> filepath <- setwd(tk_choose.dir())
>
> filename <- "Now they want us to charge our electric cars from litter
> bins.docx"
>
> #full_filename <- paste0(filepath, filename)
> full_filename <- paste(filepath, filename, sep="/")
>
> if (!file.exists(full_filename)) {
>message("File missing")
> } else {
>content <- read_docx(full_filename) |>
>  docx_summary()
># this reads docx for the full filename and
># passes it ( |> command) to the next line
># which summarises it.
># the result is saved in a data frame object
># called content which we shall show some
># heading into from
>
>head(content)
> }
>
>
> Results in this error now:Error in x$doc_obj : $ operator is invalid for
> atomic vectors
>
> Thank you.
>
>
>
> On 30/12/2023 12:12, Andy wrote:
> > Hi Eric
> >
> > Thanks for that. That seems to fix one problem (the lack of a
> > separator), but introduces a new one when I complete the function
> > Calum proposed:Error in docx_summary() : argument "x" is missing, with
> > no default
> >
> > The whole code so far looks like this:
> >
> >
> > # Load libraries
> > library(tcltk)
> > library(tidyverse)
> > library(officer)
> >
> > filepath <- setwd(tk_choose.dir())
> >
> > filename <- "Now they want us to charge our electric cars from litter
> > bins.docx"
> > #full_filename <- paste0(filepath, filename) # Calum's original
> suggestion
> >
> > full_filename <- paste(filepath, filename, sep="/") # Eric's proposed fix
> >
> > #lets double check the file does exist! # The rest here is Calum's
> > suggestion
> > if (!file.exists(full_filename)) {
> >   message("File missing")
> > } else {
> >   content <- read_docx(full_filename)
> >   docx_summary()
> >   # this reads docx for the full filename and
> >   # passes it ( |> command) to the next line
> >   # which summarises it.
> >   # the result is saved in a data frame object
> >   # called content which we shall show some
> >   # heading into from
> >
> >   head(content)
> > }
> >
> >
> > Running this, results in the error cited above.
> >
> > Thanks as always :-)
> >
> >
> >
> >
> > On 30/12/2023 11:58, Eric Berger wrote:
> >> full_filename <- paste(filepath, filename,sep="/")
> >
> >
>
> [[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.


Re: [R] Help request: Parsing docx files for key words and appending to a spreadsheet

2023-12-30 Thread Eric Berger
docx_summary(content)

You should read documentation e.g. ?docx_summary and check the examples
section

On Sat, Dec 30, 2023 at 2:12 PM Andy  wrote:

> Hi Eric
>
> Thanks for that. That seems to fix one problem (the lack of a separator),
> but introduces a new one when I complete the function Calum proposed:
> Error in docx_summary() : argument "x" is missing, with no default
>
> The whole code so far looks like this:
>
>
> # Load libraries
> library(tcltk)
> library(tidyverse)
> library(officer)
>
> filepath <- setwd(tk_choose.dir())
>
> filename <- "Now they want us to charge our electric cars from litter
> bins.docx"
> #full_filename <- paste0(filepath, filename) # Calum's original suggestion
>
> full_filename <- paste(filepath, filename, sep="/") # Eric's proposed fix
>
> #lets double check the file does exist! # The rest here is Calum's
> suggestion
> if (!file.exists(full_filename)) {
>   message("File missing")
> } else {
>   content <- read_docx(full_filename)
>   docx_summary()
>   # this reads docx for the full filename and
>   # passes it ( |> command) to the next line
>   # which summarises it.
>   # the result is saved in a data frame object
>   # called content which we shall show some
>   # heading into from
>
>   head(content)
> }
>
>
> Running this, results in the error cited above.
>
> Thanks as always :-)
>
>
>
>
> On 30/12/2023 11:58, Eric Berger wrote:
>
> full_filename <- paste(filepath, filename,sep="/")
>
>
>

[[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] Help request: Parsing docx files for key words and appending to a spreadsheet

2023-12-30 Thread Eric Berger
full_filename <- paste(filepath, filename,sep="/")

On Sat, Dec 30, 2023 at 1:45 PM Andy  wrote:

> Thanks Ivan and Calum
>
> I continue to appreciate your support.
>
> Calum, I entered the code snippet you provided, and it returns 'file
> missing'. Looking at this, while the object 'full_filename' exists, what
> is happening is that the path from getwd() is being appended to the
> title of the article, but without the '/' between the end of the path
> name (here 'TEST' and the name of the article. In other words,
> full_filename is reading "~/TESTNow they want us to charge our electric
> cars from litter bins.docx", so logically, this file doesn't exist. To
> work, the '/' needs to be inserted to differentiate between the end of
> the path name and the start of the article name. I've tried both paste0,
> as you suggested, and paste but neither do the trick.
>
> Is this a result of me using the tkinter folder selection that you
> remarked on? I wanted to keep that so that the selection is interactive,
> but if there are better ways of doing this I am open to suggestions.
>
> Thanks again, both.
>
> Best wishes
> Andrew
>
>
> On 29/12/2023 22:25, CALUM POLWART wrote:
> >
> >
> > help(read_docx) says that the function only imports one docx file. In
> > order to read multiple files, use a for loop or the lapply function.
> >
> >
> > I told you people will suggest better ways to loop!!
> >
> >
> >
> > docx_summary(read_docx("Now they want us to charge our electric cars
> > from litter bins.docx")) should work.
> >
> >
> > Ivan thanks for spotting my fail! Since the OP is new to all this I'm
> > going to suggest a little tweak to this code which we can then build
> > into a for loop:
> >
> > filepath <- getwd() #you will want to change this later. You are doing
> > something with tcl to pick a directory which seems rather fancy! But
> > keep doing it for now or set the directory here ending in a /
> >
> > filename <- "Now they want us to charge our electric cars from litter
> > bins.docx"
> >
> > full_filename <- paste0(filepath, filename)
> >
> > #lets double check the file does exist!
> > if (!file.exists(full_filename)) {
> >   message("File missing")
> > } else {
> >   content <- read_docx(full_filename) |>
> > docx_summary()
> > # this reads docx for the full filename and
> > # passes it ( |> command) to the next line
> > # which summarises it.
> > # the result is saved in a data frame object
> > # called content which we shall show some
> > # heading into from
> >
> >head(content)
> > }
> >
> > Let's get this bit working before we try and loop
> >
>
> [[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.


Re: [R] Code editor for writing R code

2023-11-29 Thread Eric Berger
Bert,
Posit (formerly RStudio) has moved from RMarkdown to Quarto. They
still support RMarkdown but major new features will be in Quarto. For
new users a better choice would be Quarto.
See https://quarto.org/docs/faq/rmarkdown.html

Secondly, the OP stated he was using the VS-Code IDE, so there is no
reason to point him to the Posit/RStudio IDE for this functionality
which VS-Code supports very well (as I noted in my response.)

Best,
Eric

On Wed, Nov 29, 2023 at 6:55 PM Christofer Bogaso
 wrote:
>
> Hi Sergei,
>
> Where can I find TeX Comments extension in VS Code?
>
> On Wed, Nov 29, 2023 at 9:34 PM Sergei Ko  wrote:
> >
> > TeX Comments extension in VS Code
> >
> >
> >
> >
> > Sent from my phone
> >
> >
> >  Original message 
> > From: Christofer Bogaso 
> > Date: Wed, 29 Nov 2023, 15:57
> > To: r-help 
> > Subject: [R] Code editor for writing R code
> >
> > Hi,
> >
> > Currently I use VS-Code to write codes in R. While it is very good, it
> > does not allow me to write Latex expressions in comments, which I am
> > willing to have to write corresponding mathematical expressions as
> > comments in my code files.
> >
> > Does there exist any Code editor for R, that allows me to write Latex
> > in comments?
> >
> > Any information will be appreciated.
> >
> > Thanks,
> >
> > __
> > 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.

__
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] Code editor for writing R code

2023-11-29 Thread Eric Berger
Another direction would be to replace your R script (.R file) with a
Quarto document (.qmd file).
VS-code has good support for Quarto and you could intersperse your
Latex with R chunks.


On Wed, Nov 29, 2023 at 6:15 PM Ben Bolker  wrote:
>
>
>
>Presumably there's nothing stopping you *writing* LaTeX in comments
> -- do you want a code editor that will render and display the LaTeX as
> you write? (Or am I misunderstanding something?)
>
>Does anyone do classic literate programming *sensu* Knuth any more?
> https://rpubs.com/bbolker/3153
> https://cran.r-project.org/web/packages/noweb/vignettes/noweb.pdf
>
>
> On 2023-11-29 10:57 a.m., Christofer Bogaso wrote:
> > Hi,
> >
> > Currently I use VS-Code to write codes in R. While it is very good, it
> > does not allow me to write Latex expressions in comments, which I am
> > willing to have to write corresponding mathematical expressions as
> > comments in my code files.
> >
> > Does there exist any Code editor for R, that allows me to write Latex
> > in comments?
> >
> > Any information will be appreciated.
> >
> > Thanks,
> >
> > __
> > 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.

__
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] annotate

2023-10-05 Thread Eric Berger
Another approach (still seems to require experimentation to get the value
'2') would be to replace the annotate() with

geom_text(data=above_92, aes(x=eruptions,y=waiting, label=waiting),
nudge_y=2)

At least this makes clear the reason for the magic number '2' in the code.

For details see
?geom_text


On Thu, Oct 5, 2023 at 10:38 AM Subia Thomas OI-US-LIV5 <
thomas.su...@draexlmaier.com> wrote:

> Colleagues,
>
> I wish to create y-data labels which meet a criterion.
>
> Here is my reproducible code.
> library(dplyr)
> library(ggplot2)
> library(cowplot)
>
> above_92 <- filter(faithful,waiting>92)
>
> ggplot(faithful,aes(x=eruptions,y=waiting))+
>   geom_point(shape=21,size=3,fill="orange")+
>   theme_cowplot()+
>   geom_hline(yintercept = 92)+
>
> annotate(geom="text",x=above_92$eruptions,y=above_92$waiting+2,label=above_92$waiting)
>
> A bit of trial and error is required to figure out what number to add or
> subtract to above_92$waiting.
>
> Is there a more efficient way to do this?
>
>
> Thomas Subia
> Lean Six Sigma Senior Practitioner
>
> DRÄXLMAIER Group
> DAA Draexlmaier Automotive of America LLC
>
> mailto:thomas.su...@draexlmaier.com
> http://www.draexlmaier.com
>
> "Nous croyons en Dieu.
> Tous les autres doivent apporter des données.
> Edward Deming
>
>
> Public: All rights reserved. Distribution to third parties allowed.
>
> __
> 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] [EXT] Theta from negative binomial regression and power_NegativeBinomiial from PASSED

2023-09-14 Thread Eric Berger
I asked your question to ChatGPT4

In statistics, what is the theta parameter of the negative binomial
distribution?

The response contained some math symbols but I am sending in plain
text mode so there may be some loss, hopefully not serious.

The negative binomial distribution is used to model the number of
successes before observing a fixed number of failures in a Bernoulli
process. It has two parameters, typically denoted r and p:

r - This is the number of failures until the experiment is stopped.
p - This is the probability of success on any given trial.

In some contexts, the negative binomial distribution is described in
terms of the number of failures before a fixed number of successes. In
this case:

r would be the number of successes.
p would still be the probability of success on any given trial.

In some literature and software implementations, the negative binomial
distribution's parameterization is provided in terms of r (or k) and θ
instead of r and p. Here:

r (or k) is the number of successes.
θ represents a "success-to-failure" ratio.

The relationship between p and θ is:  p = θ / (θ+1)

[i.e. p = theta / (theta + 1) ]

Conversely, θ = p / (1−p)

[i.e. theta = p / (1-p) ]

In summary, θ is a parameter that captures the success-to-failure
ratio in some parameterizations of the negative binomial distribution,
and it can be related back to the success probability p.


On Fri, Sep 15, 2023 at 5:06 AM Andrew Robinson via R-help
 wrote:
>
> Hi John,
>
> the negative binomial is a tricky one - there are several different 
> parameterisations and therefore different interpretations of the parameters.  
> Joseph Hilbe wrote a whole book on it that might be wroth checking.
>
> Cheers,
>
> Andrew
>
>
> --
> Andrew Robinson
> Chief Executive Officer, CEBRA and Professor of Biosecurity,
> School/s of BioSciences and Mathematics & Statistics
> University of Melbourne, VIC 3010 Australia
> Tel: (+61) 0403 138 955
> Email: a...@unimelb.edu.au
> Website: https://researchers.ms.unimelb.edu.au/~apro@unimelb/
>
> I acknowledge the Traditional Owners of the land I inhabit, and pay my 
> respects to their Elders.
> On 15 Sep 2023 at 11:52 AM +1000, Sorkin, John , 
> wrote:
> External email: Please exercise caution
>
> Colleagues,
>
> I want to use the power_NetativeBinomial function from the PASSED library. 
> The function requires a value for a parameter theta. The meaning of theta is 
> not given in the documentation (at least I can�t find it) of the function. 
> Further the descriptions of the negative binomial distribution that I am 
> familiar with do not mention theta as being a parameter of the distribution. 
> I noticed that when one runs the glm.nb function to perform a negative 
> binomial regression one obtains a value for theta. This leads to two questions
>
> 1. Is the theta required by the power_NetativeBinomial function the theta 
> that is produced by the glm.nb function
> 2. What is theta, and how does it relate to the parameters of the negative 
> binomial distribution?
>
> Thank you,
> John
>
> [[alternative HTML version deleted]]
>
>
> [[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] prop.trend.test

2023-09-07 Thread Eric Berger
You might want to consider exponential smoothing models such as Holt's
(Double Exponential Smoothing).
This method continually updates the trend parameter, and you can
monitor the most recent value (for sign, or magnitude, or both).

In R, some choices to fit the Holt model:

1. stats::HoltWinters() and set the gamma argument to FALSE for a
non-seasonal model
2. forecast::holt()

HTH,
Eric


On Fri, Sep 8, 2023 at 8:23 AM Thomas Subia via R-help
 wrote:
>
> Colleagues,
>
> Thanks all for the responses.
>
> I am monitoring the daily total number of defects per sample unit.
> I need to know whether this daily defect proportion is trending upward (a bad 
> thing for a manufacturing process).
>
> My first thought was to use either a u or a u' control chart for this.
> As far as I know, u or u' charts are poor to detect drifts.
>
> This is why I chose to use prop.trend.test to detect trends in proportions.
>
> While prop.trend.test can confirm the existence of a trend, as far as I know, 
> it is left to the user
> to determine what direction that trend is.
>
> One way to illustrate trending is of course to plot the data and use 
> geom_smooth and method lm
> For the non-statisticians in my group, I've found that using this method 
> along with the p-value of prop.trend.test, makes it easier for the users to 
> determine the existence of trending and its direction.
>
> If there are any other ways to do this, please let me know.
>
> Thomas Subia
>
>
>
>
>
>
>
>
>
>
>
>
> On Thursday, September 7, 2023 at 10:31:27 AM PDT, Rui Barradas 
>  wrote:
>
>
>
>
>
> Às 14:23 de 07/09/2023, Thomas Subia via R-help escreveu:
> >
> > Colleagues
> >
> >   Consider
> > smokers  <- c( 83, 90, 129, 70 )
> > patients <- c( 86, 93, 136, 82 )
> >
> >   prop.trend.test(smokers, patients)
> >
> >   Output:
> >
> >   Chi-squared Test for Trend inProportions
> >
> >   data:  smokers out of patients ,
> >
> > using scores: 1 2 3 4
> >
> > X-squared = 8.2249, df = 1, p-value = 0.004132
> >
> >   # trend test for proportions indicates proportions aretrending.
> >
> >   How does one identify the direction of trending?
> >   # prop.test indicates that the proportions are unequal but doeslittle to 
> > indicate trend direction.
> > All the best,
> > Thomas Subia
> >
> >
> > [[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.
> Hello,
>
> By visual inspection it seems that there is a decreasing trend.
> Note that the sample estimates of prop.test and smokers/patients are equal.
>
>
> smokers  <- c( 83, 90, 129, 70 )
> patients <- c( 86, 93, 136, 82 )
>
> prop.test(smokers, patients)$estimate
> #>prop 1prop 2prop 3prop 4
> #> 0.9651163 0.9677419 0.9485294 0.8536585
>
> smokers/patients
>
> #> [1] 0.9651163 0.9677419 0.9485294 0.8536585
>
> plot(smokers/patients, type = "b")
>
>
>
> Hope this helps,
>
> Rui Barradas
>
> __
> 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] Merge and replace data

2023-09-05 Thread Eric Berger
As Duncan points out, ifelse() provides a more general approach than
the specific pmax().

Even more generally, you might want to consider the apply() function
(and its relatives sapply(), lapply(), ...)

For example

apply(cbind(x1,x2), MAR=1, max)

In the above statement, x1 and x2 are combined into two columns, MAR=1
says apply the following function to each row,
and max specifies the function to be applied. The summary is that this
statement applies the max to each row in the object
specified by the first argument. It's very general because you can
replace max by an arbitrary function.

Similarly, if you get more into R you might modify the above using
chained operations. The following rewrites the above
by piping cbind(x1,x2) into the apply command. Not a necessary feature
to know but eventually will assist you in writing code that does
not require a lot of intermediate variables.

cbind(x1,x2) |> apply(MAR=1,max)

On Tue, Sep 5, 2023 at 2:45 PM Duncan Murdoch  wrote:
>
> On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > Hi all,
> >
> > I have these data
> >
> > x1 <- c(116,0,115,137,127,0,0)
> > x2 <- c(0,159,0,0,0,159,127)
> >
> > I want : xx <- c(116,115,137,127,159, 127)
> >
> > I would like to merge these data into one column. Whenever the data is '0'
> > it will be replaced by the value in the column which is non zero..
> > I tried append and merge but fail to get what I want.
> >
>
> Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2
> would give the same answer.
>
> The real question is what you want to do if both x1 and x2 have non-zero
> entries, or negative entries:
>
>pmax(x1, x2)
>
> will pick the larger one.  It might be zero if the other is negative,
> and that doesn't match your description.
>
>x1+x2
>
> will just give the sum, which doesn't match your description if both are
> non-zero.
>
> But maybe you want the x1 value unless it is zero, even if it is smaller
> than the x2 value:  then you would use
>
>ifelse(x1 != 0, x1, x2)
>
> Similarly
>
>ifelse(x2 != 0, x2, x1)
>
> would prefer the x2 value.
>
> You should think about what you would do in these other cases as well.
>
> Duncan Murdoch
>
> __
> 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] Merge and replace data

2023-09-05 Thread Eric Berger
xx <- pmax(x1,x2)

On Tue, Sep 5, 2023 at 11:56 AM roslinazairimah zakaria
 wrote:
>
> Hi all,
>
> I have these data
>
> x1 <- c(116,0,115,137,127,0,0)
> x2 <- c(0,159,0,0,0,159,127)
>
> I want : xx <- c(116,115,137,127,159, 127)
>
> I would like to merge these data into one column. Whenever the data is '0'
> it will be replaced by the value in the column which is non zero..
> I tried append and merge but fail to get what I want.
>
> --
> *Roslina Zakaria*
>
> [[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] Finding combination of states

2023-09-05 Thread Eric Berger
Hi Bert,
I really liked your solution.
In the spirit of code golf, I wondered if there is a shorter way to do
the regular expression test.
Kudos to my coding buddy GPT-4 for the following:

You can replace your statement

out[-grep(paste(paste0(states,states),  collapse = "|"),out)]

by

out[-grep("(.)\\1",out)]

Best,
Eric

On Tue, Sep 5, 2023 at 3:08 AM Bert Gunter  wrote:
>
> ... and just for fun, here is a non-string version (more appropriate for 
> complex state labels??):
>
> gvec <- function(ntimes, states, init, final, repeats = TRUE)
>## ntimes: integer, number of unique times
>## states: vector of unique states
>## init: initial state
>## final: final state
> {
>out <- cbind(init,
> as.matrix(expand.grid(rep(list(states),ntimes -2 ))),final)
>if(!repeats)
>  out[ apply(out,1,\(x)all(x[-1] != x[-ntimes])), ]
>else out
> }
>
> yielding:
>
>
> > gvec(4, letters[1:5], "b", "e", repeats = TRUE)
>   init Var1 Var2 final
>  [1,] "b"  "a"  "a"  "e"
>  [2,] "b"  "b"  "a"  "e"
>  [3,] "b"  "c"  "a"  "e"
>  [4,] "b"  "d"  "a"  "e"
>  [5,] "b"  "e"  "a"  "e"
>  [6,] "b"  "a"  "b"  "e"
>  [7,] "b"  "b"  "b"  "e"
>  [8,] "b"  "c"  "b"  "e"
>  [9,] "b"  "d"  "b"  "e"
> [10,] "b"  "e"  "b"  "e"
> [11,] "b"  "a"  "c"  "e"
> [12,] "b"  "b"  "c"  "e"
> [13,] "b"  "c"  "c"  "e"
> [14,] "b"  "d"  "c"  "e"
> [15,] "b"  "e"  "c"  "e"
> [16,] "b"  "a"  "d"  "e"
> [17,] "b"  "b"  "d"  "e"
> [18,] "b"  "c"  "d"  "e"
> [19,] "b"  "d"  "d"  "e"
> [20,] "b"  "e"  "d"  "e"
> [21,] "b"  "a"  "e"  "e"
> [22,] "b"  "b"  "e"  "e"
> [23,] "b"  "c"  "e"  "e"
> [24,] "b"  "d"  "e"  "e"
> [25,] "b"  "e"  "e"  "e"
> >
> > gvec(4, letters[1:5], "b", "e", repeats = FALSE)
>   init Var1 Var2 final
>  [1,] "b"  "c"  "a"  "e"
>  [2,] "b"  "d"  "a"  "e"
>  [3,] "b"  "e"  "a"  "e"
>  [4,] "b"  "a"  "b"  "e"
>  [5,] "b"  "c"  "b"  "e"
>  [6,] "b"  "d"  "b"  "e"
>  [7,] "b"  "e"  "b"  "e"
>  [8,] "b"  "a"  "c"  "e"
>  [9,] "b"  "d"  "c"  "e"
> [10,] "b"  "e"  "c"  "e"
> [11,] "b"  "a"  "d"  "e"
> [12,] "b"  "c"  "d"  "e"
> [13,] "b"  "e"  "d"  "e"
>
> :-)
>
> -- Bert
>
> On Mon, Sep 4, 2023 at 2:04 PM Bert Gunter  wrote:
>>
>> Well, if strings with repeats (as you defined them) are to be excluded, I 
>> think it's simple just to use regular expressions to remove them.
>>
>> e.g.
>> g <- function(ntimes, states, init, final, repeats = TRUE)
>>## ntimes: integer, number of unique times
>>## states: vector of unique states
>>## init: initial state
>>## final: final state
>> {
>> out <- do.call(paste0,c(init,expand.grid(rep(list(states), ntimes-2)), 
>> final))
>> if(!repeats)
>>out[-grep(paste(paste0(states,states),  collapse = "|"),out)]
>> else out
>> }
>> So:
>>
>> > g(4, LETTERS[1:5], "B", "E", repeats = FALSE)
>>  [1] "BCAE" "BDAE" "BEAE" "BABE" "BCBE" "BDBE" "BEBE" "BACE"
>>  [9] "BDCE" "BECE" "BADE" "BCDE" "B

Re: [R] Finding combination of states

2023-09-04 Thread Eric Berger
My initial response was buggy and also used a deprecated function.
Also, it seems possible that one may want to rule out any strings where the
same state appears consecutively.
I say that such a string has a repeat.

myExpand <- function(v, n) {
  do.call(tidyr::expand_grid, replicate(n, v, simplify = FALSE))
}

no_repeat <- function(s) {
  v <- unlist(strsplit(s, NULL))
  sum(v[-1]==v[-length(v)]) == 0
}

f <- function(states, nsteps, first, last, rm_repeat=TRUE) {
  if (nsteps < 3) stop("nsteps must be at least 3")
out <- paste(first,
  myExpand(states, nsteps-2) |>
apply(MAR=1, \(x) paste(x, collapse="")),
  last, sep="")
if (rm_repeat) {
  ok <- sapply(out, no_repeat)
  out <- out[ok]
}
out
}

f(LETTERS[1:5],4,"B","E")

#  [1] "BABE" "BACE" "BADE" "BCAE" "BCBE" "BCDE" "BDAE" "BDBE" "BDCE"
"BEAE" "BEBE" "BECE" "BEDE"

On Mon, Sep 4, 2023 at 10:33 PM Bert Gunter  wrote:

> Sorry, my last line should have read:
>
> If neither this nor any of the other suggestions is what is desired, I
> think the OP will have to clarify his query.
>
> Bert
>
> On Mon, Sep 4, 2023 at 12:31 PM Bert Gunter 
> wrote:
>
>> I think there may be some uncertainty here about what the OP requested.
>> My interpretation is:
>>
>> n different times
>> k different states
>> Any state can appear at any time in the vector of times and can be
>> repeated
>> Initial and final states are given
>>
>> So modifying Tim's expand.grid() solution a bit yields:
>>
>> g <- function(ntimes, states, init, final){
>>## ntimes: integer, number of unique times
>>## states: vector of unique states
>>## init: initial state
>>## final: final state
>> do.call(paste0,c(init,expand.grid(rep(list(states), ntimes-2)), final))
>> }
>>
>> e.g.
>>
>> > g(4, LETTERS[1:5], "B", "D")
>>  [1] "BAAD" "BBAD" "BCAD" "BDAD" "BEAD" "BABD" "BBBD" "BCBD"
>>  [9] "BDBD" "BEBD" "BACD" "BBCD" "BCCD" "BDCD" "BECD" "BADD"
>> [17] "BBDD" "BCDD" "BDDD" "BEDD" "BAED" "BBED" "BCED" "BDED"
>> [25] "BEED"
>>
>> If neither this nor any of the other suggestions is not what is desired,
>> I think the OP will have to clarify his query.
>>
>> Cheers,
>> Bert
>>
>> On Mon, Sep 4, 2023 at 9:25 AM Ebert,Timothy Aaron 
>> wrote:
>>
>>> Does this work for you?
>>>
>>> t0<-t1<-t2<-LETTERS[1:5]
>>> al2<-expand.grid(t0, t1, t2)
>>> al3<-paste(al2$Var1, al2$Var2, al2$Var3)
>>> al4 <- gsub(" ", "", al3)
>>> head(al3)
>>>
>>> Tim
>>>
>>> -Original Message-
>>> From: R-help  On Behalf Of Eric Berger
>>> Sent: Monday, September 4, 2023 10:17 AM
>>> To: Christofer Bogaso 
>>> Cc: r-help 
>>> Subject: Re: [R] Finding combination of states
>>>
>>> [External Email]
>>>
>>> The function purrr::cross() can help you with this. For example:
>>>
>>> f <- function(states, nsteps, first, last) {
>>>paste(first, unlist(lapply(purrr::cross(rep(list(v),nsteps-2)),
>>> \(x) paste(unlist(x), collapse=""))), last, sep="") } f(LETTERS[1:5], 3,
>>> "B", "E") [1] "BAE" "BBE" "BCE" "BDE" "BEE"
>>>
>>> HTH,
>>> Eric
>>>
>>>
>>> On Mon, Sep 4, 2023 at 3:42 PM Christofer Bogaso <
>>> bogaso.christo...@gmail.com> wrote:
>>> >
>>> > Let say I have 3 time points.as T0, T1, and T2.(number of such time
>>> > points can be arbitrary) In each time point, an object can be any of 5
>>> > states, A, B, C, D, E (number of such states can be arbitrary)
>>> >
>>> > I need to find all possible ways, how that object starting with state
>>> > B (say) at time T0, can be on state E (example) in time T2
>>> >
>>> > For example one possibility is BAE etc.
>>> >
>>> > Is there any function available with R, that can give me a vector of
>>> > such possibilities for arbitrary number of states, t

Re: [R] Finding combination of states

2023-09-04 Thread Eric Berger
The function purrr::cross() can help you with this. For example:

f <- function(states, nsteps, first, last) {
   paste(first, unlist(lapply(purrr::cross(rep(list(v),nsteps-2)),
\(x) paste(unlist(x), collapse=""))), last, sep="")
}
f(LETTERS[1:5], 3, "B", "E")
[1] "BAE" "BBE" "BCE" "BDE" "BEE"

HTH,
Eric


On Mon, Sep 4, 2023 at 3:42 PM Christofer Bogaso
 wrote:
>
> Let say I have 3 time points.as T0, T1, and T2.(number of such time
> points can be arbitrary) In each time point, an object can be any of 5
> states, A, B, C, D, E (number of such states can be arbitrary)
>
> I need to find all possible ways, how that object starting with state
> B (say) at time T0, can be on state E (example) in time T2
>
> For example one possibility is BAE etc.
>
> Is there any function available with R, that can give me a vector of
> such possibilities for arbitrary number of states, time, and for a
> given initial and final (desired) states?
>
> ANy pointer will be very appreciated.
>
> Thanks for your time.
>
> __
> 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] lag.plot showing only lines....

2023-08-28 Thread Eric Berger
Hi Akshay,
I have never used this function before but according to ?lag.plot you
simply need to pass the argument do.lines=FALSE.

HTH,
Eric


On Mon, Aug 28, 2023 at 5:45 PM akshay kulkarni  wrote:
>
> Dear members,
>  I am using the following code:
>
> lag.plot(ygrpch(OHLCData[[i]]),lags=4)
>
> But instead of dots, it is showing lines, which I don't know how to 
> interpret. How to make it plot dots? This is working properly:
>
> lag.plot(na_interpolation(OHLCData[[i]][,4]),lags=4)
>
> I am not attaching the graph here because it would not fit properly. If you 
> want to have a look at the graph, how do i send it?
>
> Thanking you,
> 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.

__
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] Help needed with olsrr package

2023-08-22 Thread Eric Berger
I copied your data and ran your code.
It worked fine for me.

> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;
 LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
LC_ADDRESS=C   LC_TELEPHONE=C
LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: Asia/Jerusalem
tzcode source: system (glibc)

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] olsrr_0.5.3

loaded via a namespace (and not attached):
 [1] vctrs_0.6.3   cli_3.6.1 rlang_1.1.1   car_3.1-2
  generics_0.1.3data.table_1.14.8 glue_1.6.2
colorspace_2.1-0  gridExtra_2.3
[10] scales_1.2.1  fansi_1.0.4   grid_4.3.1
carData_3.0-5 munsell_0.5.0 tibble_3.2.1  abind_1.4-5
 lifecycle_1.0.3   compiler_4.3.1
[19] goftest_1.2-3 dplyr_1.1.2   Rcpp_1.0.11
pkgconfig_2.0.3   rstudioapi_0.15.0 nortest_1.0-4 R6_2.5.1
 tidyselect_1.2.0  utf8_1.2.3
[28] pillar_1.9.0  magrittr_2.0.3tools_4.3.1
gtable_0.3.3  ggplot2_3.4.2
>

HTH,
Eric


On Tue, Aug 22, 2023 at 7:47 PM Ivan Krylov  wrote:
>
> В Tue, 22 Aug 2023 16:06:22 +0530
> Ashim Kapoor  пишет:
>
> > Error in eval(predvars, data, env) : object 'Var.One' not found
>
> Use traceback() to find out in which function the error was raised.
> This looks like a bug in the olsrr package. Could be due to use of
> string manipulation in order to work with formula terms, could be
> some other assumption violated by I(10*Var2).
>
> Try asking at https://github.com/rsquaredacademy/olsrr/issues.
>
> --
> Best regards,
> Ivan
>
> __
> 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] time series transformation....

2023-08-14 Thread Eric Berger
Hi Akshay,
If you do
> help(package="forecast")

you will find that arfima is listed. Unfortunately, I don't see any
ARCH-type models there, but you should check out the help page
results.

Good luck,
Eric

On Mon, Aug 14, 2023 at 8:04 PM akshay kulkarni  wrote:
>
> Dear Eric,
>  THanks for the reply...will this also work with arfima() ? 
> What if I want to fit a model like GARCH and friends?
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> ____
> From: Eric Berger 
> Sent: Sunday, August 13, 2023 10:34 AM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] time series transformation
>
> Hi Akshay,
> The forecast package will do the BoxCox transform and automatically
> backtransform the forecasts.
> The package also handles xts objects.
> For example, modifying the example from the help page of
> forecast::forecast for Arima
>
> > dt <- as.Date("2023-01-01") + 1:length(WWWusage)
> > a <- xts(WWWusage, order.by=dt)
> > fit1 <- Arima(a, c(3,1,0))
> > fit2 <- Arima(a, lambda=0.5, c(3,1,0))  ## applies the Box-Cox transform 
> > with lambda=0.5
> > par(mfrow=c(1,2))
> > plot(forecast(fit1))
> > plot(forecast(fit2))
>
> HTH,
> Eric
>
> p.s. RJH is the author/maintainer of the forecast package
>
>
> On Sun, Aug 13, 2023 at 1:01 AM akshay kulkarni  wrote:
> >
> > dear members,
> >  I have a heteroscedastic time series which I want 
> > to transform to make it homoscedastic by a box cox transformation. I am 
> > using Otexts by RJ hyndman and George Athanopolous as my textbook. They 
> > discuss transformation and also say the fpp3 and the fable package 
> > automatically back transforms the point forecast. they also discuss the 
> > process which I find to be very cumbersome. Is there any R package which 
> > automatically back transforms the point forecast when I use xts objects ( 
> > RJH and GA use tsibble objects) with arfima/arima in the forecast package?
> >
> > THanking you,
> > 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.

__
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] time series transformation....

2023-08-12 Thread Eric Berger
Hi Akshay,
The forecast package will do the BoxCox transform and automatically
backtransform the forecasts.
The package also handles xts objects.
For example, modifying the example from the help page of
forecast::forecast for Arima

> dt <- as.Date("2023-01-01") + 1:length(WWWusage)
> a <- xts(WWWusage, order.by=dt)
> fit1 <- Arima(a, c(3,1,0))
> fit2 <- Arima(a, lambda=0.5, c(3,1,0))  ## applies the Box-Cox transform with 
> lambda=0.5
> par(mfrow=c(1,2))
> plot(forecast(fit1))
> plot(forecast(fit2))

HTH,
Eric

p.s. RJH is the author/maintainer of the forecast package


On Sun, Aug 13, 2023 at 1:01 AM akshay kulkarni  wrote:
>
> dear members,
>  I have a heteroscedastic time series which I want to 
> transform to make it homoscedastic by a box cox transformation. I am using 
> Otexts by RJ hyndman and George Athanopolous as my textbook. They discuss 
> transformation and also say the fpp3 and the fable package automatically back 
> transforms the point forecast. they also discuss the process which I find to 
> be very cumbersome. Is there any R package which automatically back 
> transforms the point forecast when I use xts objects ( RJH and GA use tsibble 
> objects) with arfima/arima in the forecast package?
>
> THanking you,
> 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.

__
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] Merge with closest (not equal) time stamps

2023-08-08 Thread Eric Berger
Hi Naresh,
Perhaps the below is faster than your approach

library(dplyr)
library(tidyr)
merge(option.trades, stock.trades, by="timestamp", all=TRUE) |>
  dplyr::arrange(timestamp) |>
  dplyr::mutate(stock.timestamp =
as.POSIXct(ifelse(is.na(option.price), timestamp, NA))) |>
  tidyr::fill(stock.price, stock.timestamp) |>
  dplyr::filter(!is.na(option.price)) |>
  dplyr::select(1,2,4,3) ->
  myres2

identical(myres, myres2)  ## TRUE


On Tue, Aug 8, 2023 at 5:32 AM Naresh Gurbuxani
 wrote:
>
>
> I have two dataframes, each with a column for timestamp.  I want to
> merge the two dataframes such that each row from first dataframe
> is matched with the row in the second dataframe with most recent but
> preceding timestamp. Here is an example.
>
> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
> 1.8))
>
> stock.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:21", 
> "2023-08-07 10:23:34", "2023-08-07 10:24:57", "2023-08-07 10:28:37", 
> "2023-08-07 10:29:01")), stock.price = c(102.2, 102.9, 103.1, 101.8, 101.7))
>
> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>
> library(plyr)
> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
> tail(subset(stock.trades, timestamp <= tstamp), 1))
> names(mystock.prices)[1] <- "stock.timestamp"
> myres <- cbind(option.trades, mystock.prices)
>
> This method works. But for large dataframes, it is very slow.  Is there
> a way to speed up the merge?
>
> Thanks,
> Naresh
>
> __
> 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] Stacking matrix columns

2023-08-06 Thread Eric Berger
Avi,

I was not trying to provide the most economical solution. I was trying
to anticipate that people (either the OP or others searching for how
to stack columns of a matrix) might be motivated by calculations in
multilinear algebra, in which case they might be interested in the
rTensor package.



On Sun, Aug 6, 2023 at 6:16 PM  wrote:
>
> Eric,
>
> I am not sure your solution is particularly economical albeit it works for 
> arbitrary arrays of any dimension, presumably. But it seems to involve 
> converting a matrix to a tensor just to undo it back to a vector. Other 
> solutions offered here, simply manipulate the dim attribute of the data 
> structure.
>
> Of course, the OP may have uses in mind which the package might make easier. 
> We often get fairly specific questions here without the additional context 
> that may help guide a better answer.
>
> -Original Message-
> From: R-help  On Behalf Of Eric Berger
> Sent: Sunday, August 6, 2023 3:34 AM
> To: Bert Gunter 
> Cc: R-help Mailing List ; Steven Yen 
> Subject: Re: [R] Stacking matrix columns
>
> Stacking columns of a matrix is a standard operation in multilinear
> algebra, usually written as the operator vec().
> I checked to see if there is an R package that deals with multilinear
> algebra. I found rTensor, which has a function vec().
> So, yet another way to accomplish what you want would be:
>
> > library(rTensor)
> > vec(as.tensor(x))
>
> Eric
>
>
> On Sun, Aug 6, 2023 at 5:05 AM Bert Gunter  wrote:
> >
> > Or just dim(x) <- NULL.
> > (as matrices in base R are just vectors with a dim attribute stored in
> > column major order)
> >
> > ergo:
> >
> > > x
> >  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> > > x<- 1:20  ## a vector
> > > is.matrix(x)
> > [1] FALSE
> > > dim(x) <- c(5,4)
> > > is.matrix(x)
> > [1] TRUE
> > > attributes(x)
> > $dim
> > [1] 5 4
> >
> > > ## in painful and unnecessary detail as dim() should be used instead
> > > attr(x, "dim") <- NULL
> > > is.matrix(x)
> > [1] FALSE
> > > x
> >  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> >
> > ## well, you get it...
> >
> > -- Bert
> >
> > On Sat, Aug 5, 2023 at 5:21 PM Iris Simmons  wrote:
> > >
> > > You could also do
> > >
> > > dim(x) <- c(length(x), 1)
> > >
> > > On Sat, Aug 5, 2023, 20:12 Steven Yen  wrote:
> > >
> > > > I wish to stack columns of a matrix into one column. The following
> > > > matrix command does it. Any other ways? Thanks.
> > > >
> > > >  > x<-matrix(1:20,5,4)
> > > >  > x
> > > >   [,1] [,2] [,3] [,4]
> > > > [1,]16   11   16
> > > > [2,]27   12   17
> > > > [3,]38   13   18
> > > > [4,]49   14   19
> > > > [5,]5   10   15   20
> > > >
> > > >  > matrix(x,ncol=1)
> > > >[,1]
> > > >   [1,]1
> > > >   [2,]2
> > > >   [3,]3
> > > >   [4,]4
> > > >   [5,]5
> > > >   [6,]6
> > > >   [7,]7
> > > >   [8,]8
> > > >   [9,]9
> > > > [10,]   10
> > > > [11,]   11
> > > > [12,]   12
> > > > [13,]   13
> > > > [14,]   14
> > > > [15,]   15
> > > > [16,]   16
> > > > [17,]   17
> > > > [18,]   18
> > > > [19,]   19
> > > > [20,]   20
> > > >  >
> > > >
> > > > __
> > > > 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.
> >
> > __
> > 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.
>

__
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] Stacking matrix columns

2023-08-06 Thread Eric Berger
Stacking columns of a matrix is a standard operation in multilinear
algebra, usually written as the operator vec().
I checked to see if there is an R package that deals with multilinear
algebra. I found rTensor, which has a function vec().
So, yet another way to accomplish what you want would be:

> library(rTensor)
> vec(as.tensor(x))

Eric


On Sun, Aug 6, 2023 at 5:05 AM Bert Gunter  wrote:
>
> Or just dim(x) <- NULL.
> (as matrices in base R are just vectors with a dim attribute stored in
> column major order)
>
> ergo:
>
> > x
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> > x<- 1:20  ## a vector
> > is.matrix(x)
> [1] FALSE
> > dim(x) <- c(5,4)
> > is.matrix(x)
> [1] TRUE
> > attributes(x)
> $dim
> [1] 5 4
>
> > ## in painful and unnecessary detail as dim() should be used instead
> > attr(x, "dim") <- NULL
> > is.matrix(x)
> [1] FALSE
> > x
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>
> ## well, you get it...
>
> -- Bert
>
> On Sat, Aug 5, 2023 at 5:21 PM Iris Simmons  wrote:
> >
> > You could also do
> >
> > dim(x) <- c(length(x), 1)
> >
> > On Sat, Aug 5, 2023, 20:12 Steven Yen  wrote:
> >
> > > I wish to stack columns of a matrix into one column. The following
> > > matrix command does it. Any other ways? Thanks.
> > >
> > >  > x<-matrix(1:20,5,4)
> > >  > x
> > >   [,1] [,2] [,3] [,4]
> > > [1,]16   11   16
> > > [2,]27   12   17
> > > [3,]38   13   18
> > > [4,]49   14   19
> > > [5,]5   10   15   20
> > >
> > >  > matrix(x,ncol=1)
> > >[,1]
> > >   [1,]1
> > >   [2,]2
> > >   [3,]3
> > >   [4,]4
> > >   [5,]5
> > >   [6,]6
> > >   [7,]7
> > >   [8,]8
> > >   [9,]9
> > > [10,]   10
> > > [11,]   11
> > > [12,]   12
> > > [13,]   13
> > > [14,]   14
> > > [15,]   15
> > > [16,]   16
> > > [17,]   17
> > > [18,]   18
> > > [19,]   19
> > > [20,]   20
> > >  >
> > >
> > > __
> > > 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.
>
> __
> 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] Technical Help Request for "Version Differences" (i.e., CYTOFKIT package)

2023-08-04 Thread Eric Berger
Hi Murat,
I am not sure why you are using R 3.5.
CRAN has R 4.3.1

https://cran.r-project.org/bin/windows/base/

I suggest you start over and install R 4.3.1 and then your cytofkit
install might work.

Good luck,
Eric


On Fri, Aug 4, 2023 at 5:47 PM Murat DELMAN  wrote:
>
> Dear Ms./Mr.,
>
>
> I sent an email to this email address from my institutional address
> muratdel...@iyte.edu.tr, but could not make sure if my email reached you
> because sometimes emails are not delivered to the target due to
> server issues and country-related limitations. I wanted to send you the
> same email via my Gmail account.
>
>
>
>
> If the email text and codes below are not properly displayed, you can
> download the attached Word file, which has exactly the same content.
>
>
>
> I am a cytometrist and microscopist at Izmir Institute of Technology,
> Integrated Research Center. I operate a cytometer and fluorescence
> microscope to acquire data from our researchers’ samples and then give them
> analyzed data. I need to dive into bulk data to present all of the
> information in the sample. To do this, I have recently been trying to
> develop my skills in R, RStudio, and Cytofkit package for cytometry (flow,
> spectral, mass, imaging mass cytometry) data analysis. I am new in this
> area and must learn R for data analysis.
>
>
>
>
>
> May I request help from you or one of your assistants who is competent in
> R, RStudio, and R packages with the installation issues caused by “*version
> differences*” between R itself, R packages, and their dependencies? I
> really had to send this email to you anymore, because I could not run the
> package “Cytofkit” and find a solution on the internet.
>
>
> It has been nearly 2 weeks since I have been trying to solve the errors and
> warnings. I am in a vicious circle and cannot move forward anymore. This is
> why I needed help from an expert competent in R programming.
>
>
>
> I tried to use the renv package, but I could not move forward because I am
> not competent in R.
>
>
> I explained the errors that I faced during the installation below.
>
>
>
> I have two computers, both operating Windows 10, 64-bit.
>
>
>
> I installed R 3.5.*2* on the desktop (Windows in English) and R 3.5.*0* on
> the laptop (Windows in Turkish).
>
>
>
> I know that you are very busy with your work, but may you (or your
> assistant) help me by informing me *how to install consistent versions of
> R, RStudio, and packages/dependencies* (for example, Cytofkit, ggplot2,
> Rtools, devtools, plyr, shiny, GUI)?
>
>
>
> I guess I will need exact web page links "*in* *an orderly manner" to
> download* the correct versions of everything.
>
>
>
> I would appreciate it if you could advise me or forward this email to one
> of your assistants or experts.
>
>
>
>
> I really appreciate any help you can provide.
>
>
>
>
> Kind Regards
>
>
>
>
> *Please find the technical details below;*
>
>
>
>
>
> I used the installation order listed below;
>
>
>
> I downloaded R 3.5.0 and R 3.5.2 using the link and directory below.
>
>
>
> Link: https://www.freestatistics.org/cran/
>
>
>
> Directory: Download R for Windows > install R for the first time > Previous
> releases > R 3.5.2 (December, 2018) or R 3.5.0
>
>
>
> I downloaded RStudio from the link below;
>
>
>
> https://posit.co/download/rstudio-desktop/
>
>
>
>
> I downloaded packages via either
> https://cran.rstudio.com/bin/windows/contrib/r-devel/ or the “install
> function” tab in RStudio.
>
>
>
> Rtools : https://cran.r-project.org/bin/windows/Rtools/
>
>
>
> Devtools : https://cran.r-project.org/web/packages/devtools/index.html
>
>
>
> ggplot2 : https://cran.r-project.org/web/packages/ggplot2/index.html
>
>
>
>  https://ggplot2.tidyverse.org/
>
>
>
>  https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf
>
>
>
>
>
> plyr : https://cran.r-project.org/web/packages/plyr/index.html
>
>
>
> shiny : https://cran.r-project.org/web/packages/shiny/index.html
>
>
>
>
> https://shiny.posit.co/r/getstarted/shiny-basics/lesson1/index.html
>
>
>
> https://github.com/rstudio/shiny
>
>
>
> cytofkit : https://github.com/JinmiaoChenLab/cytofkit
>
>
>
>
> https://bioconductor.riken.jp/packages/3.3/bioc/html/cytofkit.html
>
>
>
> https://journals.plos.org/ploscompbiol/article/file?id=10.1371/journal.pcbi.1005112=printable
>
>
>
>
> Below, I provide the errors and warnings that I faced for 2 weeks for both
> R versions;
>
>
>
>
>
> *Please find the errors and warnings below;*
>
>
>
>
> *CYTOFKIT FOR R VERSION 3.5.**2*
>
>
>
>
>
> > library(cytofkit)
>
> Loading required package: ggplot2
>
> Loading required package: plyr
>
> Error: package or namespace load failed for ‘cytofkit’:
>
> package ‘cytofkit’ was installed by an R version with different internals;
> it needs to be reinstalled for use with this R version
>
> ERROR: this R is version 3.5.2, package 'cytofkit2' requires R >= 4.0.0
>
> In R CMD INSTALL
>
> Installation paths not writeable, 

Re: [R] OK, next Q - a sort of factorial on a vector

2023-06-20 Thread Eric Berger
Hi Philip,
In the decades since you learned R there have been some additions to the
language.
In particular, R now supports lambda functions.
Applying this feature to Ivan's beautiful solution cuts down 7 characters
(continuing his golfing analogy)

unlist(lapply(seq_along(x), \(i) x[i] * x[-(1:i)]))

Enjoy your return to R!


On Tue, Jun 20, 2023 at 8:46 PM Philip Rhoades via R-help <
r-help@r-project.org> wrote:

> Ivan,
>
>
> On 2023-06-21 03:32, Ivan Krylov wrote:
> > В Wed, 21 Jun 2023 03:13:52 +1000
> > Philip Rhoades via R-help  пишет:
> >
> >> This:
> >>
> >>!(1,2,3,4,5)
> >>
> >> would give this:
> >>
> >>(2,3,4,5, 6,8,10, 12,15, 20)
> >
> > Do you mean taking a product of every element of the vector with all
> > following vector elements? A relatively straightforward way would be
> > (given your vector stored in `x`):
> >
> > unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)]))
>
>
> Perfect!
>
>
> > (I'm sure it could be golfed further.)
>
>
> I will look at Sarah's suggestion too.
>
>
> >> and this:
> >>
> >>!(1,2,NA,4,5)
> >>
> >> would give this:
> >>
> >>(2,4,5, 8,10, 20)
> >
> > The previous solution seems to give your vector interspersed a bunch of
> > NAs, so one way to continue would be to filter it using v[!is.na(v)].
>
>
> Exactly!
>
> Thanks people - it would have taken forever to work that out myself (it
> has been decades since I looked at R).
>
> Phil.
> --
> Philip Rhoades
>
> PO Box 896
> Cowra  NSW  2794
> Australia
> E-mail:  p...@pricom.com.au
>
> __
> 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] Feature selection for multiclass classification

2023-06-16 Thread Eric Berger
you might want to search bioconductor

https://www.bioconductor.org/

https://bioconductor.org/books/3.13/OSCA.basic/feature-selection.html

On Fri, Jun 16, 2023 at 9:04 AM Andrew Zhang 
wrote:

> Hello everyone, can someone share a list of packages that can do feature
> selection for multiclass (more than binary) classification? I have large
> genomic datasets with thousands of genes that I'm trying to use for cancer
> type (>10) classification, I am wondering if there are any specific
> packages/methods for feature selection prior to building a classification
> model using machine learning models.
>
> Thanks,
>
> Andrew
>
> [[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.


Re: [R] Problem with filling dataframe's column

2023-06-13 Thread Eric Berger
Hi Javed,
grep returns the positions of the matches. See an example below.

> v <- c("abc", "bcd", "def")
> v
[1] "abc" "bcd" "def"
> grep("cd",v)
[1] 2
> w <- v[-grep("cd",v)]
> w
[1] "abc" "def"
>


On Tue, Jun 13, 2023 at 8:50 AM javad bayat  wrote:
>
> Dear Rui;
> Hi. I used your codes, but it seems it didn't work for me.
>
> > pat <- c("_esmdes|_Des Section|0")
> > dim(data2)
> [1]  281549  9
> > grep(pat, data2$Layer)
> > dim(data2)
> [1]  281549  9
>
> What does grep function do? I expected the function to remove 3 rows of the
> dataframe.
> I do not know the reason.
>
>
>
>
>
>
> On Mon, Jun 12, 2023 at 5:16 PM Rui Barradas  wrote:
>
> > Às 23:13 de 12/06/2023, javad bayat escreveu:
> > > Dear Rui;
> > > Many thanks for the email. I tried your codes and found that the length
> > of
> > > the "Values" and "Names" vectors must be equal, otherwise the results
> > will
> > > not be useful.
> > > For some of the characters in the Layer column that I do not need to be
> > > filled in the LU column, I used "NA".
> > > But I need to delete some of the rows from the table as they are useless
> > > for me. I tried this code to delete entire rows of the dataframe which
> > > contained these three value in the Layer column: It gave me the following
> > > error.
> > >
> > >> data3 = data2[-grep(c("_esmdes","_Des Section","0"), data2$Layer),]
> > >   Warning message:
> > >In grep(c("_esmdes", "_Des Section", "0"), data2$Layer) :
> > >argument 'pattern' has length > 1 and only the first element will
> > be
> > > used
> > >
> > >> data3 = data2[!grepl(c("_esmdes","_Des Section","0"), data2$Layer),]
> > >  Warning message:
> > >  In grepl(c("_esmdes", "_Des Section", "0"), data2$Layer) :
> > >  argument 'pattern' has length > 1 and only the first element will be
> > > used
> > >
> > > How can I do this?
> > > Sincerely
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > On Sun, Jun 11, 2023 at 5:03 PM Rui Barradas 
> > wrote:
> > >
> > >> Às 13:18 de 11/06/2023, Rui Barradas escreveu:
> > >>> Às 22:54 de 11/06/2023, javad bayat escreveu:
> >  Dear Rui;
> >  Many thanks for your email. I used one of your codes,
> >  "data2$LU[which(data2$Layer == "Level 12")] <- "Park"", and it works
> >  correctly for me.
> >  Actually I need to expand the codes so as to consider all "Levels" in
> > >> the
> >  "Layer" column. There are more than hundred levels in the Layer
> > column.
> >  If I use your provided code, I have to write it hundred of time as
> > >> below:
> >  data2$LU[which(data2$Layer == "Level 1")] <- "Park";
> >  data2$LU[which(data2$Layer == "Level 2")] <- "Agri";
> >  ...
> >  ...
> >  ...
> >  .
> >  Is there any other way to expand the code in order to consider all of
> > >> the
> >  levels simultaneously? Like the below code:
> >  data2$LU[which(data2$Layer == c("Level 1","Level 2", "Level 3", ...))]
> > >> <-
> >  c("Park", "Agri", "GS", ...)
> > 
> > 
> >  Sincerely
> > 
> > 
> > 
> > 
> >  On Sun, Jun 11, 2023 at 1:43 PM Rui Barradas 
> >  wrote:
> > 
> > > Às 21:05 de 11/06/2023, javad bayat escreveu:
> > >> Dear R users;
> > >> I am trying to fill a column based on a specific value in another
> > >> column
> > > of
> > >> a dataframe, but it seems there is a problem with the codes!
> > >> The "Layer" and the "LU" are two different columns of the dataframe.
> > >> How can I fix this?
> > >> Sincerely
> > >>
> > >>
> > >> for (i in 1:nrow(data2$Layer)){
> > >>  if (data2$Layer == "Level 12") {
> > >>  data2$LU == "Park"
> > >>  }
> > >>  }
> > >>
> > >>
> > >>
> > >>
> > > Hello,
> > >
> > > There are two bugs in your code,
> > >
> > > 1) the index i is not used in the loop
> > > 2) the assignment operator is `<-`, not `==`
> > >
> > >
> > > Here is the loop corrected.
> > >
> > > for (i in 1:nrow(data2$Layer)){
> > >  if (data2$Layer[i] == "Level 12") {
> > >data2$LU[i] <- "Park"
> > >  }
> > > }
> > >
> > >
> > >
> > > But R is a vectorized language, the following two ways are the
> > idiomac
> > > ways of doing what you want to do.
> > >
> > >
> > >
> > > i <- data2$Layer == "Level 12"
> > > data2$LU[i] <- "Park"
> > >
> > > # equivalent one-liner
> > > data2$LU[data2$Layer == "Level 12"] <- "Park"
> > >
> > >
> > >
> > > If there are NA's in data2$Layer it's probably safer to use ?which()
> > in
> > > the logical index, to have a numeric one.
> > >
> > >
> > >
> > > i <- which(data2$Layer == "Level 12")
> > > data2$LU[i] <- "Park"
> > >
> > > # equivalent one-liner
> > > data2$LU[which(data2$Layer == "Level 12")] <- "Park"

Re: [R] inconsistency in mclapply.....

2023-06-09 Thread Eric Berger
Hi Akshay,
You do not have to quit R to run 'top'. You can have, for example, 2
windows, with R running in one and top running in the oher.

Eric

On Sat, Jun 10, 2023 at 12:19 AM akshay kulkarni 
wrote:

> Dear Ivan,
>   Thanks for the reply. I am pressurised by a fast
> approaching deadline and your reply calmed me...
>
> Take a look at the following code:
>
>
> debug at #26: LYG <- mclapply(LYGH, FUN = arfima, mc.cores = 2,
> mc.preschedule = FALSE)
> Browse[2]> length(LYGH)
> [1] 357
> Browse[2]>
> ^C
>
> Browse[2]> LYG <- pbmclapply(LYGH,FUN = arfima,mc.cores = 2,mc.preschedule
> = FALSE)
>   |
>
>  |   0%, ETA NA
>
> I am debuuging a function FUN wherein the above expressions appear. The
> pbmclapply code works well if called inside FUN:
> > FUN(arg)
> Result..
>
> But as you may note, it doesn't work while in debug mode
>
> Also, if I replace pbmclapply by maclapply inside FUN, it hangs
>
> You might be interested in this:
>
> [ec2-user@ip-172-31-15-116 ~]$ exit
> logout
> There are stopped jobs.
>
> THis occurs when I close R and try to exit the shell prompt( I am on an
> AWS EC2 RHEL 8 Instance). Can this lead you somewhere? As of now I have
> quit R in my machine, so I can't get session info..but please let me know
> if you need it necessarily...
>
> by the by, how do you run top when running R? I think at least in my
> machine, you have to quit R to get to the shell prompt...
>
> I request you TO PLEASE reply to this mail as early as possible. I am
> facing an imminent deadline...please excuse my blatant violation of
> protocol, but deadlines are deadlines, right?
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
> 
> From: Ivan Krylov 
> Sent: Saturday, June 10, 2023 1:43 AM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] inconsistency in mclapply.
>
> On Fri, 9 Jun 2023 18:01:44 +
> akshay kulkarni  wrote:
>
> > > LYG <- pbmclapply(LYGH,FUN = arfima,mc.cores = 2,mc.preschedule =
> > > FALSE)
> >   |
> >
>  |
> >   0%, ETA NA^
> >
> > It just hangs.
>
> My questions from the last time still stand:
>
> 0) What is your sessionInfo()? Maybe you're running a parallel BLAS
> which doesn't always handle fork() or something. It may be worth
> disabling BLAS-level parallelism as long as you're already trying to
> use 100% of your processor by other means.
>
> 1) What does traceback() show after you interrupt pbmclapply? Most
> likely, you would be interrupting selectChildren(), but if not, the
> problem may lie in a very different place from what I'm expecting.
>
> 2) While pbmclapply is hung, without interrupting it, take a look at
> the state of the system and the processes on it (are you still on RHEL?
> use `top` or whatever task manager you're comfortable with).
>
>  a) Is 100% of the CPU being used? 100% of one core? Is system mostly
>  idle?
>
>  b) Can you find the child processes launched by pbmclapply?
>
>  c) Write down the PID of the child process and attach a debugger to
>  it (If you're on RHEL, try following this guide:
>  . If GDB asks you to install
>  additional debug symbols by running debuginfo-install, follow its
>  guidance and then restart GDB.) and obtain a backtrace. (In GDB, the
>  command to obtain a backtrace is "backtrace".) Which function is the
>  child process stuck in?
>
> --
> Best regards,
> Ivan
>
> [[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.


Re: [R] plot level, velocity, acceleration with one x axis

2023-05-31 Thread Eric Berger
I sent you an updated response to deal with the redundant copies of the x-axis.
Re-sending.

par(mfrow=c(3,1))
plot(DAX.[, 1], log='y', ylab='DAX', xaxt="n")
plot(DAX.[, 2], ylab='vel (%)', xaxt="n")
plot(DAX.[, 3], ylab='accel (%)')


On Wed, May 31, 2023 at 4:27 PM Spencer Graves
 wrote:
>
>
>
> On 5/30/23 10:23 AM, Eric Berger wrote:
> > What if you just precede these commands as follows:
> >
> > par(mfrow=c(3,1))
> > plot(DAX.[, 1], log='y', ylab='DAX')
> > plot(DAX.[, 2], ylab='vel (%)')
> > plot(DAX.[, 3], ylab='accel (%)')
>
>   Most of the space is consumed with two extraneous copies of the 
> axis.
>   We can get around that with three calls to par:
>
>
> op <- par(mfrow=c(3,1), mar=c(0, 4.1, 4.1, 2.1))
> plot(DAX.[, 1], log='y', ylab='DAX')
> par(mar=c(0, 4.1, 0, 2.1))
> plot(DAX.[, 2], ylab='vel (%)')
> par(mar=c(5.1, 4.1, 0, 2.1))
> plot(DAX.[, 3], ylab='accel (%)')
> par(op)
>
>
>   However, the three panels are NOT equal in size:  roughly 30% vs. 
> 44%
> vs. 26%.  I can get closer using layout, but if I change the aspect
> ratio, it changes the relationship between the heights of the three
> panels.
>
>
>   That's the problem I'm trying to solve.  It's also why it makes 
> sense
> to me to modify plot.ts to accept a vector for the log argument, with
> the constraint that length(lot) = either 1 or ncol(x).
>
>
>   There may be a way to do it using gglot2 / the tidyverse, but I'm 
> not
> facile with that, and my web searches have so far failed to produce
> anything better than modifying plot.ts.R (and then submitting such with
> compatible changes to plot.ts.Rd), as I suggested earlier.
>
>
>   ???
>   Thanks,
>   Spencer
>
> >
> > On Tue, May 30, 2023 at 5:45 PM Spencer Graves
> >  wrote:
> >>
> >>
> >>
> >> On 5/30/23 8:48 AM, Eric Berger wrote:
> >>> I am a bit confused as to what you are trying to achieve - and  even
> >>> if I could guess it is not clear what the interpretation would be.
> >>>> head(DAX)
> >>> 1628.75 1613.63 1606.51 1621.04 1618.16 1610.61
> >>>
> >>> Including the leading NA's, what would be the 6 leading terms of the 3
> >>> series that you want to plot,
> >>> and what would be the Y labels that you want to appear at those levels
> >>> (assuming that there was a
> >>> Y label for each of them - just to understand the units you are talking 
> >>> about)
> >>
> >>
> >> DAX <- EuStockMarkets[, 'DAX']
> >> DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX
> >> colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> >> head(DAX.)
> >>
> >>
> >>DAX exhibits growth that is roughly exponential, so I want to 
> >> plot it
> >> on a log scale:
> >>
> >>
> >> plot(DAX.[, 1], log='y', ylab='DAX')
> >> plot(DAX.[, 2], ylab='vel (%)')
> >> plot(DAX.[, 3], ylab='accel (%)')
> >>
> >>
> >>This is what I want as three panels of a single plot.
> >>
> >>
> >>I think I could get it by modifying the code for plot.ts so it
> >> accepted ylab as a vector, etc., as I previously mentioned.
> >>
> >>
> >>What do you think?
> >>Thanks,
> >>Spencer Graves
> >>>
> >>>
> >>> On Tue, May 30, 2023 at 4:06 PM Spencer Graves
> >>>  wrote:
> >>>>
> >>>>
> >>>>
> >>>> On 5/30/23 6:16 AM, Eric Berger wrote:
> >>>>> My code assumes that DAX is a ts object, as in your original post.
> >>>>>
> >>>>> On Tue, May 30, 2023 at 2:06 PM Eric Berger  
> >>>>> wrote:
> >>>>>>
> >>>>>> Untested but why not
> >>>>>>
> >>>>>> a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> >>>>>> colnames(a) <- c("logDAX", "vel", "accel")
> >>>>>> plot(a)
> >>>>
> >>>>
> >>>> Progress, but we're not there yet.
> >>>>
> >>>>
> >>>> a <- cbind(DAX, exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> >>>> colnames(a) <- c("logDAX", "vel", "accel")
> >>>

Re: [R] plot level, velocity, acceleration with one x axis

2023-05-31 Thread Eric Berger
Slight modification to have the xaxt ticks and labels only appear on
the bottom chart

par(mfrow=c(3,1))
plot(DAX.[, 1], log='y', ylab='DAX', xaxt="n")
plot(DAX.[, 2], ylab='vel (%)', xaxt="n")
plot(DAX.[, 3], ylab='accel (%)')


On Tue, May 30, 2023 at 6:23 PM Eric Berger  wrote:
>
> What if you just precede these commands as follows:
>
> par(mfrow=c(3,1))
> plot(DAX.[, 1], log='y', ylab='DAX')
> plot(DAX.[, 2], ylab='vel (%)')
> plot(DAX.[, 3], ylab='accel (%)')
>
> On Tue, May 30, 2023 at 5:45 PM Spencer Graves
>  wrote:
> >
> >
> >
> > On 5/30/23 8:48 AM, Eric Berger wrote:
> > > I am a bit confused as to what you are trying to achieve - and  even
> > > if I could guess it is not clear what the interpretation would be.
> > >> head(DAX)
> > > 1628.75 1613.63 1606.51 1621.04 1618.16 1610.61
> > >
> > > Including the leading NA's, what would be the 6 leading terms of the 3
> > > series that you want to plot,
> > > and what would be the Y labels that you want to appear at those levels
> > > (assuming that there was a
> > > Y label for each of them - just to understand the units you are talking 
> > > about)
> >
> >
> > DAX <- EuStockMarkets[, 'DAX']
> > DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX
> > colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> > head(DAX.)
> >
> >
> >   DAX exhibits growth that is roughly exponential, so I want to 
> > plot it
> > on a log scale:
> >
> >
> > plot(DAX.[, 1], log='y', ylab='DAX')
> > plot(DAX.[, 2], ylab='vel (%)')
> > plot(DAX.[, 3], ylab='accel (%)')
> >
> >
> >   This is what I want as three panels of a single plot.
> >
> >
> >   I think I could get it by modifying the code for plot.ts so it
> > accepted ylab as a vector, etc., as I previously mentioned.
> >
> >
> >       What do you think?
> >   Thanks,
> >   Spencer Graves
> > >
> > >
> > > On Tue, May 30, 2023 at 4:06 PM Spencer Graves
> > >  wrote:
> > >>
> > >>
> > >>
> > >> On 5/30/23 6:16 AM, Eric Berger wrote:
> > >>> My code assumes that DAX is a ts object, as in your original post.
> > >>>
> > >>> On Tue, May 30, 2023 at 2:06 PM Eric Berger  
> > >>> wrote:
> > >>>>
> > >>>> Untested but why not
> > >>>>
> > >>>> a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> > >>>> colnames(a) <- c("logDAX", "vel", "accel")
> > >>>> plot(a)
> > >>
> > >>
> > >>Progress, but we're not there yet.
> > >>
> > >>
> > >> a <- cbind(DAX, exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> > >> colnames(a) <- c("logDAX", "vel", "accel")
> > >> plot(a)
> > >> plot(a, axes=FALSE, log='y')
> > >> axis(1)
> > >> axis(2)
> > >>
> > >>
> > >>How do I get each y axis labeled in its original units?  I 
> > >> can use
> > >> pretty to get where I want tick marks, but I don't know where to place
> > >> them "at" in calling axis(2, at= ___)?
> > >>
> > >>
> > >> (axlb1 <- pretty(range(a[, 1])))
> > >> (axlb2 <- pretty(range(log(a[, 2]), na.rm=TRUE)))
> > >> (axlb3 <- pretty(range(log(a[, 3]), na.rm=TRUE)))
> > >>
> > >>
> > >>This suggests I write my own modification of plot.ts that 
> > >> accepts log
> > >> as a character vector of length = ncol of the ts being plotted and
> > >> returns invisibly a list with the default "at" and "label" arguments
> > >> required to produce the default labeling.  Then a user who wants a log
> > >> scale for some but not all variables can get that easily and can further
> > >> modify any of those scales further if they don't like the default.
> > >>
> > >>
> > >>???
> > >>Thanks very much.
> > >>Spencer Graves
> > >>>>
> > >>>>
> > >>>> On Tue, May 30, 2023 at 1:46 PM Spencer Graves
> > >>>>  wrote:
> > >>>>>
> > &g

Re: [R] plot level, velocity, acceleration with one x axis

2023-05-30 Thread Eric Berger
What if you just precede these commands as follows:

par(mfrow=c(3,1))
plot(DAX.[, 1], log='y', ylab='DAX')
plot(DAX.[, 2], ylab='vel (%)')
plot(DAX.[, 3], ylab='accel (%)')

On Tue, May 30, 2023 at 5:45 PM Spencer Graves
 wrote:
>
>
>
> On 5/30/23 8:48 AM, Eric Berger wrote:
> > I am a bit confused as to what you are trying to achieve - and  even
> > if I could guess it is not clear what the interpretation would be.
> >> head(DAX)
> > 1628.75 1613.63 1606.51 1621.04 1618.16 1610.61
> >
> > Including the leading NA's, what would be the 6 leading terms of the 3
> > series that you want to plot,
> > and what would be the Y labels that you want to appear at those levels
> > (assuming that there was a
> > Y label for each of them - just to understand the units you are talking 
> > about)
>
>
> DAX <- EuStockMarkets[, 'DAX']
> DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX
> colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> head(DAX.)
>
>
>   DAX exhibits growth that is roughly exponential, so I want to plot 
> it
> on a log scale:
>
>
> plot(DAX.[, 1], log='y', ylab='DAX')
> plot(DAX.[, 2], ylab='vel (%)')
> plot(DAX.[, 3], ylab='accel (%)')
>
>
>   This is what I want as three panels of a single plot.
>
>
>   I think I could get it by modifying the code for plot.ts so it
> accepted ylab as a vector, etc., as I previously mentioned.
>
>
>   What do you think?
>   Thanks,
>   Spencer Graves
> >
> >
> > On Tue, May 30, 2023 at 4:06 PM Spencer Graves
> >  wrote:
> >>
> >>
> >>
> >> On 5/30/23 6:16 AM, Eric Berger wrote:
> >>> My code assumes that DAX is a ts object, as in your original post.
> >>>
> >>> On Tue, May 30, 2023 at 2:06 PM Eric Berger  wrote:
> >>>>
> >>>> Untested but why not
> >>>>
> >>>> a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> >>>> colnames(a) <- c("logDAX", "vel", "accel")
> >>>> plot(a)
> >>
> >>
> >>Progress, but we're not there yet.
> >>
> >>
> >> a <- cbind(DAX, exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> >> colnames(a) <- c("logDAX", "vel", "accel")
> >> plot(a)
> >> plot(a, axes=FALSE, log='y')
> >> axis(1)
> >> axis(2)
> >>
> >>
> >>How do I get each y axis labeled in its original units?  I can 
> >> use
> >> pretty to get where I want tick marks, but I don't know where to place
> >> them "at" in calling axis(2, at= ___)?
> >>
> >>
> >> (axlb1 <- pretty(range(a[, 1])))
> >> (axlb2 <- pretty(range(log(a[, 2]), na.rm=TRUE)))
> >> (axlb3 <- pretty(range(log(a[, 3]), na.rm=TRUE)))
> >>
> >>
> >>    This suggests I write my own modification of plot.ts that 
> >> accepts log
> >> as a character vector of length = ncol of the ts being plotted and
> >> returns invisibly a list with the default "at" and "label" arguments
> >> required to produce the default labeling.  Then a user who wants a log
> >> scale for some but not all variables can get that easily and can further
> >> modify any of those scales further if they don't like the default.
> >>
> >>
> >>???
> >>Thanks very much.
> >>Spencer Graves
> >>>>
> >>>>
> >>>> On Tue, May 30, 2023 at 1:46 PM Spencer Graves
> >>>>  wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>> On 5/29/23 2:37 AM, Eric Berger wrote:
> >>>>>> How about this:
> >>>>>>
> >>>>>> a <- cbind(AirPassengers, diff(log(AirPassengers)),
> >>>>>> diff(diff(log(AirPassengers
> >>>>>> colnames(a)[2:3] <- c("percent increase", "acceleration")
> >>>>>> plot(a, xlab="year", main="AirPassengers")
> >>>>>
> >>>>>
> >>>>> My real problem is more difficult:  I'm analyzing CO2 data 
> >>>>> from Our
> >>>>> World in Data (https://ourworldindata.org/co2-emissions), and I need to
> >>>>> plot the CO2 data on a log scale but velo

Re: [R] plot level, velocity, acceleration with one x axis

2023-05-30 Thread Eric Berger
I am a bit confused as to what you are trying to achieve - and  even
if I could guess it is not clear what the interpretation would be.
> head(DAX)
1628.75 1613.63 1606.51 1621.04 1618.16 1610.61

Including the leading NA's, what would be the 6 leading terms of the 3
series that you want to plot,
and what would be the Y labels that you want to appear at those levels
(assuming that there was a
Y label for each of them - just to understand the units you are talking about)


On Tue, May 30, 2023 at 4:06 PM Spencer Graves
 wrote:
>
>
>
> On 5/30/23 6:16 AM, Eric Berger wrote:
> > My code assumes that DAX is a ts object, as in your original post.
> >
> > On Tue, May 30, 2023 at 2:06 PM Eric Berger  wrote:
> >>
> >> Untested but why not
> >>
> >> a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> >> colnames(a) <- c("logDAX", "vel", "accel")
> >> plot(a)
>
>
>   Progress, but we're not there yet.
>
>
> a <- cbind(DAX, exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> colnames(a) <- c("logDAX", "vel", "accel")
> plot(a)
> plot(a, axes=FALSE, log='y')
> axis(1)
> axis(2)
>
>
>   How do I get each y axis labeled in its original units?  I can use
> pretty to get where I want tick marks, but I don't know where to place
> them "at" in calling axis(2, at= ___)?
>
>
> (axlb1 <- pretty(range(a[, 1])))
> (axlb2 <- pretty(range(log(a[, 2]), na.rm=TRUE)))
> (axlb3 <- pretty(range(log(a[, 3]), na.rm=TRUE)))
>
>
>   This suggests I write my own modification of plot.ts that accepts 
> log
> as a character vector of length = ncol of the ts being plotted and
> returns invisibly a list with the default "at" and "label" arguments
> required to produce the default labeling.  Then a user who wants a log
> scale for some but not all variables can get that easily and can further
> modify any of those scales further if they don't like the default.
>
>
>   ???
>   Thanks very much.
>   Spencer Graves
> >>
> >>
> >> On Tue, May 30, 2023 at 1:46 PM Spencer Graves
> >>  wrote:
> >>>
> >>>
> >>>
> >>> On 5/29/23 2:37 AM, Eric Berger wrote:
> >>>> How about this:
> >>>>
> >>>> a <- cbind(AirPassengers, diff(log(AirPassengers)),
> >>>> diff(diff(log(AirPassengers
> >>>> colnames(a)[2:3] <- c("percent increase", "acceleration")
> >>>> plot(a, xlab="year", main="AirPassengers")
> >>>
> >>>
> >>>My real problem is more difficult:  I'm analyzing CO2 data 
> >>> from Our
> >>> World in Data (https://ourworldindata.org/co2-emissions), and I need to
> >>> plot the CO2 data on a log scale but velocity and acceleration on linear
> >>> scales.  The following is comparable:
> >>>
> >>>
> >>> str(DAX <- EuStockMarkets[, 'DAX'])
> >>> str(DAX. <- cbind(DAX, diff(log(DAX)),
> >>> diff(diff(log(DAX)
> >>> colnames(DAX.)[2:3] <- c('vel', 'accel')
> >>> plot(DAX.)
> >>>
> >>>
> >>>I want the first of the three panels to plot on the log scale, 
> >>> but
> >>> the other two on linear scales.  The obvious attempt does not work:
> >>>
> >>>
> >>> plot(DAX., log=c('y', '', ''))
> >>> #Error in length(log) && log != "" :
> >>> #  'length = 3' in coercion to 'logical(1)'
> >>>
> >>>
> >>>Trying to construct my own axes isn't easy, either:
> >>>
> >>>
> >>> str(logDAX <- cbind(log(DAX), diff(log(DAX)),
> >>> diff(diff(log(DAX)
> >>> colnames(logDAX) <- c('logDAX', 'vel', 'accel')
> >>> plot(logDAX, axes=FALSE)
> >>> axis(1)
> >>> axis(2)
> >>>
> >>>
> >>>I'm thinking of creating my own copy of "plot.ts", and 
> >>> changing it so
> >>> it accepts the "log" argument as a vector of length equal to ncol of the
> >>> ts object to be plotted AND returning an object that would allow a user
> >>> to call "axis" ncol times.
> >>>
> >>>
> >>>Suggestions?
> >>>
> >>>
&g

Re: [R] plot level, velocity, acceleration with one x axis

2023-05-30 Thread Eric Berger
My code assumes that DAX is a ts object, as in your original post.

On Tue, May 30, 2023 at 2:06 PM Eric Berger  wrote:
>
> Untested but why not
>
> a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
> colnames(a) <- c("logDAX", "vel", "accel")
> plot(a)
>
>
> On Tue, May 30, 2023 at 1:46 PM Spencer Graves
>  wrote:
> >
> >
> >
> > On 5/29/23 2:37 AM, Eric Berger wrote:
> > > How about this:
> > >
> > > a <- cbind(AirPassengers, diff(log(AirPassengers)),
> > > diff(diff(log(AirPassengers
> > > colnames(a)[2:3] <- c("percent increase", "acceleration")
> > > plot(a, xlab="year", main="AirPassengers")
> >
> >
> >   My real problem is more difficult:  I'm analyzing CO2 data from 
> > Our
> > World in Data (https://ourworldindata.org/co2-emissions), and I need to
> > plot the CO2 data on a log scale but velocity and acceleration on linear
> > scales.  The following is comparable:
> >
> >
> > str(DAX <- EuStockMarkets[, 'DAX'])
> > str(DAX. <- cbind(DAX, diff(log(DAX)),
> >diff(diff(log(DAX)
> > colnames(DAX.)[2:3] <- c('vel', 'accel')
> > plot(DAX.)
> >
> >
> >   I want the first of the three panels to plot on the log scale, but
> > the other two on linear scales.  The obvious attempt does not work:
> >
> >
> > plot(DAX., log=c('y', '', ''))
> > #Error in length(log) && log != "" :
> > #  'length = 3' in coercion to 'logical(1)'
> >
> >
> >   Trying to construct my own axes isn't easy, either:
> >
> >
> > str(logDAX <- cbind(log(DAX), diff(log(DAX)),
> >diff(diff(log(DAX)
> > colnames(logDAX) <- c('logDAX', 'vel', 'accel')
> > plot(logDAX, axes=FALSE)
> > axis(1)
> > axis(2)
> >
> >
> >   I'm thinking of creating my own copy of "plot.ts", and changing 
> > it so
> > it accepts the "log" argument as a vector of length equal to ncol of the
> > ts object to be plotted AND returning an object that would allow a user
> > to call "axis" ncol times.
> >
> >
> >   Suggestions?
> >
> >
> >   Thanks,
> >   Spencer Graves
> >
> > >
> > > HTH,
> > > Eric
> > >
> > >
> > > On Mon, May 29, 2023 at 7:57 AM Spencer Graves
> > >  wrote:
> > >>
> > >> Hello, All:
> > >>
> > >>
> > >>I want to plot level, velocity, and acceleration in three 
> > >> panels with
> > >> only one x axis.  The code below does this using "layout".  However, I
> > >> want the three plot areas to be of equal size, and this won't do that:
> > >> If I stretch the plot vertically, the relative sizes of the three panels
> > >> changes.  There's probably a way to do this with ggplot2, but I have yet
> > >> to find it.
> > >>
> > >>
> > >>Suggestions?
> > >>Thanks,
> > >>Spencer Graves
> > >>
> > >>
> > >> str(AirTime <- as.numeric(time(AirPassengers)))
> > >> str(AP <- as.numeric(AirPassengers))
> > >>
> > >> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> > >> (mat3x1 <- matrix(1:3, 3))
> > >> plot3x1 <- layout(mat3x1, heights=c(1.4, 1, 1.5))
> > >> layout.show(plot3x1)
> > >>
> > >> par(mar=c(0, 4.1, 4.1, 2.1))
> > >> plot(AirTime, AP, log='y', type='l', axes=FALSE,
> > >>main='AirPassengers', ylab='AirPassengers')
> > >> box(col='grey')
> > >> axis(2, las=1)
> > >>
> > >> par(mar=c(0, 4.1, 0, 2.1))
> > >> vAP <- diff(log(AP))
> > >> plot(tail(AirTime, -1), vAP, type='l',
> > >>ylab='percent increase', axes=FALSE)
> > >> box(col='grey')
> > >> axis(2, las=1)
> > >>
> > >> par(mar=c(5.1, 4.1, 0, 2.1))
> > >> plot(tail(AirTime, -2), diff(vAP), type='l',
> > >>ylab='acceleration', xlab='year',
> > >>las=1)
> > >> box(col='grey')
> > >>
> > >> par(def.par)
> > >>
> > >> __
> > >> 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] plot level, velocity, acceleration with one x axis

2023-05-30 Thread Eric Berger
Untested but why not

a <- cbind(log(DAX), exp(diff(log(DAX))), exp(diff(diff(log(DAX)
colnames(a) <- c("logDAX", "vel", "accel")
plot(a)


On Tue, May 30, 2023 at 1:46 PM Spencer Graves
 wrote:
>
>
>
> On 5/29/23 2:37 AM, Eric Berger wrote:
> > How about this:
> >
> > a <- cbind(AirPassengers, diff(log(AirPassengers)),
> > diff(diff(log(AirPassengers
> > colnames(a)[2:3] <- c("percent increase", "acceleration")
> > plot(a, xlab="year", main="AirPassengers")
>
>
>   My real problem is more difficult:  I'm analyzing CO2 data from Our
> World in Data (https://ourworldindata.org/co2-emissions), and I need to
> plot the CO2 data on a log scale but velocity and acceleration on linear
> scales.  The following is comparable:
>
>
> str(DAX <- EuStockMarkets[, 'DAX'])
> str(DAX. <- cbind(DAX, diff(log(DAX)),
>diff(diff(log(DAX)
> colnames(DAX.)[2:3] <- c('vel', 'accel')
> plot(DAX.)
>
>
>   I want the first of the three panels to plot on the log scale, but
> the other two on linear scales.  The obvious attempt does not work:
>
>
> plot(DAX., log=c('y', '', ''))
> #Error in length(log) && log != "" :
> #  'length = 3' in coercion to 'logical(1)'
>
>
>   Trying to construct my own axes isn't easy, either:
>
>
> str(logDAX <- cbind(log(DAX), diff(log(DAX)),
>diff(diff(log(DAX)
> colnames(logDAX) <- c('logDAX', 'vel', 'accel')
> plot(logDAX, axes=FALSE)
> axis(1)
> axis(2)
>
>
>   I'm thinking of creating my own copy of "plot.ts", and changing it 
> so
> it accepts the "log" argument as a vector of length equal to ncol of the
> ts object to be plotted AND returning an object that would allow a user
> to call "axis" ncol times.
>
>
>   Suggestions?
>
>
>   Thanks,
>   Spencer Graves
>
> >
> > HTH,
> > Eric
> >
> >
> > On Mon, May 29, 2023 at 7:57 AM Spencer Graves
> >  wrote:
> >>
> >> Hello, All:
> >>
> >>
> >>I want to plot level, velocity, and acceleration in three 
> >> panels with
> >> only one x axis.  The code below does this using "layout".  However, I
> >> want the three plot areas to be of equal size, and this won't do that:
> >> If I stretch the plot vertically, the relative sizes of the three panels
> >> changes.  There's probably a way to do this with ggplot2, but I have yet
> >> to find it.
> >>
> >>
> >>Suggestions?
> >>Thanks,
> >>Spencer Graves
> >>
> >>
> >> str(AirTime <- as.numeric(time(AirPassengers)))
> >> str(AP <- as.numeric(AirPassengers))
> >>
> >> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> >> (mat3x1 <- matrix(1:3, 3))
> >> plot3x1 <- layout(mat3x1, heights=c(1.4, 1, 1.5))
> >> layout.show(plot3x1)
> >>
> >> par(mar=c(0, 4.1, 4.1, 2.1))
> >> plot(AirTime, AP, log='y', type='l', axes=FALSE,
> >>main='AirPassengers', ylab='AirPassengers')
> >> box(col='grey')
> >> axis(2, las=1)
> >>
> >> par(mar=c(0, 4.1, 0, 2.1))
> >> vAP <- diff(log(AP))
> >> plot(tail(AirTime, -1), vAP, type='l',
> >>ylab='percent increase', axes=FALSE)
> >> box(col='grey')
> >> axis(2, las=1)
> >>
> >> par(mar=c(5.1, 4.1, 0, 2.1))
> >> plot(tail(AirTime, -2), diff(vAP), type='l',
> >>ylab='acceleration', xlab='year',
> >>las=1)
> >> box(col='grey')
> >>
> >> par(def.par)
> >>
> >> __
> >> 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] plot level, velocity, acceleration with one x axis

2023-05-29 Thread Eric Berger
How about this:

a <- cbind(AirPassengers, diff(log(AirPassengers)),
diff(diff(log(AirPassengers
colnames(a)[2:3] <- c("percent increase", "acceleration")
plot(a, xlab="year", main="AirPassengers")

HTH,
Eric


On Mon, May 29, 2023 at 7:57 AM Spencer Graves
 wrote:
>
> Hello, All:
>
>
>   I want to plot level, velocity, and acceleration in three panels 
> with
> only one x axis.  The code below does this using "layout".  However, I
> want the three plot areas to be of equal size, and this won't do that:
> If I stretch the plot vertically, the relative sizes of the three panels
> changes.  There's probably a way to do this with ggplot2, but I have yet
> to find it.
>
>
>   Suggestions?
>   Thanks,
>   Spencer Graves
>
>
> str(AirTime <- as.numeric(time(AirPassengers)))
> str(AP <- as.numeric(AirPassengers))
>
> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> (mat3x1 <- matrix(1:3, 3))
> plot3x1 <- layout(mat3x1, heights=c(1.4, 1, 1.5))
> layout.show(plot3x1)
>
> par(mar=c(0, 4.1, 4.1, 2.1))
> plot(AirTime, AP, log='y', type='l', axes=FALSE,
>   main='AirPassengers', ylab='AirPassengers')
> box(col='grey')
> axis(2, las=1)
>
> par(mar=c(0, 4.1, 0, 2.1))
> vAP <- diff(log(AP))
> plot(tail(AirTime, -1), vAP, type='l',
>   ylab='percent increase', axes=FALSE)
> box(col='grey')
> axis(2, las=1)
>
> par(mar=c(5.1, 4.1, 0, 2.1))
> plot(tail(AirTime, -2), diff(vAP), type='l',
>   ylab='acceleration', xlab='year',
>   las=1)
> box(col='grey')
>
> par(def.par)
>
> __
> 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] Recombining Mon and Year values

2023-05-17 Thread Eric Berger
An alternative that does not use the zoo package but uses the built-in
constant month.abb

df1 <- data.frame(Year = c(2021, 2021, 2021),
   Mon = c("Mar", "Jan", "Apr"))

df1$mon_year <- df1$Year*100 + sapply(df1$Mon, \(x) match(x,month.abb))

sort(df1$mon_year)

On Wed, May 17, 2023 at 2:48 AM Jeff Newmiller  wrote:
>
> I don't use lubridate, but that package works with Date and POSIXt types, 
> which I do use. Just remember to include a day when converting (1st of month 
> is typical), and use an output format to hide the day when you plot.
>
> On May 16, 2023 1:29:27 PM PDT, Jeff Reichman  wrote:
> >R Help
> >
> >
> >
> >I have a data.frame where I've broken out the year  and an ordered
> >month  values. But I need to recombine them so I can graph mon-year in
> >order but when I recombine I lose the month order and the results are
> >plotted alphabetical.
> >
> >
> >
> >Yearmonth  mon_year
> >
> > 
> >
> >2021 MarMar-2021
> >
> >2021 Jan Jan-2021
> >
> >2021 Apr Apr-2021
> >
> >
> >
> >So do I need to convert the months back to an integer then recombine to
> >plot.
> >
> >
> >
> >Jeff Reichman
> >
> >
> >   [[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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> __
> 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] RandomForest tuning the parameters

2023-05-09 Thread Eric Berger
Hi Sacha,
On second thought, perhaps this is more the direction that you want ...

X2 = cbind(X_train,y_train)
colnames(X2)[3] = "y"
regr2<-randomForest(y~x1+x2, data=X2,maxnodes=10, ntree=10)
regr
regr2

#Make prediction
predictions= predict(regr, X_test)
predictions2= predict(regr2, X_test)

HTH,
Eric


On Tue, May 9, 2023 at 6:40 AM Eric Berger  wrote:

> Hi,
> One problem you have is with the command:
> regr<-randomForest(y~x1+x2, data=X_train, proximity=TRUE)
>
> What you need is something like this:
>
> X2 <- cbind(X,y)
> regr<-randomForest(y~x1+x2, data=X2, proximity=TRUE)
>
> HTH,
> Eric
>
>
> On Mon, May 8, 2023 at 11:11 PM varin sacha via R-help
>  wrote:
> >
> > Dear R-experts,
> >
> > Here below a toy example with some error messages, especially at the end
> of the code (Tuning the parameters). Your help to correct my R code would
> be highly appreciated.
> >
> >
> > ###
> > #libraries
> > library(lattice)
> > library(ggplot2)
> > library(caret)
> > library(randomForest)
> >
> > #Data
> >
> y=c(23,34,32,12,24,35,45,56,76,87,54,34,23,45,41,13,16,98,35,65,56,67,78,89,87,64,53,31,14,34,45,46,57,69,90,80,70,65,50,45,60,56,87,79,64,34,25,47,61,24,10,13,12,15,46,58,76,89,90,98)
> >
> x1=c(4,5,6,7,1,10,19,20,21,14,23,6,5,32,15,12,16,14,2,3,4,5,3,2,1,2,6,7,5,4,3,2,1,3,4,6,7,9,5,4,3,7,10,11,12,13,10,3,2,5,6,9,8,7,4,12,15,16,2,3)
> >
> x2=c(0,0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,1,0,0,1,0,0,1,1)
> >
> > y=as.numeric(y)
> > x1=as.numeric(x1)
> > x2=as.factor(x2)
> >
> > X=data.frame(x1,x2)
> > y=y
> >
> > #Split data into training and test sets
> > index=createDataPartition(y, p=0.75, list=FALSE)
> > X_train = X[index, ]
> > X_test = X[-index, ]
> > y_train= y[index ]
> > y_test = y[-index ]
> >
> > #Train de model
> > regr=randomForest (x=X_train, y=y_train, maxnodes=10, ntree=10)
> >
> > regr<-randomForest(y~x1+x2, data=X_train, proximity=TRUE)
> > regr
> >
> > #Make prediction
> > predictions= predict(regr, X_test)
> >
> > result= X_test
> > result['y'] = y_test
> > result['prediction'] = predictions
> > result
> >
> > # Import library for Metrics
> > library(Metrics)
> >
> > print(paste0('MAE: ' , mae(y_test,predictions) ))
> > print(paste0('MSE: ' ,caret::postResample(predictions ,
> y_test)['RMSE']^2 ))
> > print(paste0('R2: ' ,caret::postResample(predictions ,
> y_test)['Rsquared'] ))
> >
> >
> > #Tuning the parameters
> > N=500 #length(X_train)
> > X_train_ = X_train[1:N , ]
> > y_train_ = y_train[1:N]
> >
> > seed <-7
> > metric<-'RMSE'
> >
> > customRF <- list(type = "Regression", library = "randomForest", loop =
> NULL)
> >
> > customRF$parameters <- data.frame(parameter = c("maxnodes", "ntree"),
> class = rep("numeric", 2), label = c("maxnodes", "ntree"))
> >
> > customRF$grid <- function(x, y, len = NULL, search = "grid") {}
> >
> > customRF$fit <- function(x, y, wts, param, lev, last, weights,
> classProbs, ...) {
> >
> >  randomForest(x, y, maxnodes = param$maxnodes, ntree=param$ntree, ...)
> >
> > }
> >
> > customRF$predict <- function(modelFit, newdata, preProc = NULL,
> submodels = NULL)
> >
> > predict(modelFit, newdata)
> >
> > customRF$prob <- function(modelFit, newdata, preProc = NULL, submodels =
> NULL)
> >
> >   predict(modelFit, newdata, type = "prob")
> >
> > customRF$sort <- function(x) x[order(x[,1]),]
> >
> > customRF$levels <- function(x) x$classes
> >
> >
> > # Set grid search parameters
> > control <- trainControl(method="repeatedcv", number=10, repeats=3,
> search='grid')
> >
> > # Outline the grid of parameters
> > tunegrid <- expand.grid(.maxnodes=c(10,20,30,50), .ntree=c(100, 200,
> 300))
> > set.seed(seed)
> >
> > # Train the model
> > rf_gridsearch <- train(x=X_train_, y=y_train_, method=customRF,
> metric=metric, tuneGrid=tunegrid, trControl=control)
> >
> > plot(rf_gridsearch)
> >
> > rf_gridsearch$bestTune
> >
> > #
> >
> > __
> > 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] RandomForest tuning the parameters

2023-05-08 Thread Eric Berger
Hi,
One problem you have is with the command:
regr<-randomForest(y~x1+x2, data=X_train, proximity=TRUE)

What you need is something like this:

X2 <- cbind(X,y)
regr<-randomForest(y~x1+x2, data=X2, proximity=TRUE)

HTH,
Eric


On Mon, May 8, 2023 at 11:11 PM varin sacha via R-help
 wrote:
>
> Dear R-experts,
>
> Here below a toy example with some error messages, especially at the end of 
> the code (Tuning the parameters). Your help to correct my R code would be 
> highly appreciated.
>
>
> ###
> #libraries
> library(lattice)
> library(ggplot2)
> library(caret)
> library(randomForest)
>
> #Data
> y=c(23,34,32,12,24,35,45,56,76,87,54,34,23,45,41,13,16,98,35,65,56,67,78,89,87,64,53,31,14,34,45,46,57,69,90,80,70,65,50,45,60,56,87,79,64,34,25,47,61,24,10,13,12,15,46,58,76,89,90,98)
> x1=c(4,5,6,7,1,10,19,20,21,14,23,6,5,32,15,12,16,14,2,3,4,5,3,2,1,2,6,7,5,4,3,2,1,3,4,6,7,9,5,4,3,7,10,11,12,13,10,3,2,5,6,9,8,7,4,12,15,16,2,3)
> x2=c(0,0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,1,0,0,1,0,0,1,1)
>
> y=as.numeric(y)
> x1=as.numeric(x1)
> x2=as.factor(x2)
>
> X=data.frame(x1,x2)
> y=y
>
> #Split data into training and test sets
> index=createDataPartition(y, p=0.75, list=FALSE)
> X_train = X[index, ]
> X_test = X[-index, ]
> y_train= y[index ]
> y_test = y[-index ]
>
> #Train de model
> regr=randomForest (x=X_train, y=y_train, maxnodes=10, ntree=10)
>
> regr<-randomForest(y~x1+x2, data=X_train, proximity=TRUE)
> regr
>
> #Make prediction
> predictions= predict(regr, X_test)
>
> result= X_test
> result['y'] = y_test
> result['prediction'] = predictions
> result
>
> # Import library for Metrics
> library(Metrics)
>
> print(paste0('MAE: ' , mae(y_test,predictions) ))
> print(paste0('MSE: ' ,caret::postResample(predictions , y_test)['RMSE']^2 ))
> print(paste0('R2: ' ,caret::postResample(predictions , y_test)['Rsquared'] ))
>
>
> #Tuning the parameters
> N=500 #length(X_train)
> X_train_ = X_train[1:N , ]
> y_train_ = y_train[1:N]
>
> seed <-7
> metric<-'RMSE'
>
> customRF <- list(type = "Regression", library = "randomForest", loop = NULL)
>
> customRF$parameters <- data.frame(parameter = c("maxnodes", "ntree"), class = 
> rep("numeric", 2), label = c("maxnodes", "ntree"))
>
> customRF$grid <- function(x, y, len = NULL, search = "grid") {}
>
> customRF$fit <- function(x, y, wts, param, lev, last, weights, classProbs, 
> ...) {
>
>  randomForest(x, y, maxnodes = param$maxnodes, ntree=param$ntree, ...)
>
> }
>
> customRF$predict <- function(modelFit, newdata, preProc = NULL, submodels = 
> NULL)
>
> predict(modelFit, newdata)
>
> customRF$prob <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
>
>   predict(modelFit, newdata, type = "prob")
>
> customRF$sort <- function(x) x[order(x[,1]),]
>
> customRF$levels <- function(x) x$classes
>
>
> # Set grid search parameters
> control <- trainControl(method="repeatedcv", number=10, repeats=3, 
> search='grid')
>
> # Outline the grid of parameters
> tunegrid <- expand.grid(.maxnodes=c(10,20,30,50), .ntree=c(100, 200, 300))
> set.seed(seed)
>
> # Train the model
> rf_gridsearch <- train(x=X_train_, y=y_train_, method=customRF, 
> metric=metric, tuneGrid=tunegrid, trControl=control)
>
> plot(rf_gridsearch)
>
> rf_gridsearch$bestTune
>
> #
>
> __
> 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] Huge differences in Ram Consumption with different versions of R on the same scripts

2023-05-08 Thread Eric Berger
Assuming the vignettes are the same in both cases, try going back to
the system where it worked and introduce a single change? i.e.
a) previous linux system and R 4.2
b) Previous R version and Oracle Linux 8

Repeat the test for a) and b) and if one works fine and the other
fails at least that will narrow down the search.

HTH,
Eric




On Mon, May 8, 2023 at 4:41 AM Robert Knight  wrote:
>
> Hello R Help,
>
> I have some R vignettes that run fpp2 time series regressions.  I have run
> these for a long time using R and a 12 core computer system.  I ran each
> core using Linux to run a vignette on that core, so that all 12 could work
> concurrently.  With 48GB of ram, the ram never filled up. I ran these
> regressions for hours, one data set right after the other on each core.
> Recently, I switched to Oracle Linux 8 and R 4.2 Now, with the same
> scripts, and the same data, the ram fills up and R reserves 4.2GB per
> instance in some cases.  This results in all the ram being consumed and the
> swap space on the system activating constantly so that the performance is
> abysmal. It begins using 12Gb of swap space in addition to the ram
> consumed.  It bogs the system so bad that one can't even establish new
> terminal sessions.
>
> Is there a way to specify a maximum ram allowance in an R vignette?  If
> that’s not possible, what resources can you recommend to help identify the
> reason for the change in memory use?  Why would a different version of
> R/Linux use 2GB per instance, while another uses 4.4GB? What kind of
> troubleshooting or programming techniques should I research for this kind
> of concern?
> Robert Knight
>
> [[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] problem installing RGBL library

2023-04-22 Thread Eric Berger
looks fine.
what's the problem?

On Sat, Apr 22, 2023 at 7:27 PM varin sacha  wrote:

> Eric,
>
> Here it is :
>
>
> library(RBGL)
> Loading required package: graph
> Loading required package: BiocGenerics
>
> Attaching package: ‘BiocGenerics’
>
> The following objects are masked from ‘package:igraph’:
>
>   normalize, path, union
>
> The following objects are masked from ‘package:stats’:
>
>   IQR, mad, sd, var, xtabs
>
> The following objects are masked from ‘package:base’:
>
>   anyDuplicated, aperm, append, as.data.frame, basename, cbind, colnames,
> dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep, grepl,
> intersect, is.unsorted, lapply, Map,
>   mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
> Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort, table,
> tapply, union, unique, unsplit, which.max,
>   which.min
>
>
> Attaching package: ‘graph’
>
> The following objects are masked from ‘package:igraph’:
>
>   degree, edges, intersection
>
>
> Attaching package: ‘RBGL’
>
> The following objects are masked from ‘package:igraph’:
>
>   bfs, dfs, transitivity
>
>
>
>
>
>
>
>
>
>
> Le samedi 22 avril 2023 à 18:12:56 UTC+2, Eric Berger <
> ericjber...@gmail.com> a écrit :
>
>
>
>
>
> What happens with the command
> > library(RBGL)
>
>
>
> On Sat, Apr 22, 2023 at 7:08 PM varin sacha via R-help <
> r-help@r-project.org> wrote:
> > Dear R-experts,
> >
> > How to solve that problem?
> >
> > My R version is 4.2.1
> >
> > Here below trying to install RGBL library found here :
> https://bioconductor.org/packages/release/bioc/html/RBGL.html
> >
> > So, I run this R code :
> >
> > if (!require("BiocManager", quietly = TRUE))
> > install.packages("BiocManager")
> >
> > BiocManager::install("RBGL")
> >
> >
> > Here is what I get :
> >
> >> if (!require("BiocManager", quietly = TRUE))
> > + install.packages("BiocManager")
> > Bioconductor version 3.16 (BiocManager 1.30.20), R 4.2.1 (2022-06-23)
> >>
> >> BiocManager::install("RBGL")
> > Bioconductor version 3.16 (BiocManager 1.30.20), R 4.2.1 (2022-06-23)
> > Warning message:
> > package(s) not installed when version(s) same as or greater than
> current; use `force = TRUE` to re-install: 'RBGL'
> >
> > __
> > 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] problem installing RGBL library

2023-04-22 Thread Eric Berger
What happens with the command
> library(RBGL)



On Sat, Apr 22, 2023 at 7:08 PM varin sacha via R-help 
wrote:

> Dear R-experts,
>
> How to solve that problem?
>
> My R version is 4.2.1
>
> Here below trying to install RGBL library found here :
> https://bioconductor.org/packages/release/bioc/html/RBGL.html
>
> So, I run this R code :
>
> if (!require("BiocManager", quietly = TRUE))
> install.packages("BiocManager")
>
> BiocManager::install("RBGL")
>
>
> Here is what I get :
>
> > if (!require("BiocManager", quietly = TRUE))
> + install.packages("BiocManager")
> Bioconductor version 3.16 (BiocManager 1.30.20), R 4.2.1 (2022-06-23)
> >
> > BiocManager::install("RBGL")
> Bioconductor version 3.16 (BiocManager 1.30.20), R 4.2.1 (2022-06-23)
> Warning message:
> package(s) not installed when version(s) same as or greater than current;
> use `force = TRUE` to re-install: 'RBGL'
>
> __
> 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] Package Caret

2023-04-14 Thread Eric Berger
prettyseq() is in the seqinr package. That seems to usher in a lot of other
issues.

How about caTools::sample.split() ?   (I have never used it, I just learned
about it from a search)

https://rdrr.io/cran/caTools/src/R/sample.split.R

Good luck,
Eric


On Fri, Apr 14, 2023 at 10:50 AM Gábor Malomsoki 
wrote:

> Or is there any similar function to split the dataframe to trainDF and
> testDF?
>
> Gábor Malomsoki  schrieb am Fr., 14. Apr. 2023,
> 09:45:
>
>>
>> This is the error then:
>> error in prettyseq(1:ncol(out)) : could not find function "prettyseq"
>>
>>
>>
>>
>> Eric Berger  schrieb am Fr., 14. Apr. 2023, 09:06:
>>
>>> What happens if you do the following?
>>> > library(caret)
>>> > ?caret::createDataPartition
>>>
>>> i.e. to confirm that caret is loaded, seek help on this function
>>>
>>>
>>> On Fri, Apr 14, 2023 at 9:56 AM Gábor Malomsoki <
>>> gmalomsoki1...@gmail.com> wrote:
>>>
>>>> Eric,
>>>>
>>>> Yes, i know, i am calling the package, but this is not working.
>>>> I saw a similar question in Stackoverflow, advising install package
>>>> Rcpp, but this one i am unable to install.
>>>>
>>>> Eric Berger  schrieb am Fr., 14. Apr. 2023,
>>>> 08:47:
>>>>
>>>>> You first have to load the package using the library command.
>>>>> > library(caret)
>>>>>
>>>>> Then you can call createDataPartition. e.g.
>>>>> > data(oil)
>>>>> > createDataPartition(oilType,2)
>>>>>
>>>>> HTH,
>>>>> Eric
>>>>>
>>>>>
>>>>> On Fri, Apr 14, 2023 at 7:52 AM Gábor Malomsoki <
>>>>> gmalomsoki1...@gmail.com> wrote:
>>>>>
>>>>>> Dear all,
>>>>>> When i try to use createDataPartition after calling package 'caret',
>>>>>> i get
>>>>>> the message:
>>>>>> "could not find function createDataPartition"
>>>>>>
>>>>>> I use:
>>>>>> R-4.2.3 for Windows
>>>>>> RStudio-2023.03.0-386
>>>>>>
>>>>>> Do you have any experience with this failure?
>>>>>>
>>>>>> Thank you!
>>>>>> Best regards
>>>>>> Gabor
>>>>>>
>>>>>> [[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.


Re: [R] Package Caret

2023-04-14 Thread Eric Berger
What happens if you do the following?
> library(caret)
> ?caret::createDataPartition

i.e. to confirm that caret is loaded, seek help on this function


On Fri, Apr 14, 2023 at 9:56 AM Gábor Malomsoki 
wrote:

> Eric,
>
> Yes, i know, i am calling the package, but this is not working.
> I saw a similar question in Stackoverflow, advising install package Rcpp,
> but this one i am unable to install.
>
> Eric Berger  schrieb am Fr., 14. Apr. 2023, 08:47:
>
>> You first have to load the package using the library command.
>> > library(caret)
>>
>> Then you can call createDataPartition. e.g.
>> > data(oil)
>> > createDataPartition(oilType,2)
>>
>> HTH,
>> Eric
>>
>>
>> On Fri, Apr 14, 2023 at 7:52 AM Gábor Malomsoki 
>> wrote:
>>
>>> Dear all,
>>> When i try to use createDataPartition after calling package 'caret', i
>>> get
>>> the message:
>>> "could not find function createDataPartition"
>>>
>>> I use:
>>> R-4.2.3 for Windows
>>> RStudio-2023.03.0-386
>>>
>>> Do you have any experience with this failure?
>>>
>>> Thank you!
>>> Best regards
>>> Gabor
>>>
>>> [[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.


Re: [R] Package Caret

2023-04-14 Thread Eric Berger
You first have to load the package using the library command.
> library(caret)

Then you can call createDataPartition. e.g.
> data(oil)
> createDataPartition(oilType,2)

HTH,
Eric


On Fri, Apr 14, 2023 at 7:52 AM Gábor Malomsoki 
wrote:

> Dear all,
> When i try to use createDataPartition after calling package 'caret', i get
> the message:
> "could not find function createDataPartition"
>
> I use:
> R-4.2.3 for Windows
> RStudio-2023.03.0-386
>
> Do you have any experience with this failure?
>
> Thank you!
> Best regards
> Gabor
>
> [[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.


Re: [R] Split String in regex while Keeping Delimiter

2023-04-12 Thread Eric Berger
This seems to do the job but there are probably more elegant solutions:

f <- function(s) { sub("^ ","",unlist(strsplit(gsub("\\+ ","+@ ",s),"@"))) }
g <- function(s) { sub("^ ","",unlist(strsplit(gsub("- ","-@ ",s),"@"))) }
h <- function(s) { g(f(s)) }

To try it out:
s <- “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
t <- “leucocyten – grampositieve coccen +”

h(s)
h(t)

HTH,
Eric


On Wed, Apr 12, 2023 at 7:56 PM Emily Bakker 
wrote:

> Hello List,
>
> I have a dataset consisting of strings that I want to split while saving
> the delimiter.
>
> Some example data:
> “leucocyten + gramnegatieve staven +++ grampositieve staven ++”
> “leucocyten – grampositieve coccen +”
>
> I want to split the strings such that I get the following result:
> c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
> c(“leucocyten –“, “grampositieve coccen +”)
>
> I have tried strsplit with a regular expression with a positive lookahead,
> but I am not able to achieve the results that I want.
>
> I have tried:
> as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
>
> Which results in:
> c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,
>  “grampositieve staven ++”)
> c(“leucocyten “, “–“, “grampositieve coccen +”)
>
>
> Is there a function or regular expression that will make this possible?
>
> Kind regards,
> Emily
>
> __
> 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] converting to date object...

2023-04-12 Thread Eric Berger
lubridate::dmy("12 APR 2023")


On Wed, Apr 12, 2023 at 5:34 PM akshay kulkarni 
wrote:

> dear members,
> I want to convert "12 APR 2023" into a Date
> object. I tried as_Date() from lubridate, but it is not working:
>
> > as_date("12 APR 2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
> > as_date("12-APR-2023")
> [1] NA
> Warning message:
> All formats failed to parse. No formats found.
>
> I am looking for a shorthand way of doing it. Of course, one can convert
> APR to 4 and get the job done, but I ma looking for a short and elegant code
>
> please help...
>
> THanking you,
> 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.
>

[[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] Matrix scalar operation that saves memory?

2023-04-12 Thread Eric Berger
One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
  'void subtractConst(NumericVector x, double c) {
  for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

  [,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric


On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter  wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szh...@ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> > [[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.
>

[[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] Simple Stacking of Two Columns

2023-04-03 Thread Eric Berger
pivot_longer()

Sent from my iPhone

> On 3 Apr 2023, at 18:09, Sparks, John  wrote:
> 
> Hi R-Helpers,
> 
> Sorry to bother you, but I have a simple task that I can't figure out how to 
> do.
> 
> For example, I have some names in two columns
> 
> NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
> 
> and I simply want to get a single column
> NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
>> NamesLong
>  Names
> 1   Tom
> 2  Dick
> 3 Larry
> 4 Curly
> 
> 
> Stack produces an error
> NamesLong<-stack(NamesWide$Name1,NamesWide$Names2)
> Error in if (drop) { : argument is of length zero
> 
> So does bind_rows
>> NamesLong<-dplyr::bind_rows(NamesWide$Name1,NamesWide$Name2)
> Error in `dplyr::bind_rows()`:
> ! Argument 1 must be a data frame or a named atomic vector.
> Run `rlang::last_error()` to see where the error occurred.
> 
> I tried making separate dataframes to get around the error in bind_rows but 
> it puts the data in two different columns
> Name1<-data.frame(c("Tom","Dick"))
> Name2<-data.frame(c("Larry","Curly"))
> NamesLong<-dplyr::bind_rows(Name1,Name2)
>> NamesLong
>  c..TomDick.. c..LarryCurly..
> 1  Tom
> 2 Dick
> 3Larry
> 4Curly
> 
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
>> NamesLong
>  Name1 Name2
> 1   Tom Larry
> 2  Dick Curly
> 
> 
> Please help me solve what should be a very simple problem.
> 
> Thanks,
> John Sparks
> 
> 
> 
> 
> 
>[[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] Detpack package

2023-02-01 Thread Eric Berger
Detpack or detpack?

What happens when you try detpack::chi2testuniform(...) ?


On Wed, Feb 1, 2023 at 6:08 PM Nick Wray  wrote:

> Hello   I've successfully installed the package "Detpack" and remembered to
> call it via library()  But when I try to use the functions i get this:
>
>
> chi2testuniform(vals, 0.05)
>
> Error in chi2testuniform(vals, 0.05) :
>   could not find function "chi2testuniform"
>
>
> And yet this function (amongst other which also are not recognised) are all
> listed on the CRAN page:
> http://cran.nexr.com/web/packages/detpack/detpack.pdf
>
> I can't remember anything like this happening before - as far as I can see
> I don't need to load or write anything else before executing the function
> and I can't find references to any problems with this package on the net
> Can anyone cast light on what's happening?  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.
>

[[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] Problem to run python code in r markdown

2023-01-20 Thread Eric Berger
You don't specify what platform you are on (linux / Windows / other),
and what tools you are using.
I am running a linux system and I have virtual environments set up.
I set the environment variable RETICULATE_PYTHON to point to python
(in my virtual environment).
I am using RStudio, and I use the File pulldown menu and select New
File --> 'R Markdown' or New File --> 'Quarto document'.
In the R Markdown (or Quarto) document, create a python code chunk:

```{python}
import numpy as np
print(np.arange(5))
```

works for me.
HTH,
Eric




On Fri, Jan 20, 2023 at 6:20 PM Kai Yang via R-help
 wrote:
>
> Hi Team,I'm trying to run python in R markdown (flexdashboard). The code is 
> below:
>
> try Python=
> ```{r, include=FALSE, echo=TRUE}library(reticulate)py_install("numpy")
> use_condaenv("base")
> ```
> ```{python}import numpy as np```
>
> I got error message below:
> Error in py_call_impl(callable, dots$args, dots$keywords) :   
> ModuleNotFoundError: No module named 'numpy'Calls:  ... 
> py_capture_output -> force ->  -> py_call_implIn addition: There 
> were 26 warnings (use warnings() to see them)Execution halted
>
>
> Based on message, the python can not find numpy package. But I'm sure I 
> installed the package. I don't know how to fix the problem. please help
> Thank you,Kai
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> [[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] Fwd: Reg: Frequency in declaring time series data

2023-01-17 Thread Eric Berger
Hi Upananda,
A few comments:
1. As you know, CRAN has thousands of packages. One of the ways to
learn about the packages you might care about is to use the CRAN
views. A 'view' is an attempt to provide some information on a certain
subset of the packages related to a particular area.
See a list of the views at: https://cran.r-project.org/web/views/
2. In your case, you might be interested in the 'Time Series' view:
https://cran.r-project.org/web/views/TimeSeries.html
3. On that page, the subsection 'Time Series Classes' mentions two
packages, 'tsbox' and 'timetk' which provide tools to convert between
different representations of time series used in different packages. I
have not used either of these packages, but perhaps they could be of
use to you.
4. For financial time series, it is often useful to have the exact
dates. Rui mentioned the zoo package. I often use the xts package.
xts = eXtensible Time Series. The xts package is an extension of the
zoo package.
5. If your data is coming from market prices, and you have daily data
corresponding to market prices, then presumably you don't have weekend
or holiday prices (when the markets are closed). This leads to about
250 or 252 trading days per year (52 weeks X 5 days/wk = 260, minus
8-10 holidays). It is standard in financial calculations to annualize
based on 250 or 252. So, you might want to specify a frequency of 250
or 252 in your time series when creating a ts object.

I hope that helps,
Eric


On Tue, Jan 17, 2023 at 6:29 PM Upananda Pani  wrote:
>
> Hi Rui,
>
> Thank you so much for your help. As I have to fit a Markov Switching Model
> using  MSwM package.
>
> May I know whether i can convert from zoo object to a time series object.
>
> As I have to use several packages which uses ts so I am not able to decide
> how to do it.
>
> Grateful to you for your help and support.
>
> With sincere regards,
> Upananda
>
> On Tue, 17 Jan, 2023, 01:40 Rui Barradas,  wrote:
>
> > Às 16:39 de 16/01/2023, Upananda Pani escreveu:
> > > Dear All,
> > >
> > > I have a time series daily data with date are stored ( %dd-%mm-%yy
> > format )
> > > from 22-01-20 to 03-08-21. In total I have 560 observations. I am using
> > the
> > > following command to declare as a time series object. Here the the data
> > set
> > > is 7 days a week.
> > > oil <- read_xlsx("crudefinal.xlsx")
> > > pricet=ts(oil$price, start = c(2020, 22), freq = 365)
> > > roilt=ts(diff(log(oil$price))*100,start=c(2020,22), freq=365)
> > >
> > > Shall I have to declare the dates here?  I want to know also if it is a 5
> > > day trading a week, how to declare the frequency.
> > >
> > > Looking forward to your reply
> > >
> > > Regards,
> > > Upananda Pani
> > >
> > > Looking forward to your suggestions.
> > >
> > >   [[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.
> > Hello,
> >
> > Package zoo is the best way of having dates in a time series. The
> > package comes with several vignettes that can get you started quickly.
> > See the difference between classes ts and zoo below.
> >
> > # make up some test data
> > oil <- data.frame(price = cumsum(rnorm(560)))
> > oil$date <- seq(as.Date("2020-01-22"), by = "1 day", length.out = 560)
> >
> > # base R
> > pricet <- ts(oil$price, start = c(2020, 22), freq = 365)
> > time(pricet)
> > index(pricet)
> > plot(pricet)
> >
> > #---
> >
> > library(zoo)
> > library(ggplot2)
> >
> > pricez <- zoo(oil$price, order.by = oil$date)
> > time(pricez)
> > index(pricez)
> > autoplot(pricez)
> >
> > vignette(package = "zoo")
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> >
> >
>
> [[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] Printing special characters

2023-01-16 Thread Eric Berger
How about  just 'EVENT > 30 sec' or 'EVENT > 29 sec' ?


On Mon, Jan 16, 2023 at 10:19 AM Dennis Fisher  wrote:

> R 4.2.2
> OS X
>
> Colleagues
>
> A file that I have read includes strings like this:
> "EVENT ≥ 30 sec"
> When I include the string in a graphic using:
> mtext(STRING, …)
> it appears as:
> "EVENT ... 30 sec"
>
> Is there a simple work-around (short of reformatting all the strings, then
> using plotmath)?
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
> __
> 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] Removing variables from data frame with a wile card

2023-01-13 Thread Eric Berger
mydata[, -grep("^yr",colnames(mydata))]

On Sat, Jan 14, 2023 at 8:57 AM Steven T. Yen  wrote:

> I have a data frame containing variables "yr3",...,"yr28".
>
> How do I remove them with a wild cardsomething similar to "del yr*"
> in Windows/doc? Thank you.
>
>  > colnames(mydata)
>[1] "year"   "weight" "confeduc"   "confothr" "college"
>[6] ...
>   [41] "yr3""yr4""yr5""yr6" "yr7"
>   [46] "yr8""yr9""yr10"   "yr11" "yr12"
>   [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
>   [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
>   [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
>   [66] "yr28"...
>
> __
> 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] Topological network Map

2023-01-11 Thread Eric Berger
Google using "R package subway map"
produced
https://www.r-bloggers.com/2021/01/non-hierarchical-edge-bundling-flow-maps-and-metro-maps-in-r/

which looks promising

On Wed, Jan 11, 2023 at 2:48 PM Nick Wray  wrote:
>
> Hello I'd like to create a simple topological map of a river system, ie all
> the waterways, where they join and points on them such as gauging stations
> so that it looks something like the London Tube map - where distances are
> not important but the relationships of the nodes are.
> I could probably Heath-Robinson something together but I wondered whether
> there was any kind of package already out there to save me reinventing the
> wheel.  I've had a look but I can't find anything which seems to do this.
> Does anyone know whether there is already such a thing or is this an
> opportunity to create my first candidate R package?
> 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] Reg: ggplot error

2023-01-11 Thread Eric Berger
No code or data came through.
Please read the posting guidelines.


On Wed, Jan 11, 2023 at 1:38 PM Upananda Pani  wrote:
>
> Dear All,
>
> I am using roptest  function of package "ROptEst" (Kohl and Ruckdeschel
> (2019)) to find out the ML, CvM-MD, and the RMX estimator and their
> asymptotic confidence intervals. I am assuming 1-5% of erroneous data for
> the RMX estimator.
>
> Then I am trying to Plot the data in the form of a histogram and add the
> three Gamma distribution densities with the estimated parameters and
> validate the three models additionally with pp- and qq-plots.
>
> I have tried to code it. I have attached the code and data. I am getting
> error while fitting ggplot to plot the distribution densities.
>
> I am doing some error which I am not able to correct. Please help me to
> find out my error.
>
> With sincere regards,
> Upananda Pani
> __
> 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] Reg: Help in assigning colors to factor variable in ggplot2

2022-12-26 Thread Eric Berger
Your problem is that the color "#4DAF4A8" is not a valid color. It should
have only 6 hex digits.
e.g. "#4DAF4A" will work.

Also you seem to be confused about ggplot. You are not using ggplot in your
code at all.
You are using base graphics. You can remove the 3 library calls.

HTH,
Eric


On Mon, Dec 26, 2022 at 4:06 PM Upananda Pani 
wrote:

> Dear All,
>
> I am trying to plot a scatter plot between  temperature and heart rate and
> additionally marking the outcome of the patients by colors. I am using the
> standard package Use the standard function plot as well as the functions of
> package "ggplot2" (Wickham (2009)). Save the plots in pdf files.
>
> I am geeting an error to plot when assigning colsOutcome to the
> scatterplot. I am doing it wrongly. Please advise me.
> ```{r}
> library(ggplot2)
> library(RColorBrewer)
> library(ggsci)
> ICUData <- read.csv(file = "ICUData.csv")
> ```
> ```{r}
> ## Generate empty vector
> colsOutcome <- character(nrow(ICUData))
> ## Fill with colors
> colsOutcome[ICUData$outcome == "died"] <- "#E41A1C"
> colsOutcome[ICUData$outcome == "home"] <- "#377EB8"
> colsOutcome[ICUData$outcome == "other hospital"] <- "#4DAF4A8"
> colsOutcome[ICUData$outcome == "secondary care/rehab"] <- "#984EA3"
> ```
>
> ```{r}
> plot(x = ICUData$temperature, y = ICUData$heart.rate, pch = 19,
>  xlab = "Maximum body temperature", ylab = "Maximum heart rate",
>  main = "500 ICU patients", col = colsOutcome, xlim = c(33,43))
> legend(x = "topleft", legend = c("died", "home", "other hospital",
> "secondary care/rehab"), pch = 19,
>col = c("#E41A1C", "#377EB8", "#4DAF4A8", "#984EA3"))
> ```
> __
> 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] Amazing AI

2022-12-19 Thread Eric Berger
Hi Boris,
I think these are good questions.
Some initial reactions:
1. with better tools available to the students, you can cover more material
at a faster pace 
2. For years, it has been possible for students to find "answers" online
(e.g. Google search). Most programmers would regard this as an essential
part of their work - looking to overcome some obstacle via searching on
StackOverflow, Google, R-Help etc*. I consider the ability to do such
searches something worth teaching. In this spirit, you could teach
effective ways to use ChatGPT.
3. Finally, as you observed, some of the ChatGPT responses have
bugs/deficiencies. For example, the Codex code does *not* remove
duplicates. It requires knowledge and skill of the material you are
teaching, for the students to be able to spot and fix the bugs. In which
case, what's your problem? 

HTH,
Eric

* and as a shout-out to Bert, they should also read the documentation -
e.g. ?subset

On Mon, Dec 19, 2022 at 12:47 AM Ebert,Timothy Aaron  wrote:

> It would help students formulate a plan for coding. Successful students
> will be able to give good directions that the AI can turn into good code.
> This skill is essential no matter who writes the program.
> In more advanced classes I might collect some data sets designed to cause
> the AI problems. Another option is to make tests where students have to
> write code on paper or multiple guess where students must choose between
> similar code snips.
>
>
> -Original Message-
> From: R-help  On Behalf Of Boris Steipe
> Sent: Sunday, December 18, 2022 2:01 PM
> To: r-help@r-project.org
> Subject: [R] Amazing AI
>
> [External Email]
>
> Technically not a help question. But crucial to be aware of, especially
> for those of us in academia, or otherwise teaching R. I am not aware of a
> suitable alternate forum. If this does not interest you, please simply
> ignore - I already know that this may be somewhat OT.
>
> Thanks.
> --
>
> You very likely have heard of ChatGPT, the conversation interface on top
> of the GPT-3 large language model and that it can generate code. I thought
> it doesn't do R - I was wrong. Here is a little experiment:
>
> MY PROMPT:
> ==
> Please write me some R code that will read a tsv file, remove all rows
> that have duplicates in the column "names", and store the median values of
> the remaining rows in a variable called "Medians". Of course, that
> calculation should only be done with columns that are numeric. Print the
> last variable in order from largest to smallest.
>
>
> AI RESPONSE:
> 
>
> Here is some R code that should accomplish what you've described:
>
> (Note: the code is formatted in a little terminal window, complete with a
> button to "copy code" at the top.)
>
> 
> # Read the tsv file
> data <- read.table("file.tsv", header = TRUE, sep = "\t")
>
> # Remove rows with duplicates in the "names" column data <-
> data[!duplicated(data$names), ]
>
> # Select only the numeric columns
> num_columns <- sapply(data, is.numeric)
> numeric_data <- data[, num_columns]
>
> # Calculate the medians for each column
> Medians <- apply(numeric_data, 1, median)
>
> # Sort Medians in descending order
> Medians <- sort(Medians, decreasing = TRUE)
>
> # Print Medians
> print(Medians)
> 
>
> This code assumes that the tsv file is called "file.tsv" and is stored in
> the current working directory. It also assumes that the first row of the
> file contains the column names.
>
>
>
> MY PROMPT:
> ==
> Can you provide me with a bit of sample data that I can use to test this
> code?
>
> AI RESPONSE:
> 
> Certainly! Here is some sample data that you can use to test the code I
> provided:
>
> 
> nameage height  weight
> Alice   23  5.6 120
> Bob 32  6.2 180
> Charlie 29  5.9 160
> Alice   23  5.6 120
> David   25  5.8 140
> Eve 27  5.5 110
> 
>
> You can save this data as a tsv file and use it to test the code.
>
>
> --
>
> Notes:
> 1) The code, as written does not work with the test data. The reason is
> that the test data specifies a column "name", but the code assumes "names"
> (which I specified in the instructions). Once this is fixed in the test
> data, the code executes.
>
> 2) The instructions are not accurately translated: I had asked for row
> means. But changing the margin in the apply() statement from 2 to 1 does
> the correct thing.
>
> 3) Note that the test data contains both numeric and non-numeric columns.
> Also, the name "Alice" is duplicated in the test data, which is subtle, and
> the right thing to do.
>
>
> --
>
> A similar experiment, run on the beta-site of OpenAI's Codex
> code-completion tool gives the following code (complete with the comments.):
>
> 
> # Read in the data
> data <- 

Re: [R] is.na()<- on a character vector

2022-12-16 Thread Eric Berger
maybe this will make it clear

x <- c(a=1, b=5)
is.na(x) <- "b"

i.e. your second case is dealing with a named vector

HTH,
Eric


On Fri, Dec 16, 2022 at 8:29 PM Göran Broström 
wrote:

> I'm confused:
>
> > x <- 1:2
> > is.na(x) <- 1
> > x
> [1] NA  2
>
> OK, but
>
> > x <- c("A", "B")
> > is.na(x) <- "A"
> > x
>A
> "A" "B"  NA
>
> What happens?
>
> G_ran
>
> __
> 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] What is new in R especially about Tidyverse.

2022-11-27 Thread Eric Berger
Hi Abdullah,
The Tidyverse is a set of R packages that are designed to work well
together for handling a variety of common tasks in data science. Many of
these packages are written by Hadley Wickham, chief scientist at
RStudio.com.

https://en.wikipedia.org/wiki/Hadley_Wickham

RStudio.com is a commercial company which provides both free and non-free
products for data science. Many R users use the free IDE RStudio provided
by them. The company recently changed its name from RStudio.com to Posit,
as it wants to emphasize that its tools are not restricted to R (e.g.
Python is supported, Quarto for documents, etc)

I suggest you look at the resources available at https::/posit.co as a
start.

Besides the tidyverse packages, you might want to investigate how different
groups have used the tidyverse philosophy (and packages) to provide groups
of packages for handling specific sub-disciplines in data science. For
example, Rob Hyndman's group has created the tidyverts (the 'ts' refers to
time series) which extends the tidyverse packages and also contributed
additional packages. The tidyverts facilitates time series forecasting.

Good luck,
Eric



On Sun, Nov 27, 2022 at 10:16 AM Abdullah DOĞRUL 
wrote:

> To whom it may concern,
>
> Currently I'm doing my MSc in Turkey. My department is Industrial
> Engineering. I'm going to do my thesis about Tidyverse in R. I'm looking
> for articles which contain what is new, what changed and what is
> Tidyverse's contributions?
>
> I need a help about how to find those articles. I have found some of
> articles. But, probably they'll not help much.
>
> Thank you for your help.
> Best regards.
>
> Abdullah DOĞRUL
>
> [[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.


Re: [R] IGCI implemented in R package ?

2022-11-21 Thread Eric Berger
Hi Sacha,
My search turned up the Causal Discovery Toolbox which includes IGCI among
10 (or more) methods.
This site includes both Python and R resources and seems to be quite rich -
hopefully you will find something useful.
Here's the link:
https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/index.html

HTH,
Eric


On Mon, Nov 21, 2022 at 9:00 PM varin sacha via R-help 
wrote:

> Dear R experts,
>
> Google is very often my friend but this time it does not !
> Are you aware of an R package in which the directed causal discovery
> algorithm called the Information Geometric Causal Inference (IGCI) of
> (Daniusis et al., 2010) is implemented ?
>
> Best,
> Sacha
>
> Envoyé de mon iPhone
> __
> 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] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-08 Thread Eric Berger
Hi Tiffany,
You can replace

Conc_min <- lapply(1:N, function(i) { ...

by

Conc_min <- lapply(seq_len(nrow(pts)), function(i) { ..

Best,
Eric

On Wed, Nov 9, 2022 at 4:38 AM Duhl, Tiffany R.  wrote:
>
> First off, thanks SO much Eric and Micha for your help on this problem!  I 
> think Micha's spatially-oriented solution with Eric's slight modifications 
> will work best for my application but there is one snag (see the commented 
> section near the end of the following code)-- basically I don't know how to 
> apply the lapply operator to a list with a variable length (namely the length 
> of my input csv files) rather than the fixed length that Eric used:
>
> library(sf)
> library(spdep)
> data<- read.csv("R_find_pts_testdata.csv")
>
> MAX_DIST <- 0.05  #50 m in km, the units of dnearneigh when coords are in 
> degrees
> pts <- st_as_sf(data, coords=c('LON', 'LAT'), crs=4326)
> dist_matrix <- dnearneigh(pts, 0, MAX_DIST, use_s2=TRUE)
>
> #Micha's function to get the minimum Conc value among all points
> #within the buffer distance to a given single point:
> # Function to get minimum Conc values for one row of distance matrix
>
> MinConc <- function(x, lst, pts) {
>Concs <- lapply(lst, function(p) {
>  pts$Conc[p]
>})
>return(min(Concs[[1]]))
> }
>
> # above, x is an index to a single point, lst is a list of point indices
> #from distance matrix within the buffer distance
>
> #Next run function on all points to get list of minimum Conc
> #values for all points, and merge back to pts.
> #...modified by Eric to include original point
>
> return(min(c(Concs[[1]], pts$Conc[x])))
>
> # Now apply this function to all points in pts
> ###This is where the problem is, I think:
> ###Eric had used N <- 1000L and later
> ###Conc_min <- lapply(1:N, function(i) {
> ### MinConc(i, dist_matrix[i], pts)})
> ###But I need the length of the list the function is applied to
> ###to be variable, depending on the length of the input csv file
> ###unlike the dummy variable dataframe that Eric used with a set length
> ###So I changed the "x" argument in lapply to "pts$X" but that generates an 
> empty list
>
> Conc_min <- lapply(pts$X, function(i){
>MinConc(i, dist_matrix[i], pts)
> #})
> Conc_min <- data.frame("Conc_min" = as.integer(Conc_min))
>
> # Add back as new attrib to original points sf object
> pts_with_min <- do.call(cbind, c(pts, Conc_min))
>
>
>
> Many thanks again for your help on this!
> Best regards,
> -Tiffany
> 
> From: Micha Silver 
> Sent: Monday, November 7, 2022 8:11 AM
> To: Duhl, Tiffany R. ; Eric Berger 
> 
> Cc: R-help 
> Subject: Re: [R] [External] Re: Selecting a minimum value of an attribute 
> associated with point values neighboring a given point and assigning it as a 
> new attribute
>
> Eric's solution notwithstanding, here's a more "spatial" approach.
>
>
> I first create a fictitious set of 1000 points (and save to CSV to
> replicate your workflow)
>
> library(sf)
> library(spdep)
>
> # Prepare fictitious data
> # Create a data.frame with 1000 random points, and save to CSV
> LON <- runif(1000, -70.0, -69.0)
> LAT <- runif(1000, 42.0, 43.0)
> Conc <- runif(1000, 9, 10)
> df <- data.frame(LON, LAT, Conc)
> csv_file = "/tmp/pts_testdata.csv"
> write.csv(df, csv_file)
>
>
> Now read that CSV back in directly as an sf object (No need for the old
> SpatialPointsDataFrame). THen create a distance matrix for all points,
> which contains indicies to those points within a certain buffer
> distance, just as you did in your example.
>
>
> # Read back in as sf object, including row index
> pts <- st_as_sf(read.csv(csv_file), coords=c('LON', 'LAT'), crs=4326)
> dist_matrix <- dnearneigh(pts, 0, 100, use_s2=TRUE) # use_s2 since these
> are lon/lat
>
> Now I prepare a function to get the minimum Conv value among all points
> within the buffer distance to a given single point:
> # Function to get minimum Conc values for one row of distance matrix
> MinConc <- function(x, lst, pts) {
># x is an index to a single point,
># lst is a list of point indices from distance matrix
># that are within the buffer distance
>Concs <- lapply(lst, function(p) {
>  pts$Conc[p]
>})
>return(min(Concs[[1]]))
> }
>
> Next run that function on all points to get a list of minimum Conv
> values for all points, and merge back to pts.
>
>
> # Now apply this function to all points in pts
> Conc_min <- lapply(pts$X, function(i){
>MinConc(i, dist_matrix[i], pts)
&

Re: [R] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-07 Thread Eric Berger
 that function on all points to get a list of minimum Conv
> values for all points, and merge back to pts.
>
>
> # Now apply this function to all points in pts
> Conc_min <- lapply(pts$X, function(i){
>MinConc(i, dist_matrix[i], pts)
> })
> Conc_min <- data.frame("Conc_min" = as.integer(Conc_min))
>
> # Add back as new attrib to original points sf object
> pts_with_min <- do.call(cbind, c(pts, Conc_min))
>
> HTH,
>
> Micha
>
>
>
> On 06/11/2022 18:40, Duhl, Tiffany R. wrote:
> > Thanks so much Eric!
> >
> >   I'm going to play around with your toy code (pun intended) & see if I can 
> > make it work for my application.
> >
> > Cheers,
> > -Tiffany
> > 
> > From: Eric Berger 
> > Sent: Sunday, November 6, 2022 10:27 AM
> > To: Bert Gunter 
> > Cc: Duhl, Tiffany R. ; R-help 
> > Subject: [External] Re: [R] Selecting a minimum value of an attribute 
> > associated with point values neighboring a given point and assigning it as 
> > a new attribute
> >
> > Whoops ... left out a line in Part 2. Resending with the correction
> >
> > ## PART 2: You can use this code on the real data with f() defined 
> > appropriately
> > A <- matrix(0,N,N)
> > v <- 1:N
> > ## get the indices (j,k) where j < k (as columns in a data.frame)
> > idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
> > u <- sapply(1:nrow(idx),
> > \(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<- f(j,k,myData) })
> > B <- A + t(A) + diag(N)
> > C <- diag(myData$Conc)
> > D <- B %*% C
> > D[D==0] <- NA
> > myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
> > print(head(myData))
> >
> > On Sun, Nov 6, 2022 at 5:19 PM Eric Berger  wrote:
> >> Hi Tiffany,
> >> Here is some code that might help with your problem. I solve a "toy"
> >> problem that is conceptually the same.
> >> Part 1 sets up my toy problem. You would have to replace Part 1 with
> >> your real case. The main point is to define
> >> a function f(i, j, data) which returns 0 or 1 depending on whether the
> >> observations in rows i and j in your dataset 'data'
> >> are within your cutoff distance (i.e. 50m).
> >>
> >> You can then use Part 2 almost without changes (except for changing
> >> 'myData' to the correct name of your data).
> >>
> >> I hope this helps,
> >> Eric
> >>
> >> library(dplyr)
> >>
> >> ## PART 1: create fake data for minimal example
> >> set.seed(123) ## for reproducibility
> >> N <- 5   ## replace by number of locations (approx 9000 in your case)
> >> MAX_DISTANCE <- 2  ## 50 in your case
> >> myData <- data.frame(x=rnorm(N),y=rnorm(N),Conc=sample(1:N,N))
> >>
> >> ## The function which you must re-define for your actual case.
> >> f <- function(i,j,a) {
> >>   dist <- sqrt(sum((a[i,1:2] - a[j,1:2])^2)) ## Euclidean distance
> >>   as.integer(dist < MAX_DISTANCE)
> >> }
> >>
> >> ## PART 2: You can use this code on the real data with f() defined 
> >> appropriately
> >> A <- matrix(0,N,N)
> >> ## get the indices (j,k) where j < k (as columns in a data.frame)
> >> idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
> >> u <- sapply(1:nrow(idx),\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<-
> >> f(j,k,myData) })
> >> B <- A + t(A) + diag(N)
> >> C <- diag(myData$Conc)
> >> D <- B %*% C
> >> D[D==0] <- NA
> >> myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
> >> print(head(myData))
> >>
> >>
> >> On Sat, Nov 5, 2022 at 5:14 PM Bert Gunter  wrote:
> >>> Probably better posted on R-sig-geo.
> >>>
> >>> -- Bert
> >>>
> >>> On Sat, Nov 5, 2022 at 12:36 AM Duhl, Tiffany R. 
> >>> wrote:
> >>>
> >>>> Hello,
> >>>>
> >>>> I have sets of spatial points with LAT, LON coords (unprojected, WGS84
> >>>> datum) and several value attributes associated with each point, from
> >>>> numerous csv files (with an average of 6,000-9,000 points in each file) 
> >>>> as
> >>>> shown in the following example:
> >>>>
> >>>> data<- read.csv("R_find_pts_testdata.csv")
> >>&g

Re: [R] Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-06 Thread Eric Berger
Whoops ... left out a line in Part 2. Resending with the correction

## PART 2: You can use this code on the real data with f() defined appropriately
A <- matrix(0,N,N)
v <- 1:N
## get the indices (j,k) where j < k (as columns in a data.frame)
idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
u <- sapply(1:nrow(idx),
   \(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<- f(j,k,myData) })
B <- A + t(A) + diag(N)
C <- diag(myData$Conc)
D <- B %*% C
D[D==0] <- NA
myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
print(head(myData))

On Sun, Nov 6, 2022 at 5:19 PM Eric Berger  wrote:
>
> Hi Tiffany,
> Here is some code that might help with your problem. I solve a "toy"
> problem that is conceptually the same.
> Part 1 sets up my toy problem. You would have to replace Part 1 with
> your real case. The main point is to define
> a function f(i, j, data) which returns 0 or 1 depending on whether the
> observations in rows i and j in your dataset 'data'
> are within your cutoff distance (i.e. 50m).
>
> You can then use Part 2 almost without changes (except for changing
> 'myData' to the correct name of your data).
>
> I hope this helps,
> Eric
>
> library(dplyr)
>
> ## PART 1: create fake data for minimal example
> set.seed(123) ## for reproducibility
> N <- 5   ## replace by number of locations (approx 9000 in your case)
> MAX_DISTANCE <- 2  ## 50 in your case
> myData <- data.frame(x=rnorm(N),y=rnorm(N),Conc=sample(1:N,N))
>
> ## The function which you must re-define for your actual case.
> f <- function(i,j,a) {
>  dist <- sqrt(sum((a[i,1:2] - a[j,1:2])^2)) ## Euclidean distance
>  as.integer(dist < MAX_DISTANCE)
> }
>
> ## PART 2: You can use this code on the real data with f() defined 
> appropriately
> A <- matrix(0,N,N)
> ## get the indices (j,k) where j < k (as columns in a data.frame)
> idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
> u <- sapply(1:nrow(idx),\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<-
> f(j,k,myData) })
> B <- A + t(A) + diag(N)
> C <- diag(myData$Conc)
> D <- B %*% C
> D[D==0] <- NA
> myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
> print(head(myData))
>
>
> On Sat, Nov 5, 2022 at 5:14 PM Bert Gunter  wrote:
> >
> > Probably better posted on R-sig-geo.
> >
> > -- Bert
> >
> > On Sat, Nov 5, 2022 at 12:36 AM Duhl, Tiffany R. 
> > wrote:
> >
> > > Hello,
> > >
> > > I have sets of spatial points with LAT, LON coords (unprojected, WGS84
> > > datum) and several value attributes associated with each point, from
> > > numerous csv files (with an average of 6,000-9,000 points in each file) as
> > > shown in the following example:
> > >
> > > data<- read.csv("R_find_pts_testdata.csv")
> > >
> > > > data
> > > ID  Date TimeLATLON   Conc
> > > Leg.SpeedCO2  H2O BC61 Hr Min Sec
> > > 1   76 4/19/2021 21:25:38 42.40066 -70.98802 99300   0.0 mph 428.39 9.57
> > > 578 21  25  38
> > > 2   77 4/19/2021 21:25:39 42.40066 -70.98802 96730   0.0 mph 428.04 9.57
> > > 617 21  25  39
> > > 3   79 4/19/2021 21:25:41 42.40066 -70.98802 98800   0.2 mph 427.10 9.57
> > > 1027 21  25  41
> > > 4   80 4/19/2021 21:25:42 42.40066 -70.98802 96510 2 mph 427.99 9.58
> > > 1381 21  25  42
> > > 5   81 4/19/2021 21:25:43 42.40067 -70.98801 95540 3 mph 427.99 9.58
> > > 1271 21  25  43
> > > 6   82 4/19/2021 21:25:44 42.40068 -70.98799 94720 4 mph 427.20 9.57
> > > 910 21  25  44
> > > 7   83 4/19/2021 21:25:45 42.40069 -70.98797 94040 5 mph 427.18 9.57
> > > 652 21  25  45
> > > 8   84 4/19/2021 21:25:46 42.40072 -70.98795 95710 7 mph 427.07 9.57
> > > 943 21  25  46
> > > 9   85 4/19/2021 21:25:47 42.40074 -70.98792 96200 8 mph 427.44 9.56
> > > 650 21  25  47
> > > 10  86 4/19/2021 21:25:48 42.40078 -70.98789 9375010 mph 428.76 9.57
> > > 761 21  25  48
> > > 11  87 4/19/2021 21:25:49 42.40081 -70.98785 9336011 mph 429.25 9.56
> > > 1158 21  25  49
> > > 12  88 4/19/2021 21:25:50 42.40084 -70.98781 9434012 mph 429.56 9.57
> > > 107 21  25  50
> > > 13  89 4/19/2021 21:25:51 42.40087 -70.98775 9278012 mph 428.62 9.56
> > > 720 21  25  51
> > >
> > >
> > > What I want to do is, for each point, identify all points within 50m of
> > > that point, find the minimum va

Re: [R] Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute

2022-11-06 Thread Eric Berger
Hi Tiffany,
Here is some code that might help with your problem. I solve a "toy"
problem that is conceptually the same.
Part 1 sets up my toy problem. You would have to replace Part 1 with
your real case. The main point is to define
a function f(i, j, data) which returns 0 or 1 depending on whether the
observations in rows i and j in your dataset 'data'
are within your cutoff distance (i.e. 50m).

You can then use Part 2 almost without changes (except for changing
'myData' to the correct name of your data).

I hope this helps,
Eric

library(dplyr)

## PART 1: create fake data for minimal example
set.seed(123) ## for reproducibility
N <- 5   ## replace by number of locations (approx 9000 in your case)
MAX_DISTANCE <- 2  ## 50 in your case
myData <- data.frame(x=rnorm(N),y=rnorm(N),Conc=sample(1:N,N))

## The function which you must re-define for your actual case.
f <- function(i,j,a) {
 dist <- sqrt(sum((a[i,1:2] - a[j,1:2])^2)) ## Euclidean distance
 as.integer(dist < MAX_DISTANCE)
}

## PART 2: You can use this code on the real data with f() defined appropriately
A <- matrix(0,N,N)
## get the indices (j,k) where j < k (as columns in a data.frame)
idx <- expand.grid(v,v) |> rename(j=Var1,k=Var2) |> filter(j < k)
u <- sapply(1:nrow(idx),\(i){ j <- idx$j[i]; k <- idx$k[i]; A[j,k] <<-
f(j,k,myData) })
B <- A + t(A) + diag(N)
C <- diag(myData$Conc)
D <- B %*% C
D[D==0] <- NA
myData$Conc_min <- apply(D,MAR=1,\(v){min(v,na.rm=TRUE)})
print(head(myData))


On Sat, Nov 5, 2022 at 5:14 PM Bert Gunter  wrote:
>
> Probably better posted on R-sig-geo.
>
> -- Bert
>
> On Sat, Nov 5, 2022 at 12:36 AM Duhl, Tiffany R. 
> wrote:
>
> > Hello,
> >
> > I have sets of spatial points with LAT, LON coords (unprojected, WGS84
> > datum) and several value attributes associated with each point, from
> > numerous csv files (with an average of 6,000-9,000 points in each file) as
> > shown in the following example:
> >
> > data<- read.csv("R_find_pts_testdata.csv")
> >
> > > data
> > ID  Date TimeLATLON   Conc
> > Leg.SpeedCO2  H2O BC61 Hr Min Sec
> > 1   76 4/19/2021 21:25:38 42.40066 -70.98802 99300   0.0 mph 428.39 9.57
> > 578 21  25  38
> > 2   77 4/19/2021 21:25:39 42.40066 -70.98802 96730   0.0 mph 428.04 9.57
> > 617 21  25  39
> > 3   79 4/19/2021 21:25:41 42.40066 -70.98802 98800   0.2 mph 427.10 9.57
> > 1027 21  25  41
> > 4   80 4/19/2021 21:25:42 42.40066 -70.98802 96510 2 mph 427.99 9.58
> > 1381 21  25  42
> > 5   81 4/19/2021 21:25:43 42.40067 -70.98801 95540 3 mph 427.99 9.58
> > 1271 21  25  43
> > 6   82 4/19/2021 21:25:44 42.40068 -70.98799 94720 4 mph 427.20 9.57
> > 910 21  25  44
> > 7   83 4/19/2021 21:25:45 42.40069 -70.98797 94040 5 mph 427.18 9.57
> > 652 21  25  45
> > 8   84 4/19/2021 21:25:46 42.40072 -70.98795 95710 7 mph 427.07 9.57
> > 943 21  25  46
> > 9   85 4/19/2021 21:25:47 42.40074 -70.98792 96200 8 mph 427.44 9.56
> > 650 21  25  47
> > 10  86 4/19/2021 21:25:48 42.40078 -70.98789 9375010 mph 428.76 9.57
> > 761 21  25  48
> > 11  87 4/19/2021 21:25:49 42.40081 -70.98785 9336011 mph 429.25 9.56
> > 1158 21  25  49
> > 12  88 4/19/2021 21:25:50 42.40084 -70.98781 9434012 mph 429.56 9.57
> > 107 21  25  50
> > 13  89 4/19/2021 21:25:51 42.40087 -70.98775 9278012 mph 428.62 9.56
> > 720 21  25  51
> >
> >
> > What I want to do is, for each point, identify all points within 50m of
> > that point, find the minimum value of the "Conc" attribute of each nearby
> > set of points (including the original point) and then create a new variable
> > ("Conc_min") and assign this minimum value to a new variable added to
> > "data".
> >
> > So far, I have the following code:
> >
> > library(spdep)
> > library(sf)
> >
> > setwd("C:\\mydirectory\\")
> > data<- read.csv("R_find_pts_testdata.csv")
> >
> > #make sure the data is a data frame
> > pts <- data.frame(data)
> >
> > #create spatial data frame and define projection
> > pts_coords <- cbind(pts$LON, pts$LAT)
> > data_pts <- SpatialPointsDataFrame(coords= pts_coords,
> > data=pts, proj4string = CRS("+proj=longlat +datum=WGS84"))
> >
> > #Re-project to WGS 84 / UTM zone 18N, so the analysis is in units of m
> > ptsUTM  <- sf::st_as_sf(data_pts, coords = c("LAT", "LON"), remove = F)%>%
> > st_transform(32618)
> >
> > #create 50 m buffer around each point then intersect with points and
> > finally find neighbors within the buffers
> > pts_buf <- sf::st_buffer(ptsUTM, 50)
> > coords  <- sf::st_coordinates(ptsUTM)
> > int <- sf::st_intersects(pts_buf, ptsUTM)
> > x   <- spdep::dnearneigh(coords, 0, 50)
> >
> > Now at this point, I'm not sure what to either the "int" (a sgbp list) or
> > "x" (nb object) objects (or even if I need them both)
> >
> > > int
> > Sparse geometry binary predicate list of length 974, where the predicate
> > was `intersects'
> > first 10 elements:
> >  1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
> >  2: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
> >  3: 1, 2, 

Re: [R] Single pdf of all R vignettes request

2022-10-31 Thread Eric Berger
You can see all the installed vignettes on your system with
`browseVignettes()`. (This opens a browser page.)
Alternatively `browseVignettes("pkgname")` for those related to a single
package.

HTH,
Eric


On Mon, Oct 31, 2022 at 11:37 AM Richard O'Keefe  wrote:

> Let's put some numbers on that.
> The CRAN package repository claims 18770 packages.
> That excludes packages in other repositories, of
> course; the total collection of vignettes may not
> be discoverable.
> It could be useful to collect documents and vignettes
> and stuff them into an information retrieval system,
> but I cannot imagine a single PDF being usable.
>
> On Mon, 31 Oct 2022 at 08:34, Jeff Newmiller 
> wrote:
>
> > No. It would be impractically large and would be out of date by the time
> > it was finished being assembled. And not all vignettes are built in PDF
> > form anyway.
> >
> > R packages are maintained by individuals or small teams completely
> > independently from R.
> >
> > On October 30, 2022 12:23:54 PM PDT, "Sun, John" 
> > wrote:
> > >Dear All,
> > >
> > >I am writing to ask whether there exists a single pdf of all the
> > vignettes from R packages.
> > >This would be good resource.
> > >
> > >Best regards,
> > >John
> > >
> > >__
> > >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.
> >
> > --
> > Sent from my phone. Please excuse my brevity.
> >
> > __
> > 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.
>

[[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] Color Nodes

2022-10-27 Thread Eric Berger
You may want to change the color of the vertex labels. e.g.
plot(g, vertex.size = 20, vertex.label.dist = 0.5,
  vertex.color = V(g)$color, vertex.label.color="cyan")



On Thu, Oct 27, 2022 at 1:52 AM Jeff Reichman  wrote:
>
> Yes Rui that will work for my needs thank you
>
> -Original Message-
> From: Rui Barradas 
> Sent: Wednesday, October 26, 2022 4:40 PM
> To: reichm...@sbcglobal.net; R-help@r-project.org
> Subject: Re: [R] Color Nodes
>
> Às 21:37 de 26/10/2022, Jeff Reichman escreveu:
> > R-Help
> >
> >
> >
> > For those of you who use igraph is there a way to highlight (color)
> > particular notes. For example if I want to color the "Peter" node red
> > and everything else blue  or leave default) how would I do that or can
> > I do that. Seems as though I might create an attribute column - maybe??
> >
> >
> >
> > Jeff
> >
> >
> >
> > library(igraph)
> >
> >
> >
> > # define links in data
> >
> > edges <- rbind(c("Dave", "Jenny"), c("Peter", "Jenny"), c("John",
> > "Jenny"), c("Dave", "Peter"), c("Dave", "John"), c("Peter", "Sam"),
> > c("Sam", "Albert"), c("Peter", "John"))
> >
> >
> >
> > # generate and plot graph
> >
> > # set argument directed = FALSE in graph.edgelist() to plot an
> > undirected graph.
> >
> > g <- graph.edgelist(edges, directed = FALSE)
> >
> > plot(g, vertex.size = 1, vertex.label.dist = 0.5)
> >
> >
> >   [[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.
>
> Hello,
>
> After creating the graph, set the vertices colors by subsetting V(g).
> Here are two exaples, the first color Peter red and leave the others with 
> their default color. The second, color the other vertices blue.
>
>
>
> V(g)["Peter"]$color <- "red"
>
> plot(g, vertex.size = 20, vertex.label.dist = 0.5,
>   vertex.color = V(g)$color)
>
> V(g)$color <- "blue"
> V(g)["Peter"]$color <- "red"
> plot(g, vertex.size = 20, vertex.label.dist = 0.5,
>   vertex.color = V(g)$color)
>
>
>
> Hope this helps,
>
> Rui Barradas
>
> __
> 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] Best place to ask questions about non-R Base topics, ex. dplyr, dbplyr, etc. ?

2022-10-26 Thread Eric Berger
Also, since your examples were dplyr and dbplyr which are associated
with RStudio.com (aka Posit) you might also try

https://community.rstudio.com

You can apply filters on that site to narrow the results



On Wed, Oct 26, 2022 at 10:12 PM Jeff Newmiller
 wrote:
>
> The general recommendation is to consult the contributed package web page on 
> CRAN for a URL entry or other recommendation for support, e.g. [1]. Other 
> forums include StackExchange for theory and StackOverflow for computing, and 
> there are several sub-reddits on Reddit related to R.
>
> [1] https://cran.r-project.org/package=dplyr
>
> On October 26, 2022 11:09:58 AM PDT, Kelly Thompson  
> wrote:
> >
> >__
> >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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> __
> 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] Unintended behaviour of stats::time not returning integers for the first cycle

2022-10-15 Thread Eric Berger
Alternatively

correct.year <- floor(time(x)+1e-6)

On Sat, Oct 15, 2022 at 10:26 AM Andreï V. Kostyrka <
andrei.kosty...@gmail.com> wrote:

> Dear all,
>
>
>
> I was using stats::time to obtain the year as a floor of it, and
> encountered a problem: due to a rounding error (most likely due to its
> reliance on the base::seq.int internally, but correct me if I am wrong),
> the actual number corresponding to the beginning of a year X can still be
> (X-1).999..., resulting in the following undesirable behaviour.
>
>
>
> One of the simplest ways of getting the year from a ts object is
> floor(time(...)). However, if the starting time cannot be represented
> nicely as a power of 2, then, supposedly integer time does not have a
> .00... mantissa:
>
>
>
> x <- ts(2:252, start = c(2002, 2), freq = 12)
>
> d <- seq.Date(as.Date("2002-02-01"), to = as.Date("2022-12-01"), by =
> "month")
>
> true.year <- rep(2002:2022, each = 12)[-1]
>
> wrong.year <- floor(as.numeric(time(x)))
>
> tail(cbind(as.character(d), true.year, wrong.year), 15) # Look at
> 2022-01-01
>
> print(as.numeric(time(x))[240], 20) # 2021.7726, the floor of
> which is 2021
>
>
>
> Yes, I have read the 'R inferno' book and know the famous '0.3 != 0.7 -
> 0.4' example, but I believe that the expected / intended behaviour would be
> actually returning round years for the first observation in a year. This
> could be achieved by rounding the near-integer time to integers.
>
>
>
> Since users working with dates are expecting to get exact integer years for
> the first cycle of a ts, this should be changed. Thank you in advance for
> considering a fix.
>
>
>
> Yours sincerely,
>
> Andreï V. Kostyrka
>
> [[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.


Re: [R] Fatal Error: Contains Space

2022-09-28 Thread Eric Berger
In the link I sent there is a solution suggested by Venk Murthy.

On Wed, Sep 28, 2022 at 5:14 AM Kaitlyn Light  wrote:
>
> Hello!
>
> That perfectly describes the problem I am facing but I did not see any 
> solutions suggested in the link. Do you happen to know of any? Thank you so 
> much for the help!!
>
> Best,
> Kaitlyn Light
>
> On Thu, Sep 22, 2022 at 3:42 AM Eric Berger  wrote:
>>
>> Does this help?
>> https://stackoverflow.com/questions/72138987/r-studio-fatal-error-r-tempdircontains-space
>>
>> On Thu, Sep 22, 2022 at 10:37 AM Kaitlyn Light  wrote:
>>>
>>> Hello!
>>> I recently downloaded R and RStudio to my windows laptop. I downloaded the
>>> correct version and made sure it was for windows and not mac. However, when
>>> I tried to open RStudio, a message saying " R_tempdir Fatal Error:
>>> Contains Space" would pop-up. The program would open as a blank screen and
>>> usually make the rest of my laptop stall until it was shut off. I tried
>>> uninstalling and reinstalling the programs, however, the issue did not
>>> change. I checked my Microsoft username to make sure it did not "contain
>>> space" (as in spacebars) but it did not. I was hoping I would be able to
>>> get some help moving forward on how to get this fixed. Thank you for your
>>> help!
>>>
>>> Best,
>>> Kaitlyn Light
>>>
>>> [[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] apply to row and column of matrix

2022-09-26 Thread Eric Berger
Bert provided an excellent answer to your question.
FYI here is a different approach to do the calculation.
It uses data.frame rather than matrix. A data frame is a list of its columns.
Here the function supplied to sapply operates on each column of the data.frame.

> m <- as.data.frame(t(matrix(1:10,ncol=2)))
> m
   V1  V2  V3  V4  V5
1   1 23 45
2678 9   10

> sapply(m, cumsum)
  V1  V2  V3  V4  V5
[1,]1 23 45
[2,]7 9   11   13  15





On Mon, Sep 26, 2022 at 5:57 PM Bert Gunter  wrote:
>
> from ?apply:
> "If each call to FUN returns a vector of length n, and simplify is
> TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) ."
>
> For margin = 1 (cumsum over rows), each call to cumsum return a vector
> of length 2. Hence the array returned will be of dimension c(2,
> c(5,2)[1]) = c(2,5).
>
> Cheers,
> Bert
>
>
> On Mon, Sep 26, 2022 at 7:43 AM Jinsong Zhao  wrote:
> >
> > Hi there,
> >
> > I try to calculate the cumsum of row and column of a matrix as follows.
> >
> >  > m <- matrix(1:10, ncol = 2)
> >  > m
> >   [,1] [,2]
> > [1,]16
> > [2,]27
> > [3,]38
> > [4,]49
> > [5,]5   10
> >  > apply(m, 1, cumsum)
> >   [,1] [,2] [,3] [,4] [,5]
> > [1,]12345
> > [2,]79   11   13   15
> >  > apply(m, 2, cumsum)
> >   [,1] [,2]
> > [1,]16
> > [2,]3   13
> > [3,]6   21
> > [4,]   10   30
> > [5,]   15   40
> >
> > My question is why the dim of the return value of apply(m, 1, cumsum) is
> > not 5x2, but 2x5.
> >
> > Best,
> >
> > Jinsong
> >
> > __
> > 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.

__
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] Solve equality function with GA

2022-09-25 Thread Eric Berger
?ga states that ga() searches for the maximum of the fitness function.
The maximum of your fitness function is at x=0.



On Sun, Sep 25, 2022 at 4:52 AM Bert Gunter  wrote:

> We aren't supposed to do homework on this list.
>
> Bert Gunter
>
> On Sat, Sep 24, 2022 at 5:29 PM Barry King via R-help <
> r-help@r-project.org>
> wrote:
>
> > I am having difficulty solving for 5 in this coded snippet. Please help.
> > ---
> > library(GA)
> >
> > # Solve for x where x^2 = 25, not x^2 <= 25
> >
> > f <- function(x)
> > {
> >   x^2-25
> > }
> >
> > fitness <- function(x) -f(x)
> >
> > GA <- GA <- ga(type = "real-valued", fitness = fitness, lower = 0, upper
> =
> > 10)
> > summary(GA)
> > -
> > Thank you,
> > Barry King
> >
> > --
> >
> >
> >
> >
> >
> >
> > This message is for the intended recipient only and may contain
> > confidential and privileged information. If you are not the intended
> > recipient, please return this message to the sender and delete this
> message
> > and any attachments from your computer.
> >
> > [[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.
>

[[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] Fatal Error: Contains Space

2022-09-22 Thread Eric Berger
Does this help?
https://stackoverflow.com/questions/72138987/r-studio-fatal-error-r-tempdircontains-space

On Thu, Sep 22, 2022 at 10:37 AM Kaitlyn Light  wrote:

> Hello!
> I recently downloaded R and RStudio to my windows laptop. I downloaded the
> correct version and made sure it was for windows and not mac. However, when
> I tried to open RStudio, a message saying " R_tempdir Fatal Error:
> Contains Space" would pop-up. The program would open as a blank screen and
> usually make the rest of my laptop stall until it was shut off. I tried
> uninstalling and reinstalling the programs, however, the issue did not
> change. I checked my Microsoft username to make sure it did not "contain
> space" (as in spacebars) but it did not. I was hoping I would be able to
> get some help moving forward on how to get this fixed. Thank you for your
> help!
>
> Best,
> Kaitlyn Light
>
> [[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.


Re: [R] Error in if (class(networks) == "matrix") from a function

2022-09-21 Thread Eric Berger
In R 4.2.0 there is a significant change. When you use an if() statement
with a condition of length > 1 this now reports an error.
e.g. this link mentions it as a change
https://www.jumpingrivers.com/blog/new-features-r420/

In your case this is because class(obj) can return a character vector of
length > 1 (all the classes).
One possible workaround would be:

if ( "matrix" %in% class(networks) ) ...

HTH,
Eric


On Wed, Sep 21, 2022 at 10:21 PM Chao Liu  wrote:

> Dear R-Help community,
>
> This is a crosspost on SO but I've had no luck so far. So I have a function
> which computes a centrality index for the nodes in a network or matrix.
> Here is the function:
>
> library(igraph) #load package igraph
> centrality <- function (networks, type = c("indegree", "outdegree",
> "freeman",
> "betweenness", "flow", "closeness", "eigenvector", "information",
> "load", "bonpow"), directed = TRUE, lag = 0, rescale = FALSE,
> center = FALSE, coefname = NULL, ...) {
> if (is.null(directed) || !is.logical(directed)) {
> stop("'directed' must be TRUE or FALSE.")
> }
> else if (length(directed) != 1) {
> stop("The 'directed' argument must contain a single logical
> value only.")
> }
> else if (directed == FALSE) {
> gmode <- "graph"
> }
> else {
> gmode <- "digraph"
> }
> objects <- checkDataTypes(y = NULL, networks = networks,
> lag = lag)
> centlist <- list()
> for (i in 1:objects$time.steps) {
> if (type[1] == "indegree") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "indegree", rescale = rescale, ...)
> }
> else if (type[1] == "outdegree") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "outdegree", rescale = rescale, ...)
> }
> else if (type[1] == "freeman") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "freeman", rescale = rescale, ...)
> }
> else if (type[1] == "betweenness") {
> cent <- betweenness(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "flow") {
> cent <- flowbet(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "closeness") {
> cent <- closeness(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "eigenvector") {
> cent <- evcent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "information") {
> cent <- infocent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "load") {
> cent <- loadcent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "bonpow") {
> cent <- bonpow(objects$networks[[i]], gmode = gmode,
> rescale = rescale, tol = 1e-20, ...)
> }
> else {
> stop("'type' argument was not recognized.")
> }
> centlist[[i]] <- cent
> }
> time <- numeric()
> y <- numeric()
> for (i in 1:objects$time.steps) {
> time <- c(time, rep(i, objects$n[[i]]))
> if (is.null(centlist[[i]])) {
> y <- c(y, rep(NA, objects$n[[i]]))
> }
> else {
> if (center == TRUE) {
> centlist[[i]] <- centlist[[i]] - mean(centlist[[i]],
>   na.rm = TRUE)
> }
> y <- c(y, centlist[[i]])
> }
> }
> if (is.null(coefname) || !is.character(coefname) || length(coefname) >
> 1) {
> coeflabel <- ""
> }
> else {
> coeflabel <- paste0(".", coefname)
> }
> if (lag == 0) {
> laglabel <- ""
> }
> else {
> laglabel <- paste0(".lag", paste(lag, collapse = "."))
> }
> label <- paste0(type[1], coeflabel, laglabel)
> dat <- data.frame(y, time = time, node = objects$nodelabels)
> dat$node <- as.character(dat$node)
> colnames(dat)[1] <- label
> attributes(dat)$lag <- lag
> return(dat)}
>
> Here is the matrix:
>
> dat <- read.table(text="A B #this is edgelist
> 1 2
> 1 3
> 1 2
> 2 1
> 2 3
> 3 1
> 3 2
> 3 1", header=TRUE)
> datmat <- as.matrix(get.adjacency(graph.edgelist(as.matrix(dat),
> directed=TRUE))) #this is the matrix
> colnames(datmat) <- c("1", "2", "3") #rename the columns
>
> When I applied the function to a matrix
> centrality(datmat,type="flow",center=TRUE), it returns the error:
>
> Error in if (class(networks) == "matrix") { :
>   the condition has length > 1
>
> What is the cause of this error? How to fix it? Any help will be greatly
> appreciated!
>
>

Re: [R] How to set default encoding for sourced files

2022-09-21 Thread Eric Berger
Hi Andrew,
If you look at ?source you see its default value for encoding is
picked up from getOption("encoding"). Couldn't you just set this
option in your Rprofile?

HTH,
Eric


On Wed, Sep 21, 2022 at 5:34 PM Andrew Hart via R-help
 wrote:
>
> Hi there. I'm working with some utf-8 incoded csv files which gives me
> data frames with utf-8 encoded headers. This means when I write things like
> dat$proporción
> in an R script and then source it, I have to make sure the R script is
> incoded using utf-8 (and not latin1) and then I also have to explicitly
> tell R that the encoding is utf-8 every time I source the file, that is,
> I need to type
> source("sr.R", encoding="utf-8").
>
> Sure, I could eliminate accents and so forth from the headers by
> renaming the data frame columns, and I have done and do do this, but I
> shouldn't be required to do this just to avoid encoding issues.
> We're living in the 21st century and imho Unicode-based encodings should
> be the de facto standard these days. I'm aware that R is pretty clever
> and stores the encoding along with the string value in all character
> objects and then converts on the fly as necessary. However, Almost
> everything I work with is in utf-8 or ASCII (which is compatible with
> utf-8 anyway),
> so I'd like R to behave as though it does everything natively in utf-8
> so I don't have to worry about it.
> Is there something in Rprofile.site or the user Rprofile or an
> environment variable I can set or some other way to instruct R to always
> assume that input stream encodings will be utf-8  unless otherwise
> specified? This way, I would only ever have to supply an encoding or
> fileEncoding argument to specify "latin1" if I happ en to encounter it.
>
> Many thanks,
> Andrew.
>
> __
> 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] aplpack / bagplot

2022-09-21 Thread Eric Berger
[re-sending as plain text]

Hi Sigbert,
I have never used the aplpack package but out of curiosity I tried it out.
Doing a scatter plot of your (x,y) data shows that there are many
repeated x values, and this seems to be the source of the error.
There are no repeated y values. It seems that the bagplot() function
does not handle repeated x-values.
I tried adding some noise to the x-values and that worked fine.

x2 <- x + rnorm(length(x),0,1.e-5)
bagplot(x2,y)

HTH,
Eric

On Wed, Sep 21, 2022 at 12:54 PM Sigbert Klinke
 wrote:
>
> Hi,
>
> I get an error when I use bagplot from the package aplpack. Any ideas
> what theproblem is with the data set?
>
> library("aplpack")
>
> x <- c(5, 2, 1, 6, 3, 5, 4, 7, 4, 4, 3, 4, 5, 4, 6, 3, 3)
>
> y <- c(2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 1, 14, 17, 18, 19, 20, 22)
>
> bagplot(y,x) # works
>
> bagplot(x,y) # Error in pg[segm.no, 2] : (subscript) boolean index too long
>
> Thanks Sigbert
>
> --
> https://hu.berlin/sk
> https://www.stat.de/faqs
> https://hu.berlin/mmstat
> https://hu.berlin/mmstat-int
> https://hu.berlin/mmstat-ar
>
> __
> 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] aplpack / bagplot

2022-09-21 Thread Eric Berger
Hi Sigbert,
I have never used the aplpack package but out of curiosity I tried it out.
Doing a scatter plot of your (x,y) data shows that there are many repeated
x values, and this seems to be the source of the error.
There are no repeated y values. It seems that the bagplot() function does
not handle repeated x-values.
I tried adding some noise to the x-values and that worked fine.

x2 <- x + rnorm(length(x),0,1.e-5)
bagplot(x2,y)

HTH,
Eric



On Wed, Sep 21, 2022 at 12:54 PM Sigbert Klinke 
wrote:

> Hi,
>
> I get an error when I use bagplot from the package aplpack. Any ideas
> what theproblem is with the data set?
>
> library("aplpack")
>
> x <- c(5, 2, 1, 6, 3, 5, 4, 7, 4, 4, 3, 4, 5, 4, 6, 3, 3)
>
> y <- c(2, 3, 4, 6, 7, 8, 10, 11, 12, 13, 1, 14, 17, 18, 19, 20, 22)
>
> bagplot(y,x) # works
>
> bagplot(x,y) # Error in pg[segm.no, 2] : (subscript) boolean index too
> long
>
> Thanks Sigbert
>
> --
> https://hu.berlin/sk
> https://www.stat.de/faqs
> https://hu.berlin/mmstat
> https://hu.berlin/mmstat-int
> https://hu.berlin/mmstat-ar
>
> __
> 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] Question concerning side effects of treating invalid factor levels

2022-09-19 Thread Eric Berger
You are misinterpreting what is going on.
The rbind command includes c(char, char, int) which produces a
character vector of length 3.
This is what you are rbind-ing which changes the type of the RT column.

If you do rbind(df, data.frame(P="in", ANSWER="V>N",
RT=round(runif(1,7000,16000),0)))
you will see that everything is fine. (New factor values are created.)

HTH,
Eric

On Mon, Sep 19, 2022 at 2:14 PM Tibor Kiss via R-help
 wrote:
>
> Dear List members,
>
> I have tried now for several times to find out about a side effect of 
> treating invalid factor levels, but did not find an answer. Various answers 
> on stackexchange etc. produce the stuff that irritates me without even 
> mentioning it.
> So I am asking the list (apologies if this has been treated in the past).
>
> If you add an invalid factor level to a column in a data frame, this has the 
> side effect of turning a numerical column into a column with character 
> strings. Here is a simple example:
>
> > df <- data.frame(
> P = factor(c("mittels", "mit", "mittels", "ueber", "mit", "mit")),
> ANSWER = factor(c(rep("PP>OBJ", 4), rep("OBJ>PP", 2))),
> RT = round(runif(6, 7000, 16000), 0))
>
> > str(df)
> 'data.frame':   6 obs. of  3 variables:
> $ P : Factor w/ 3 levels "mit","mittels",..: 2 1 2 3 1 1
> $ ANSWER: Factor w/ 2 levels "OBJ>PP","PP>OBJ": 2 2 2 2 1 1
> $ RT: num  11157 13719 14388 14527 14686 ..
>
> > df <- rbind(df, c("in", "V>N", round(runif(1, 7000, 16000), 0)))
>
> > str(df)
> 'data.frame':   7 obs. of  3 variables:
> $ P : Factor w/ 3 levels "mit","mittels",..: 2 1 2 3 1 1 NA
> $ ANSWER: Factor w/ 2 levels "OBJ>PP","PP>OBJ": 2 2 2 2 1 1 NA
> $ RT: chr  "11478" "15819" "8305" "8852" …
>
> You see that RT has changed from _num_ to _chr_ as a side effect of adding 
> the invalid factor level as NA. I would appreciate understanding what the 
> purpose of the type coercion is.
>
> Thanks in advance
>
>
> Tibor
> __
> 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] Converting time format

2022-09-19 Thread Eric Berger
What is the output of

> class(b$time)

?

Also, start sending your emails in plaint text format, if possible.

On Mon, Sep 19, 2022 at 2:12 PM Parkhurst, David  wrote:
>
> I have a dataframe obtained using read.csv from an excel file.  Its first 
> column is times, running from 18:00 to 19:30.  If I want to plot other 
> columns against time, do I need to convert those somehow, and how would I do 
> that?
>
> If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that 
> R is just treating those numbers as text, and putting them in alphabetical 
> order.  True?
>
> [[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] removing non-table lines

2022-09-19 Thread Eric Berger
Hi Nick,

Here's one way to do it. It is based on the heuristic that you keep
each line with the correct number of fields. The correct number of
fields is automatically determined by the number of fields in the last
line of the file.

Here's the contents of a sample csv file - "tmp3.csv" where the first
few rows contain random garbage text.

Collecting numpy
Downloading numpy-1.23.1.tar.gz (10.7 MB)
━━━
10.7/10.7 MB 14.8 MB/s eta 0:00:00
Installing build dependencies: started
Installing build dependencies: finished with status 'error'
2003-07-25,100,100
2003-07-28,102.51597849244192,100
2003-07-29,102.85076595312975,102.51597849244192
2003-07-30,101.62321193060768,102.85076595312975
2003-07-31,102.008724764127,101.62321193060768

Here is my R code to process this file

library(readr)

g <- function(s) { length(unlist(strsplit(s,","))) }

processFile <- function(filename) {
a <- readr::read_lines(filename)
N <- g(a[length(a)]) ## get the "correct" number of columns from
the last row
a2 <- sapply(a, \(x) strsplit(x,","))   ## split each row into its
comma-separated fields
names(a2) <- NULL
iV <- unlist(lapply(a2, function(x) {g(x) == N}))   ## identify
the rows with exactly N fields
a2 <- a2[iV] ## heuristic: keep only the lines with N fields
a3 <- as.data.frame(do.call(rbind,a2))
a3
}

myDf <- processFile("tmp3.csv")
print(myDf)

HTH,
Eric

On Mon, Sep 19, 2022 at 2:26 AM  wrote:
>
> Adding to what Nick said, extra lines like those described often are in some 
> comment format like beginning with "#" or some consistent characters that can 
> be filtered out using comment.char='#' for example in read.csv() or 
> comment="string" in the tidyverse function read_csv().
>
> And, of course you can skip lines if that makes sense albeit it can be tricky 
> with header lines.
>
> -Original Message-
> From: R-help  On Behalf Of Rui Barradas
> Sent: Sunday, September 18, 2022 6:19 PM
> To: Nick Wray ; r-help@r-project.org
> Subject: Re: [R] removing non-table lines
>
> Helo,
>
> Unfortunatelly there are many files with a non tabular data section followed 
> by the data. R's read.table has a skip argument:
>
> skip
> integer: the number of lines of the data file to skip before beginning to 
> read data.
>
> If you do not know how many lines to skip because it's not always the same 
> number, here are some ideas.
>
> Is there a pattern in the initial section? Maybe a end-of-section line or 
> maybe the text lines come in a specified order and a last line in that order 
> can be detected with a regex.
>
> Is there a pattern in the tables' column headers? Once again a regex might be 
> the solution.
>
> Is the number of initial lines variable because there are file versions?
> If there are, did the versions evolve over time, a frequent case?
>
> What you describe is not unfrequent, it's always a nuisance and error prone 
> but it should be solvable once patterns are found. Inspect a small number of 
> files with a text editor and try to find both common points and differences. 
> That's half way to a solution.
>
> Hope this helps,
>
> Rui Barradas
>
> Às 20:39 de 18/09/2022, Nick Wray escreveu:
> > Hello - I am having to download lots of rainfall and temperature data
> > in csv form from the UK Met Office.  The data isn't a problem - it's
> > in nice columns and can be read into R easily - the problem is that in
> > each csv there are 60 or so lines of information first which are not
> > part of the columnar data.  If I read the whole csv into R the column
> > data is now longer in columns but in some disorganised form - if I
> > manually delete all the text lines above and download I get a nice
> > neat data table.  As the text lines can't be identified in R by line
> > numbers etc I can't find a way of deleting them in R and atm have to
> > do it by hand which is slow.  It might be possible to write a
> > complicated and dirty algorithm to rearrange the meteorological data
> > back into columns but I suspect that it might be hard to get right and 
> > consistent across every csv sheet and any errors
> > might be hard to spot.   I can't find anything on the net about this - has
> > anyone else had to deal with this problem and if so do they have any
> > solutions using R?
> > 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, 

Re: [R] opening nimroad tar gz files in R

2022-09-15 Thread Eric Berger
Is there a way to download a file from that site without registering for
the site?
If you have a unix/linux shell, what does the 'file' command output?

$ file foo.gz



On Thu, Sep 15, 2022 at 2:11 PM Nick Wray  wrote:

> Hello
>
> I am trying to download data sets from the 1 km Resolution UK Composite
> Rainfall Data from the Met Office Nimrod System
>
> Eg
>
> CEDA Archive Web Browser
> 
> /badc/ukmo_nimrod/data/composite/uk-1km/2004
>
> I can download an individual file (there are lots for each year) as a
> .gz.tar and then it appears as a TAR file in the directory I am using.
>
> I have then used the instruction untar() (targeted on the correct
> directory) and what this produces is about a dozen these files which are
> labelled as .gz files.  They appear in the same folder as the original TAR
> folder but as R files (ie although they have .gz in the name they have the
> R icon next to them as do any R progs which I have).  I can’t open them
> though as R progs (which I don’t really think they can be) and trying to
> just takes me back to the R studio interface.
>
> I’ve tried  -- read.table (gzfile
> ("metoffice-c-band-rain-radar_uk_200404062250_1km-composite.dat.gz")) but
> that just gives a load of error messages and I haven’t found any other way
> of opening them as .gz files.
>
> I’m rather baffled – can anyone make any suggestions?  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.
>

[[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] Problem with installing packages in R

2022-09-15 Thread Eric Berger
Can you bring up R in a shell? Do you get the same message?
(Also, set your email to send plain text. HTML versions are deleted.)



On Thu, Sep 15, 2022 at 11:27 AM Farah Al Saifi  wrote:
>
> Dear Sir/Madam
>
> After the update of the new version of R 4.2.1, an error message ( Error in 
> nchar(homeDir): invalid multibyte string, element 1)  appears every time i 
> open RStudio. Also the following warning message appears in the console: In 
> normalizePath (path.expand(path),  winslash, mustwork): 
> path[1]="C:/Users/41784/oneDrive - Universität zürich UZH/Dokumente": the 
> system cannot find the given path.
>
> I would be glad, if you can tell me the solution for this problem. Thank you 
> for your consideration.
>
> Best,
>
>
> [[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] von Neumann/Bartels/Serial Correlation

2022-08-14 Thread Eric Berger
Hi Nick,
I have no experience with this package but I just installed it with no
problem and found the help and ran the functions.

> library(DescTools)
> ?VonNeumannTest   <-- works fine
> VonNeumannTest(d.pizza$temperature)  <-- suggested in the help page; works 
> fine
> ?BartelsRankTest  <-- works fine
also the example in the help page for BartelsRankTest works fine.

Did you run
> library(DescTools)

> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] DescTools_0.99.43

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9lattice_0.20-45   mvtnorm_1.1-3 class_7.3-20
 [5] MASS_7.3-58   grid_4.2.1rootSolve_1.8.2.3 e1071_1.7-11
 [9] gld_2.6.2 Exact_3.0 data.table_1.14.2 rstudioapi_0.13
[13] Matrix_1.4-1  boot_1.3-28   tools_4.2.1   proxy_0.4-27
[17] compiler_4.2.1lmom_2.8  expm_0.999-6

HTH,
Eric

On Sun, Aug 14, 2022 at 11:38 AM Nick Wray  wrote:
>
> Hello  Although there is info on the net about the von Neumann test -
> VonNeumannTest:
> Von Neumann's Successive Difference Test in DescTools: Tools for
> Descriptive Statistics (rdrr.io)
> 
>
> I can't make the function run and it doesn't say that the VonNeumannTest()
> function is ain a package.  ?VonNeumannTest yields nothing
>
> Likewise, although there is a CRAN page for BartelsRankTest() it doesn't
> work and again ?BartelsRankTest gives nothing
>
> And again serialCorrelationTest isn't recognise although again there are
> Cran pages
>
> Can anyone point me to where I can find these functions and any info?
>
> 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] Fitted values from AR model

2022-08-12 Thread Eric Berger
The model that you are fitting to the data is an AR(2) model, which means

y(t) = a0 + a1 * y(t-1) + a2 * y(t-2) + eps(t) (1)

The fitting procedure estimates the coefficients a0, a1, a2 (and the
variance of eps).

After the coefficients have been estimated, the fitted values can be
calculated using equation (1) (setting eps(t) = 0)
using
fitted(t) = a0 + a1 * y(t-1) + a2 * y(t-2)

Assuming the series is given for t=1,2,...,20, there is no problem to
apply equation (1) to get the
fitted values for t=3,4,...,20. But there is a problem for t=1 and
t=2. For example, for t=1, equation (1)
says

fitted(1) = a0 + a1 * y(0) + a2 * y(-1)

But there are no values given for y(0) and y(-1). So, either no fitted
values should be given for t=1,2, or some
other method is being used. Apparently, the arima functions in R and
python use different methodology
to generate these two fitted points. (For all the other values, the
fits are extremely close.)

HTH,
Eric

On Thu, Aug 11, 2022 at 9:53 PM bogus christofer
 wrote:
>
> Hi,
>
> I have below AR model and fitted values from the forecast package
>
> library(forecast)
> dta = c(5.0, 11, 16, 23, 36, 58, 29, 20, 10, 8, 3, 0, 0, 2, 11, 27, 47, 63,
> 60, 39)
> fit <- arima(dta, order=c(2,0,0))
> fitted(fit)
>
> This gives fitted values as
>
> Time Series:
> Start = 1
> End = 20
> Frequency = 1
>  [1] 13.461017  9.073427 18.022166 20.689420 26.352282 38.165635 57.502926
> 9.812106 15.335303  8.298995 11.543320  6.606999  5.800820  7.502621
> 9.930962 19.723966 34.045298 49.252447 57.333846 44.615067
>
>
> However when I compare this result with Python, I see significant
> difference particularly in the first few values as below
>
> from statsmodels.tsa.arima.model import ARIMA
> dta = [5.0, 11, 16, 23, 36, 58, 29, 20, 10, 8, 3, 0, 0, 2, 11, 27, 47, 63,
> 60, 39]
> fit = ARIMA(dta, order=(2, 0, 0)).fit()
> fit.predict()
>
> array([21.24816788, 8.66048306, 18.02197059, 20.68931006,
> 26.35225759,38.16574655, 57.503278 , 9.81253693, 15.33539514,
> 8.29894655,11.54316056, 6.60679489, 5.80055038, 7.50232004,
> 9.93067155,19.72374025, 34.04524337, 49.25265365, 57.3343347 , 44.6157026 ])
>
> Any idea why there are such difference between R and Python results will be
> very helpful.
>
> Thanks,
>
> [[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] extracting numeric values returned when using the by function to run the acf function

2022-05-10 Thread Eric Berger
as.numeric(by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)[[1]]$acf)


On Tue, May 10, 2022 at 8:29 PM Sorkin, John 
wrote:

> I am using the by function to run the acf function. Each run of the by
> function returns more information than I want. All I want is the four
> values that acf returns, which using the data below would be
> 1.00  0.15   -0.50  -0.15
> How  can I isolate these four values from the output returned by the by
> function.
>
> Sample code:
>
> RecallSmall <- data.frame(ID=c(1,1,1,1),value=c(6,5,3,4))
> cat("Here are the data\n")
> RecallSmall
> cat("All I want is autocorrerlations, e.g. 1.00  0.15 -0.50 -0.15 \n ")
> # attempting to subset does not work at all
>
> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)$acf
> # this gives me more than I want, not just the numeric results.
>
> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)
>
> # For your reference, this is what the acf function returns
> acf(RecallSmall[1:4,"value"],na.action=na.pass,plot=FALSE)
> # For your reference, this is what the acf function returns when the
> output is subset
> acf(RecallSmall[1:4,"value"],na.action=na.pass,plot=FALSE)Thank you, John
>
> Thank you,
> John
>
>
>
>
>
>
>
>
>
> [[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.


Re: [R] struccchange on zoo time series

2022-05-01 Thread Eric Berger
Hi Achim,
My point was that tsbox (apparently) provides tools to convert zoo -->
ts which should help the OP.

On Sun, May 1, 2022 at 5:56 PM Achim Zeileis  wrote:
>
> On Sun, 1 May 2022, Eric Berger wrote:
>
> > Hi Naresh,
> > The tsbox package on CRAN -
> > https://cran.r-project.org/web/packages/tsbox/index.html - has the
> > following description:
> >
> > tsbox: Class-Agnostic Time Series
> >
> > Time series toolkit with identical behavior for all time series
> > classes: 'ts','xts', 'data.frame', 'data.table', 'tibble', 'zoo',
> > 'timeSeries', 'tsibble', 'tis' or 'irts'. Also converts reliably
> > between these classes.
> >
> > Hopefully this will provide you the necessary tools to solve your problem.
>
> Not really because the code inside strucchange::efp does not use tsbox but
> just ts directly.
>
> Best,
> Achim
>
> > Good luck,
> > Eric
> >
> >
> >
> > On Sun, May 1, 2022 at 3:37 PM Naresh Gurbuxani
> >  wrote:
> >>
> >> I am trying to replicate empirical fluctuation process fit (efp) described 
> >> in the book "Applied Econometrics with R".  This fit works when data input 
> >> is an object of class ts, but not when data input is object of class zoo.  
> >> I prefer to use zoo because it provides better housekeeping with dates.  
> >> Is it possible to achieve the fit with zoo?
> >>
> >> library(AER)
> >> library(strucchange)
> >>
> >> data(UKDriverDeaths)
> >> dd <- log(UKDriverDeaths)
> >> dd.z <- zoo(dd, order.by = as.yearmon(time(dd)))
> >> dd.z <- merge(dd = dd.z, dd.lag1 = lag(dd.z, k = -1),
> >>   dd.lag12 = lag(dd.z, k = -12))
> >>
> >> # Does not work
> >> dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = na.trim(dd.z),
> >>type = "OLS-CUSUM")
> >> # Error message
> >> # Error in eval(attr(mt, "variables")[[2]], data, env) :
> >> # numeric 'envir' arg not of length one
> >>
> >> # Works
> >> dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = ts(na.trim(dd.z)),
> >>type = "OLS-CUSUM")
> >>
> >> # But time stamps are lost
> >> plot(dd.ocus)
> >> # Time indexed from 0 to 180
> >>
> >> Thanks,
> >> Naresh
> >> __
> >> 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.
> >

__
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] Confusing fori or ifelse result in matrix manipulation

2022-04-25 Thread Eric Berger
M[,x==1] is not the same as M[,x] :-)
However, M[,!!x] is the same as M[,x==1] and saves one character!
The point of this is "I can name that tune in ... " (as if that was not
obvious)



On Mon, Apr 25, 2022 at 5:30 PM Bert Gunter  wrote:

> x == 1 is the same as M[, x] so your expression is the same as
> M[, c(FALSE, TRUE, FALSE)] <- 0
> which is the same as M[, 2]  <- 0
>
> So what is the point of all this, exactly?
>
> Bert
>
> On Mon, Apr 25, 2022 at 7:18 AM Ivan Calandra 
> wrote:
> >
> > Hi Uwe,
> >
> > If I understood the problem completely and building up on Tim's answer,
> > this is even easier:
> > M <- A <- matrix(1:9, ncol = 3)
> > x <- c(0, 1, 0)
> > M[, x == 1] <- 0
> > M
> >
> > The original issue was with the way ifelse works. The explanation is in
> > the help page: "ifelse returns a value with the same shape as test||".
> > So, because x[i] == 0 returns a single value (TRUE or FALSE), ifelse
> > will also return a single value (either A[, i][1] or 0) and not a vector
> > of length 3 as you wanted. This single value is recycled to fill M[, i],
> > hence the result.
> >
> > HTH,
> > Ivan
> >
> > --
> > Dr. Ivan Calandra
> > Imaging lab
> > RGZM - MONREPOS Archaeological Research Centre
> > Schloss Monrepos
> > 56567 Neuwied, Germany
> > +49 (0) 2631 9772-243
> > https://www.researchgate.net/profile/Ivan_Calandra
> >
> > On 25/04/2022 16:01, Ebert,Timothy Aaron wrote:
> > > A <- matrix(1:9,ncol=3)
> > > x <- c(0,1,0)
> > > M <- matrix(ncol=3,nrow=3)
> > > M<-A
> > > for(i in 1:3) {
> > >if(x[i]){
> > >  M[,i] <-0
> > >  }
> > >}
> > > }
> > > M
> > >
> > > The outcome you want is to set all of the middle column values to
> zero. So I used x as a logical in an if test and when true everything in
> that column is set to zero.
> > >
> > > Your approach also works but you must go through each element
> explicitly.
> > > A <- matrix(1:9,ncol=3)
> > > x <- c(0,1,0)
> > > M <- matrix(ncol=3,nrow=3)
> > > for(j in 1:3){
> > >for(i in 1:3){
> > >  ifelse(x[i]==1, M[j,i]<-0, M[j,i]<-A[j,i])
> > >}
> > > }
> > > M
> > >
> > >
> > >
> > > Tim
> > >
> > > -Original Message-
> > > From: R-help  On Behalf Of Uwe Freier
> > > Sent: Sunday, April 24, 2022 11:06 AM
> > > To: r-help@r-project.org
> > > Subject: [R] Confusing fori or ifelse result in matrix manipulation
> > >
> > > [External Email]
> > >
> > > Hello,
> > >
> > > sorry for the newbie question but I can't find out where I'm wrong.
> > >
> > > A <- matrix(1:9,ncol=3)
> > > x <- c(0,1,0)
> > > M <- matrix(ncol=3,nrow=3)
> > > for(i in 1:3) {
> > >M[,i] <- ifelse(x[i] == 0, A[,i], 0)
> > > }
> > >
> > > expected:
> > >
> > >> M
> > >[,1] [,2] [,3]
> > > [1,]107
> > > [2,]208
> > > [3,]309
> > >
> > >
> > > but the result is:
> > >
> > >> M
> > >[,1] [,2] [,3]
> > > [1,]107
> > > [2,]107
> > > [3,]107
> > >
> > >
> > > If I do it "manually":
> > >
> > >> M[,1] <- A[,1]
> > >> M[,2] <- 0
> > >> M[,3] <- A[,3]
> > > M is as expected, where is my misconception?
> > >
> > > Thanks for any hint and best regards,
> > >
> > > Uwe
> > >
> > > __
> > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4=
> > > PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q=
> > > 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.
> >
> > __
> > 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.
>

[[alternative HTML version deleted]]


Re: [R] Looping through all matrix columns each 1 row at a time

2022-04-21 Thread Eric Berger
Hi Paul,
I am not sure I understand your question, but perhaps the following is helpful.
In particular, the apply() function used with MAR=1, applies a
function to a matrix row-wise.

set.seed(123)
m <- matrix(sample(1:6,5*12,replace=TRUE),ncol=12) ## dummy data
m
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]366311154 1 5 1
[2,]631356315 2 2 6
[3,]352133515 5 1 5
[4,]243424423 5 1 1
[5,]265126236 4 3 2

apply(m,MAR=1,function(v) length(setdiff(1:6,v))==0)

[1] FALSE FALSE FALSE FALSE  TRUE  ## only the last row has all numbers from 1-6

HTH,
Eric


On Thu, Apr 21, 2022 at 7:55 AM Paul Bernal  wrote:
>
> Dear R friends,
>
> One question, so, thanks to the Bert's kind feedback, I was able to create
> my matrix using the following code:
> dice_rolls = 120
> num_dice   = 1
> dice_sides = 6
>
> #performing simulation
> dice_simul = data.frame(dice(rolls = dice_rolls, ndice = num_dice, sides =
> dice_sides, plot.it = TRUE))
>
> dice_simul
>
>
> prob_matrix <- matrix(dice_simul[,1], ncol = 12, byrow = TRUE)
>
> colnames(prob_matrix) <-
> c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
>
> Now, I need to perform an analysis for each column, one row at a time. For
> example, I need to know if numbers 1 through 6 all appear in the twelve
> column for row 1, then for row 2, etc.
>
> Any guidance will be greatly appreciated.
>
> Best,
> Paul
>
> [[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] Max-Diff Package

2022-04-13 Thread Eric Berger
I have no experience with this but a Google search came up with

CRAN support.BWS

See https://cran.r-project.org/web/packages/support.BWS/index.html

On Wed, Apr 13, 2022 at 4:37 PM Dr Cazhaow Qazaz 
wrote:

> Hi All,
>
> Any recommendations for a R package to perform MaxDiff analysis?
>
> Thank you,
>
> Cazhaow Qazaz
>
>
> [[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.


Re: [R] Multiplying each row of data.frame by a row in another data.frame

2022-04-09 Thread Eric Berger
This looks like homework, which is not permitted on this list. But
giving you the benefit of the doubt, here are two ways to do it

> as.matrix(val_df) * matrix(rep(weights$value,5),ncol=3,byrow = TRUE)
 ABC DEF  ONS
[1,]  10   2  500
[2,]  20   4  500
[3,]  10  20 1000
[4,]  10   4  500
[5,]  10  10  500
> t(apply(as.matrix(val_df),MAR=1,function(v) v * weights$value))

HTH,
Eric


On Sat, Apr 9, 2022 at 7:57 AM maithili_shiva--- via R-help
 wrote:
>
> Dear R forum
> weights <- data.frame(id = c("ABC", "DEF",  "ONS"), value = c(1, 2, 5))
> val_df <- data.frame(ABC = c(10, 20, 10, 10, 10), DEF = c(1, 2, 10, 2, 5), 
> ONS = c(100, 100, 200, 100, 100))
>
> > weights
>id   value1 ABC   12 DEF   23 ONS   5
>
> > val_df
>   ABC DEF ONS1  10   1 1002  20   2 1003  10  102004  10   2
> 1005  10   5100
> I wish to multilpy each row of data.frame val_df by value column of 
> data.frame weights.
>   ABCDEFONS1  10*1   1*2   100*52  20*1   2*2   100*53  10*1   10*2  
> 200*54  10*1   2*2   100*55  10*1   5*2   100*5
> ie I wish to have output as
> OUTPUT
>   ABCDEFONS1  102  5002  204 5003  1020   
>10004  1045005  1010 500
>
> O have tried sweep function, even matrix multiplcation etc, but nothing seems 
> to be working.
> Please guide
> Regards
> Maithili
>
> [[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] [External] "apply" a function that takes two or more vectors as arguments, such as cor(x, y), over a "category" or "grouping variable" or "index"?

2022-04-08 Thread Eric Berger
library(dplyr)
my_df |> group_by(my_category) |> summarise(my_z = cor(my_x, my_y))


On Sat, Apr 9, 2022 at 4:37 AM Richard M. Heiberger  wrote:

> look at
> ?mapply
> Apply a Function to Multiple List or Vector Arguments
>
> to see if that meets your needs
>
> > On Apr 08, 2022, at 21:26, Kelly Thompson  wrote:
> >
> > #Q. How can I "apply" a function that takes two or more vectors as
> > arguments, such as cor(x, y), over a "category" or "grouping variable"
> > or "index"?
> > #I'm using cor() as an example, I'd like to find a way to do this for
> > any function that takes 2 or more vectors as arguments.
> >
> >
> > #create example data
> >
> > my_category <- rep ( c("a","b","c"),  4)
> >
> > set.seed(12345)
> > my_x <- rnorm(12)
> >
> > set.seed(54321)
> > my_y <- rnorm(12)
> >
> > my_df <- data.frame(my_category, my_x, my_y)
> >
> > #review data
> > my_df
> >
> > #If i wanted to get the correlation of x and y grouped by category, I
> > could use this code and loop:
> >
> > my_category_unique <- unique(my_category)
> >
> > my_results <- vector("list", length(my_category_unique) )
> > names(my_results) <- my_category_unique
> >
> > #start i loop
> >  for (i in 1:length(my_category_unique) ) {
> >my_criteria_i <- my_category == my_category_unique[i]
> >my_x_i <- my_x[which(my_criteria_i)]
> >my_y_i <- my_y[which(my_criteria_i)]
> >my_correl_i <- cor(x = my_x_i, y = my_y_i)
> >my_results[i] <- list(my_correl_i)
> > } # end i loop
> >
> > #review results
> > my_results
> >
> > #Q. Is there a better or more "elegant" way to do this, using by(),
> > aggregate(), apply(), or some other function?
> >
> > #This does not work and results in this error message: "Error in
> > FUN(dd[x, ], ...) : incompatible dimensions"
> > by (data = my_x, INDICES = my_category, FUN = cor, y = my_y)
> >
> > #This does not work and results in this error message: "Error in
> > cor(my_df$x, my_df$y) : ... supply both 'x' and 'y' or a matrix-like
> > 'x' "
> > by (data = my_df, INDICES = my_category, FUN = function(x, y) { cor
> > (my_df$x, my_df$y) } )
> >
> >
> > #if I wanted the mean of x by category, I could use by() or aggregate():
> > by (data = my_x, INDICES = my_category, FUN = mean)
> >
> > aggregate(x = my_x, by = list(my_category), FUN = mean)
> >
> > #Thanks!
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=04%7C01%7Crmh%40temple.edu%7C4c8a50fd1bf14b2cf7b408da19c7fe20%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637850644148770767%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000sdata=23Y%2Fqw7G1gb4ACIz5V41DjBIR8c2IFkkZgud9dGaftE%3Dreserved=0
> > PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=04%7C01%7Crmh%40temple.edu%7C4c8a50fd1bf14b2cf7b408da19c7fe20%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637850644148770767%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000sdata=3vIZYrMBnAZKZhZCwHcLpILHEE72NuLc03LXAxr%2BXQ4%3Dreserved=0
> > 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.
>

[[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] Where is list.names?

2022-03-30 Thread Eric Berger
At the R console prompt do:
> table

It prints out the source of the table() function. The first thing there is
the definition of list.names()

HTH,
Eric




On Wed, Mar 30, 2022 at 12:27 PM Göran Broström 
wrote:

> I am trying to redefine the default behavior of base::table by editing
> .Rprofile in my home directory:
>
> table <- function(...,
>useNA = "ifany",
>exclude = if (useNA == "no") c(NA, NaN),
>dnn = list.names(...),
>deparse.level = 1){
>  base::table(...,
>  useNA = useNA,
>  exclude = exclude,
>  dnn = dnn,
>  deparse.level = deparse.level)
> }
>
> Trying it, I get
>
>  > table(c(1,1,2,NA))
> Error in list.names(...) : could not find function "list.names"
>
> However, by removing the argument 'dnn', it apparently works. I am
> probably doing this in the wrong way, but how should it be done?
> I can guess what 'list.names' is (from the documentation), but where and
> how is it defined?
>
> Thanks, Göran
> (On R-4.1.3)
>
> __
> 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] stdev error

2022-03-11 Thread Eric Berger
try changing
Bse_ftv = mean(Bse_ftv)
to
Bse_ftv_mean = mean(Bse_ftv)

On Fri, Mar 11, 2022 at 4:15 PM Jeff Reichman 
wrote:

> r-help forum
>
>
>
> When I run the following code
>
>
>
> my_tbl %>%
>
>   mutate(Bse_bwt = round(Bse_bwt * 2) / 2) %>%
>
>   group_by(Cat, Bse_bwt) %>%
>
>   summarize(count = n(), Bse_ftv = mean(Bse_ftv), stdev = sd(Bse_ftv))
>
>
>
> I get the following error:
>
>
>
> Error: `stdev` refers to a variable created earlier in this summarise().
>
> Do you need an extra mutate() step?
>
>
>
> I suspect it is because the standard deviation of a length-one vector is NA
> and R is errorerrors out on the standard deviation  of 1. So then I tried
>
>
>
> summarize(count = n(), Bse_ftv = mean(Bse_ftv), stdev = if(n()>1)
> sd(Bse_ftv) else 0) and this didn't seem to work either. So there has to be
> a way to add some sort of error checker to my standard deviation function
> to
> check if n > 1 and then take the standard deviation in dplyr.
>
>
>
> Jeff
>
>
>
>
> [[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.


Re: [R] Possible causes of unexpected behavior

2022-03-04 Thread Eric Berger
Please confirm that when you do the manual load and check that f(v*)
matches the result from qsub() it succeeds for cases #1,#2 but only fails
for #3.


On Fri, Mar 4, 2022 at 10:06 AM Arthur Fendrich  wrote:

> Dear all,
>
> I am currently having a weird problem with a large-scale optimization
> routine. It would be nice to know if any of you have already gone through
> something similar, and how you solved it.
>
> I apologize in advance for not providing an example, but I think the
> non-reproducibility of the error is maybe a key point of this problem.
>
> Simplest possible description of the problem: I have two functions: g(X)
> and f(v).
> g(X) does:
>  i) inputs a large matrix X;
>  ii) derives four other matrices from X (I'll call them A, B, C and D) then
> saves to disk for debugging purposes;
>
> Then, f(v) does:
>  iii) loads A, B, C, D from disk
>  iv) calculates the log-likelihood, which vary according to a vector of
> parameters, v.
>
> My goal application is quite big (X is a 4x4 matrix), so I created
> the following versions to test and run the codes/math/parallelization:
> #1) A simulated example with X being 100x100
> #2) A degraded version of the goal application, with X being 4000x4000
> #3) The goal application, with X being 4x4
>
> When I use qsub to submit the job, using the exact same code and processing
> cluster, #1 and #2 run flawlessly, so no problem. These results tell me
> that the codes/math/parallelization are fine.
>
> For application #3, it converges to a vector v*. However, when I manually
> load A, B, C and D from disk and calculate f(v*), then the value I get is
> completely different.
> For example:
> - qsub job says v* = c(0, 1, 2, 3) is a minimum with f(v*) = 1.
> - when I manually load A, B, C, D from disk and calculate f(v*) on the
> exact same machine with the same libraries and environment variables, I get
> f(v*) = 1000.
>
> This is a very confusing behavior. In theory the size of X should not
> affect my problem, but it seems that things get unstable as the dimension
> grows. The main issue for debugging is that g(X) for simulation #3 takes
> two hours to run, and I am completely lost on how I could find the causes
> of the problem. Would you have any general advices?
>
> Thank you very much in advance for literally any suggestions you might
> have!
>
> Best regards,
> Arthur
>
> [[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.


Re: [R] A question of data frame filter based on another one

2022-02-18 Thread Eric Berger
x[apply(y,MAR=1,sum) > 0,]


On Fri, Feb 18, 2022 at 10:24 PM Li, Aiguo (NIH/NCI) [E] via R-help <
r-help@r-project.org> wrote:

> I have tow dataframes as below:
> > x
>   id  g
> 1  1 21
> 2  3 52
> 3  2 43
> 4  4 94
> 5  5 35
>
> > y
>   id g
> 1  1 1
> 2  0 0
> 3  0 1
> 4  1 0
> 5  1 0
>
> Results dataframe I want is:
> 1 21
> 2 43
> 4 94
> 5 35
>
> Basically I want to extract all the values in x which corresponding those
> values =1 in y.
>
> I tried:
> x[which(y==1),].  It gets:
> id  g
> 1 1 21
> 4 4 94
> 5 5 35
> NA   NA NA
> NA.1 NA NA
>
> But missing the row: 2 43.
>
> Any help will be appreciated.
>
> Thanks,
> Aiguo
>
> [[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.


Re: [R] Time for a companion mailing list for R packages?

2022-01-13 Thread Eric Berger
Re: constructive criticism to make this list more useful to more people:

Suggestion 1: accommodate questions related to non-base-R packages
   This has been addressed by many already. The current
de facto situation is that such questions are asked and often answered.
Perhaps the posting guide should be altered so that such questions fall
within the guidelines.

Suggestion 2: expand beyond plain-text mode
I assume there is a reason for this restriction but
it seems to create a lot of delay and often havoc. Also, many questions on
this list relate to graphics which is an important part of R (even base R)
and such questions may often be more easily communicated with images.

Eric




On Thu, Jan 13, 2022 at 6:08 PM John Fox  wrote:

> Dear Avi et al.,
>
> Rather than proliferating R mailing lists, why not just allow questions
> on non-standard packages on the r-help list?
>
> (1) If people don't want to answer these questions, they don't have to.
>
> (2) Users won't necessarily find the new email list and so may post to
> r-help anyway, only to be told that they should have posted to another
> list.
>
> (3) Many of the questions currently posted to the list concern
> non-standard packages and most of them are answered.
>
> (4) If people prefer other sources of help (as listed on the R website
> "getting help" page) then they are free to use them.
>
> (5) As I read the posting guide, questions about non-standard packages
> aren't actually disallowed; the posting guide suggests, however, that
> the package maintainer be contacted first. But answers can be helpful to
> other users, and so it may be preferable for at least some of these
> questions to be asked on the list.
>
> (6) Finally, the instruction concerning non-standard packages is buried
> near the end of the posting guide, and users, especially new users, may
> not understand what the term "standard packages" means even if they find
> their way to the posting guide.
>
> Best,
>   John
>
> --
> John Fox, Professor Emeritus
> McMaster University
> Hamilton, Ontario, Canada
> web: https://socialsciences.mcmaster.ca/jfox/
>
> On 2022-01-12 10:27 p.m., Avi Gross via R-help wrote:
> > Respectfully, this forum gets lots of questions that include non-base R
> components and especially packages in the tidyverse. Like it or not, the
> extended R language is far more useful and interesting for many people and
> especially those who do not wish to constantly reinvent the wheel.
> > And repeatedly, we get people reminding (and sometimes chiding) others
> for daring to post questions or supply answers on what they see as a pure R
> list. They have a point.
> > Yes, there are other places (many not being mailing lists like this one)
> where we can direct the questions but why can't there be an official
> mailing list alongside this one specifically focused on helping or just
> discussing R issues related partially to the use of packages. I don't mean
> for people making a package to share, just users who may be searching for
> an appropriate package or using a common package, especially the ones in
> the tidyverse that are NOT GOING AWAY just because some purists ...
> > I prefer a diverse set of ways to do things and base R is NOT enough for
> me, nor frankly is R with all packages included as I find other languages
> suit my needs at times for doing various things. If this group is for
> purists, fine. Can we have another for the rest of us? Live and let live.
> >
> >
> > -Original Message-
> > From: Duncan Murdoch 
> > To: Kai Yang ; R-help Mailing List <
> r-help@r-project.org>
> > Sent: Wed, Jan 12, 2022 3:22 pm
> > Subject: Re: [R] how to find the table in R studio
> >
> > On 12/01/2022 3:07 p.m., Kai Yang via R-help wrote:
> >> Hi all,
> >> I created a function in R. It will be generate a table "temp". I can
> view it in R studio, but I cannot find it on the top right window in R
> studio. Can someone tell me how to find it in there? Same thing for f_table.
> >> Thank you,
> >> Kai
> >> library(tidyverse)
> >>
> >> f1 <- function(indata , subgrp1){
> >>  subgrp1 <- enquo(subgrp1)
> >>  indata0 <- indata
> >>  temp<- indata0 %>% select(!!subgrp1) %>% arrange(!!subgrp1) %>%
> >>group_by(!!subgrp1) %>%
> >>mutate(numbering =row_number(), max=max(numbering))
> >>  view(temp)
> >>  f_table <- table(temp$Species)
> >>  view(f_table)
> >> }
> >>
> >> f1(iris, Species)
> >>
> >
> > Someone is sure to point out that this isn't an RStudio support list,
> > but your issue is with R, not with RStudio.  You created the table in
> > f1, but you never returned it.  The variable f_table is local to the
> > function.  You'd need the following code to do what you want:
> >
> > f1 <- function(indata , subgrp1){
> >subgrp1 <- enquo(subgrp1)
> >indata0 <- indata
> >temp<- indata0 %>% select(!!subgrp1) %>% arrange(!!subgrp1) %>%
> >  group_by(!!subgrp1) %>%
> >  

Re: [R] for loop question in R

2021-12-22 Thread Eric Berger
Try replacing
"c:/temp/f[i].jpg"
with
paste0("c:/temp/",f[i],".jpg")


On Wed, Dec 22, 2021 at 7:08 PM Kai Yang via R-help 
wrote:

> Hello R team,I want to use for loop to generate multiple plots with 3
> parameter, (y is for y axis, c is for color and f is for file name in
> output). I created a data frame to save the information and use the
> information in for loop. I use y[i], c[i] and f[i] in the loop, but it
> seems doesn't work. Can anyone correct my code to make it work?
> Thanks,Kai
>
> library(ggplot2)library(tidyverse)
> y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class")
> mac <- data.frame(y,c,f)
> for (i in nrow(mac)){  mpg %>%filter(hwy <35) %>% ggplot(aes(x =
> displ, y = y[i], color = c[i])) + geom_point()
> ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")}
>
> [[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.


Re: [R] nlme::lme sigma parameter standard deviation or variance?

2021-12-18 Thread Eric Berger
You can run a test. Multiply all your data by a scalar, say 2.
If this changes the result lme_mod$sigma by a factor of 2, then it is
a std deviation.
If it changes the result by a factor of 4, then it is a variance.

HTH,
Eric

On Sat, Dec 18, 2021 at 11:26 AM Courtney Van Den elzen
 wrote:
>
> Hi R-help,
>
> I am a researcher fitting linear mixed models using the package nlme. I am
> wondering whether the sigma value that is outputted as part of the model
> object is standard deviation or variance? for example, I might fit a model
>
> lme_mod <- nlme::lme(response ~ predictor1 + predictor2, random =
> (~1|grouping1))
>
> I am wondering whether lme_mod$sigma is actually a standard deviation or if
> it's a variance.
>
> Thanks so much,
> Courtney
>
> [[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] matching type question, please

2021-12-16 Thread Eric Berger
> a <- cbind(c(xr1,xs1),c(xr2,xs2))
> a[duplicated(a)]
[1] 4 4


On Thu, Dec 16, 2021 at 10:18 PM Erin Hodgess  wrote:
>
> Hello!
>
> I have the following:
>
>  cbind(xr1,xr2)
>
>   xr1 xr2
>
>  [1,]   8   0
>
>  [2,]   7   1
>
>  [3,]   6   2
>
>  [4,]   5   3
>
>  [5,]   4   4
>
>  [6,]   3   5
>
>  [7,]   2   6
>
>  [8,]   1   7
>
>  [9,]   0   8
>
> > cbind(xs1,xs2)
>
>  xs1 xs2
>
> [1,]   9   4
>
> [2,]   8   4
>
> [3,]   7   4
>
> [4,]   6   4
>
> [5,]   5   4
>
> [6,]   4   4
>
> [7,]   3   4
>
> >
>
> These are ordered pairs.  I would like to get something that shows that the
> pair (4,4) appears in both.  I have tried cbind with match and %in% and
> intersect, but not getting the exact results.
>
> Any suggestions would be appreciated.  I have a feeling that it's something
> really easy that I'm just not seeing.
>
> Thanks,
> Erin
>
>
> Erin Hodgess, PhD
> mailto: erinm.hodg...@gmail.com
>
> [[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] Puzzled about loading the Rattle interface package...

2021-12-07 Thread Eric Berger
Hi Brian,
I have never tried Rattle but I did a quick search and found this link
which might give some clues.

https://book.huihoo.com/data-mining-desktop-survival-guide/Installing_GTK.html

Good luck,
Eric


On Tue, Dec 7, 2021 at 1:29 PM Brian Lunergan  wrote:
>
> Evening folks:
>
> Hope I'm in the right place to ask this.
>
> Running R 4.1.2 on Linux Mint 19.3. I do have R Commander and R Studio
> in place and working, so I'm not without choices for a GUI to use R.
> However, I wanted to try out the Rattle interface.
>
> The package installs OK, but it apparently needs the RGtk2 package to
> run its graphic state. When I try to install that package I get the
> following:
>
> > install.packages("RGtk2")
> Installing package into ‘/home/brian/R/x86_64-pc-linux-gnu-library/4.1’
> (as ‘lib’ is unspecified)
> trying URL 'https://cloud.r-project.org/src/contrib/RGtk2_2.20.36.2.tar.gz'
> Content type 'application/x-gzip' length 2792234 bytes (2.7 MB)
> ==
> downloaded 2.7 MB
>
> * installing *source* package ‘RGtk2’ ...
> ** package ‘RGtk2’ successfully unpacked and MD5 sums checked
> ** using staged installation
> checking for pkg-config... /usr/bin/pkg-config
> checking pkg-config is at least version 0.9.0... yes
> checking for INTROSPECTION... no
> checking for GTK... no
> configure: error: GTK version 2.8.0 required
> ERROR: configuration failed for package ‘RGtk2’
> * removing ‘/home/brian/R/x86_64-pc-linux-gnu-library/4.1/RGtk2’
>
> The downloaded source packages are in
> ‘/tmp/Rtmphfaxc3/downloaded_packages’
> Warning message:
> In install.packages("RGtk2") :
>   installation of package ‘RGtk2’ had non-zero exit status
> >
>
> I'm at a bit of a loss as to how to solve this. Any help would be
> appreciated. Otherwise, I guess I'd be SOL for trying it out and fall
> back to my existing two options.
>
> Thanks...
> --
> Brian Lunergan
> Russell, ON
> Canada
>
>
>
> __
> 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] SOMAscan data analysis

2021-12-03 Thread Eric Berger
Hi Kai,
Check out https://www.bioconductor.org
or the help there at
https://www.bioconductor.org/help/

You can also post your question there.

Best,
Eric


On Fri, Dec 3, 2021 at 2:22 AM Kai Yang via R-help 
wrote:

> Hello R team,we have a huge SOMAscan data set. This is an aptamer-based
> protecomics assay capable of measuring 1305 human protein analytes. does
> anyone know which package can load the data and do analysis? I apricate any
> suggestion, your experience, web page, paper
> Thank you,Kai
> [[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.


Re: [R] Problem with lm Giving Wrong Results

2021-12-02 Thread Eric Berger
Hi Thomas,
I could not reproduce your problem. Both examples worked fine for me.

Here is my setup:

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
LC_TIME=en_US.UTF-8
 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] R.matlab_3.6.2

loaded via a namespace (and not attached):
[1] compiler_4.1.2tools_4.1.2   R.methodsS3_1.8.1
R.utils_2.11.0R.oo_1.24.0


On Thu, Dec 2, 2021 at 4:18 PM Labone, Thomas  wrote:
>
> In the code below the first and second plots should look pretty much the 
> same, the only difference being that the first has n=1000 points and the 
> second n=1 points. On two of my Linux machines (info below) the second 
> plot is a horizontal line (incorrect answer from lm), but on my Windows 10 
> machine and a third Linux machine it works as expected. The interesting thing 
> is that the code works as expected for n <= 4095 but fails for n>=4096 (which 
> equals 2^12). Can anyone else reproduce this problem? Any ideas on how to fix 
> it?
>
> set.seed(132)
>
> #~~~
> # This works
> n <- 1000# OK <= 4095
> Z <- qnorm(ppoints(n))
>
> k <- sort(rlnorm(n,log(2131),log(1.61)) / rlnorm(n,log(355),log(1.61)))
>
> quantile(k,probs=c(0.025,0.5,0.975))
> summary(k)
>
> fit <- lm(log(k) ~ Z)
> summary(fit)
>
> gm <- exp(coef(fit)[1])
> gsd <- exp(coef(fit)[2])
> gm
> gsd
>
> plot(Z,k,log="y",xlim=c(-4,4),ylim=c(0.1,100))
> lines(Z,gm*gsd^Z,col="red")
>
> #~~~
> #this does not
> n <- 1# fails >= 4096 = 2^12
> Z <- qnorm(ppoints(n))
>
> k <- sort(rlnorm(n,log(2131),log(1.61)) / rlnorm(n,log(355),log(1.61)))
>
> quantile(k,probs=c(0.025,0.5,0.975))
> summary(k)
>
> fit <- lm(log(k) ~ Z)
> summary(fit)
>
> gm <- exp(coef(fit)[1])
> gsd <- exp(coef(fit)[2])
> gm
> gsd
>
> plot(Z,k,log="y",xlim=c(-4,4),ylim=c(0.1,100))
> lines(Z,gm*gsd^Z,col="red")
>
>
> #~~~
> > sessionInfo() #for two Linux machines having problem
> R version 4.1.2 (2021-11-01)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Linux Mint 20.2
>
> Matrix products: default
> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C   
> LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
>  [6] LC_MESSAGES=en_US.UTF-8LC_PAPER=en_US.UTF-8   LC_NAME=C  
> LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] compiler_4.1.2  Matrix_1.3-4tools_4.1.2 expm_0.999-6
> grid_4.1.2  lattice_0.20-45
>
> #~~
> > sessionInfo() # for a third Linux machine not having the problem
> R version 4.1.1 (2021-08-10)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Linux Mint 19.3
>
> Matrix products: default
> BLAS/LAPACK: 
> /opt/intel/compilers_and_libraries_2020.0.166/linux/mkl/lib/intel64_lin/libmkl_rt.so
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C   LC_TIME=en_US.UTF-8
>  [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
> LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=en_US.UTF-8   LC_NAME=C  LC_ADDRESS=C
> [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] compiler_4.1.1 tools_4.1.1
>
>
>
> Thomas R. LaBone
> PhD student
> Department of Epidemiology and Biostatistics
> Arnold School of Public Health
> University of South Carolina
> Columbia, South Carolina USA
>
>
> [[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

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Eric Berger
Hi Philip,
This is a recurring question and there are many ways to do this.
My preference is to use the lubridate package.

library(lubridate)
a <- "15/01/2010"
b <- dmy(a)
b
# "2010-01-15"
class(b)
# [1] "Date"

HTH,
Eric


On Sat, Nov 20, 2021 at 7:09 PM Philip Monk  wrote:

> Hello,
>
> Simple but infuriating problem.
>
> Reading in CSV of data using :
>
> ```
> # CSV file has column headers with date of scene capture in format
> dd/mm/
> # check.names = FALSE averts R incorrectly processing dates due to '/'
> data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
>
> # Converts data table from wide (many columns) to long (many rows) and
> creates the new object 'data_long'
> # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly
> data covering 2 years (the header row being the date, and rows 2-21 being a
> value for each buffer).
> # Column headers for columns 2:25 are mutated into a column called 'Date',
> values for each buffer and each date into the column 'LST'
> data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
>
> # Instructs R to treat the 'Date' column data as a date
> data_long$Date <- as.Date(data_long$Date)
> ```
>
> Using str(data), I can see that R has correctly read the dates in the
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
> Once changing the type to 'Date', however, the date is reconfigured.  For
> instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
> I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
>
> How do I make R change Date from 'chr' to 'date' without it going wrong?
>
> Suggestions/hints/solutions would be most welcome.  :)
>
> Thanks for your time,
>
> Philip
>
> Part-time PhD Student (Environmental Science)
> Lancaster University, UK.
>
> ~
>
> I asked a question a few weeks ago and put together the answer I needed
> from the responses but didn't know how to say thanks on this list.  So,
> thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>
> [[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.


  1   2   3   4   5   6   >