Re: [R] Output multiple sheets to Excel files with openxlsx::write.xlsx

2020-05-27 Thread Enrico Schumann
On Wed, 27 May 2020, John writes:

> Hi,
>
>This is my code a few years ago. I was able to output multiple sheet to
> an excel file. Nevertheless, the "append" argument appears to be obsolete.
> Now I see only one sheet, the latest added sheet, in the output. Is there
> any other way to do it with openxlsx::write.xlsx or other
> functions/packages?
>
>
> openxlsx::write.xlsx(df1, file=fl_out, sheetName="a",
>  col.names=TRUE, row.names=FALSE, append=TRUE, showNA=FALSE)
>
> openxlsx::write.xlsx(df2, file=fl_out, sheetName="b",
>  col.names=TRUE, row.names=FALSE,
> append=TRUE, showNA=FALSE)
>
> Thanks!!
>

I think you need to create a workbook first, then add
the sheets, and finally write the workbook to a file.
Something like this:

df <- data.frame(a = 1:3,
 b = 4:6)

library("openxlsx")
wb <- createWorkbook()

sheet <- "sheet1"
addWorksheet(wb, sheet)
writeData(wb, sheet = sheet, x = df)

sheet <- "sheet2"
addWorksheet(wb, sheet)
writeData(wb, sheet = sheet, x = df + 1)

saveWorkbook(wb, file = "~/Desktop/two_sheets.xlsx")



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
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] Output multiple sheets to Excel files with openxlsx::write.xlsx

2020-05-27 Thread Ivan Calandra
Hi,

Rather than creating a workbook as suggested by Enrico, you can simply
supply a list to write.xlsx(); each element will be saved in a separate
sheet:
write.xlsx(list(a = df1, b = df2), file = fl_out)

That is not really appending, but that might work for you.

HTH,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 27/05/2020 9:15, Enrico Schumann wrote:
> On Wed, 27 May 2020, John writes:
>
>> Hi,
>>
>>This is my code a few years ago. I was able to output multiple sheet to
>> an excel file. Nevertheless, the "append" argument appears to be obsolete.
>> Now I see only one sheet, the latest added sheet, in the output. Is there
>> any other way to do it with openxlsx::write.xlsx or other
>> functions/packages?
>>
>>
>> openxlsx::write.xlsx(df1, file=fl_out, sheetName="a",
>>  col.names=TRUE, row.names=FALSE, append=TRUE, showNA=FALSE)
>>
>> openxlsx::write.xlsx(df2, file=fl_out, sheetName="b",
>>  col.names=TRUE, row.names=FALSE,
>> append=TRUE, showNA=FALSE)
>>
>> Thanks!!
>>
> I think you need to create a workbook first, then add
> the sheets, and finally write the workbook to a file.
> Something like this:
>
> df <- data.frame(a = 1:3,
>  b = 4:6)
> 
> library("openxlsx")
> wb <- createWorkbook()
> 
> sheet <- "sheet1"
> addWorksheet(wb, sheet)
> writeData(wb, sheet = sheet, x = df)
> 
> sheet <- "sheet2"
> addWorksheet(wb, sheet)
> writeData(wb, sheet = sheet, x = df + 1)
> 
> saveWorkbook(wb, file = "~/Desktop/two_sheets.xlsx")
>
>
>

__
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] Output multiple sheets to Excel files with openxlsx::write.xlsx

2020-05-27 Thread Jeff Newmiller
give the function a list of data frames.

On May 26, 2020 11:21:58 PM PDT, John  wrote:
>Hi,
>
>This is my code a few years ago. I was able to output multiple sheet to
>an excel file. Nevertheless, the "append" argument appears to be
>obsolete.
>Now I see only one sheet, the latest added sheet, in the output. Is
>there
>any other way to do it with openxlsx::write.xlsx or other
>functions/packages?
>
>
>openxlsx::write.xlsx(df1, file=fl_out, sheetName="a",
>col.names=TRUE, row.names=FALSE, append=TRUE, showNA=FALSE)
>
>openxlsx::write.xlsx(df2, file=fl_out, sheetName="b",
> col.names=TRUE, row.names=FALSE,
>append=TRUE, showNA=FALSE)
>
>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.

-- 
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] Problem in generating an "Orthogonal fractional design"

2020-05-27 Thread Rahul Chakraborty
Dear all,

Presently I am working on designing a questionnaire for my discrete choice
experiment. I want to generate an orthogonal fractional factorial design
for the following problem-

The respondent has to choose one out of 4 objects (*X1, X2, X3, X4*). Each
of the 4 objects are classified by 10 different attributes. However, the
levels are not the same under each of the objects. The table below displays
the situation.

Attributes No. of Levels  Choices and values
X1 X2 X3 X4
A 5 1 1,2,3 3,4,5 3,4,5
B 4 1 1 1,2 3,4
C 4 1 1 2,4 3,4
D 5 1 1,2,3 1,2,3 1,4,5
E  5 1,2 2,3 3,4 5
F 2 1 1 1,2 1,2
G 2 1 1 1,2 2
H 2 1 1 1,2 1,2
I 4 1 2,3,4 2,3,4 2,3,4
J 3 1 2,3 2,3 2,3
*X* 4 1 2 3 4

The last row denotes the 4 objects.

Now I want to generate the choice sets for my questionnaire. I would like
to use *orthogonal fractional factorial design*. I kept the row with *X* in
order to sort out the redundant combinations from the choice sets.

I have the following questions-
1. *How to decide on the number of runs that one has to chose for
fractional factorial design?*  I used *AlgDesign* to generate the full
factorial which consists of 0.768 million combinations. So, I need a modest
number of runs, but how much should I target? I do not see any document
where one explains how to choose the number of trials/experimental runs.
The papers I am following only tell that they have used N number of runs
instead of the full factorial.

2. Out of 0.768 million combinations in the full factorial, there will be
many which are redundant. For example- I don't want those rows where (X=X1)
and A=(2 or 3 or 4 or 5). There are many other such cases which I don't
want in my design. I have coded all levels for each attribute and that's
why they are in the full factorial. *How do I generate an orthogonal
fractional factorial so that it does not contain such redundant
combinations?* I included the X attribute with the purpose of dropping
those combinations conditioned upon specific values of X and other factors.
Should I execute that and then generate the fractional factorial using
*optFederov* from the remaining data in the dataframe?

I would be highly obliged if you can kindly help me in this regard. I am a
student of Economics, so I do not have very deep understanding of the
statistical procedure of such algorithms. So, my question might sound
extremely naive for which I am sorry.


-- Regards,
Rahul Chakraborty
Research Fellow
National Institute of Public Finance and Policy
New Delhi- 110067

[[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] iterators : checkFunc with ireadLines

2020-05-27 Thread Laurent Rhelp

I installed raku on my PC to test your solution:

The command raku -e '.put for lines.grep( / ^^N053 | ^^N163 /, :p );'  
Laurents.txt works fine when I write it in the bash command but when I 
use the pipe command in R as you say there is nothing in lines with 
lines <- read.table(i)


There is the same problem with Ivan's solution the command grep -E 
'^(N053|N163)' test.txt works fine under the bash command but not i <- 
pipe("grep -E '^(N053|N163)' test.txt"); lines <- read.table(i)


May be it is because I work with MS windows ?

thx
LP




Le 24/05/2020 à 04:34, William Michels a écrit :

Hi Laurent,

Seeking to give you an "R-only" solution, I thought the read.fwf()
function might be useful (to read-in your first column of data, only).
However Jeff is correct that this is a poor strategy, since read.fwf()
reads the entire file into R (documented in "Fixed-width-format
files", Section 2.2: R Data Import/Export Manual).

Jeff has suggested a number of packages, as well as using a database.
Ivan Krylov has posted answers using grep, awk and perl (perl5--to
disambiguate). [In point of fact, the R Data Import/Export Manual
suggests using perl]. Similar to Ivan, I've posted code below using
the Raku programming language (the language formerly known as Perl6).
Regexes are claimed to be more readable, but are currently very slow
in Raku. However on the plus side, the language is designed to handle
Unicode gracefully:


# pipe() using raku-grep on Laurent's data (sep=mult whitespace):
con_obj1 <- pipe(paste("raku -e '.put for lines.grep( / ^^N053 | ^^N163 /, :p );' ", 
"Laurents.txt"), open="rt");
p6_import_a <- scan(file=con_obj1, what=list("","","","","","","","","",""), 
flush=TRUE, multi.line=FALSE, quiet=TRUE);
close(con_obj1);
as.data.frame(sapply(p6_import_a, t), stringsAsFactors=FALSE);

   V1   V2V3V4V5V6V7V8
   V9   V10
1  2 N053 -0.014083 -0.004741  0.001443 -0.010152 -0.012996 -0.005337
-0.008738 -0.015094
2  4 N163 -0.054023 -0.049345 -0.037158  -0.04112 -0.044612 -0.036953
-0.036061 -0.044516

# pipe() using raku-grep "starts-with" to find genbankID ( >3GB TSV file)
# "lines[0..5]" restricts raku to reading first 6 lines!
# change "lines[0..5]" to "lines" to run raku code on whole file:
con_obj2 <- pipe(paste("raku -e '.put for lines[0..5].grep( *.starts-with(q[A00145]), :p);' ", 
"genbankIDs_3GB.tsv"), "rt");
p6_import_b <- read.table(con_obj2, sep="\t");
close(con_obj2)
p6_import_b

   V1 V2   V3  V4 V5
1  4 A00145 A00145.1 IFN-alpha A NA

# unicode test using R's system() function:
try(system("raku -ne '.grep( /  你好  |  こんにちは  |  مرحبا  |  Привет  /, :v ).put;'  
hello_7lang.txt", intern = TRUE, ignore.stderr = FALSE))

[1] """"""
"你好 Chinese"
[5] "こんにちは Japanese" "مرحبا Arabic""Привет Russian"
[special thanks to Brad Gilbert, Joseph Brenner and others on the
perl6-users mailing list. All errors above are my own.]

HTH, Bill.

W. Michels, Ph.D.




On Fri, May 22, 2020 at 4:48 AM Laurent Rhelp  wrote:

Hi Ivan,
Endeed, it is a good idea. I am under MSwindows but I can use the
bash command I use with git. I will see how to do that with the unix
command lines.


Le 20/05/2020 à 09:46, Ivan Krylov a écrit :

Hi Laurent,

I am not saying this will work every time and I do recognise that this
is very different from a more general solution that you had envisioned,
but if you are on an UNIX-like system or have the relevant utilities
installed and on the %PATH% on Windows, you can filter the input file
line-by-line using a pipe and an external program:

On Sun, 17 May 2020 15:52:30 +0200
Laurent Rhelp  wrote:


# sensors to keep
sensors <-  c("N053", "N163")

# filter on the beginning of the line
i <- pipe("grep -E '^(N053|N163)' test.txt")
# or:
# filter on the beginning of the given column
# (use $2 for the second column, etc.)
i <- pipe("awk '($1 ~ \"^(N053|N163)\")' test.txt")
# or:
# since your message is full of Unicode non-breaking spaces, I have to
# bring in heavier machinery to handle those correctly;
# only this solution manages to match full column values
# (here you can also use $F[1] for second column and so on)
i <- pipe("perl -CSD -F'\\s+' -lE \\
   'print join qq{\\t}, @F if $F[0] =~ /^(N053|N163)$/' \\
   test.txt
")
lines <- read.table(i) # closes i when done

The downside of this approach is having to shell-escape the command
lines, which can become complicated, and choosing between use of regular
expressions and more wordy programs (Unicode whitespace in the input
doesn't help, either).



--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

__
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 

Re: [R] read_excel() ignore case of worksheet name?

2020-05-27 Thread Ravi Jeyaraman
Ista, With few tweaks this worked beautifully.  Thank you so much.  

-Original Message-
From: Ista Zahn [mailto:istaz...@gmail.com] 
Sent: Tuesday, May 26, 2020 11:38 PM
To: Ravi Jeyaraman 
Cc: Erin Hodgess ; r-help@r-project.org
Subject: Re: [R] read_excel() ignore case of worksheet name?

How about

read_excel_table <- function(x) {
  readxl::read_excel(
x,
sheet=grep("tables",
   excel_sheets(x),
   ignore.case = TRUE,
   value = TRUE),
.name_repair = fixColNames
  )
}

lapply(SIS$FULL_FILEPATH, read_excel_table)


--Ista

On Tue, May 26, 2020 at 11:05 PM Ravi Jeyaraman  wrote:
>
> I’ve already tried that and doesn’t work
>
>
>
> From: Erin Hodgess [mailto:erinm.hodg...@gmail.com]
> Sent: Tuesday, May 26, 2020 10:55 PM
> To: Ravi Jeyaraman 
> Cc: r-help@r-project.org
> Subject: Re: [R] read_excel() ignore case of worksheet name?
>
>
>
> Here’s a thought, please.  Could you use the tolower function and make them 
> all lower case?
>
>
>
> Thanks,
>
> Erin
>
>
>
> On Tue, May 26, 2020 at 8:21 PM Ravi Jeyaraman   > wrote:
>
> Hello All, Is there any parameter to make read_excel() ignore the 
> case-sensitiveness of the worksheet?   I'm using the below to ready in 
> multiple spreadsheets and it works perfectly fine if the worksheet is named 
> 'Tables', but fails when it's named ' TABLES'.  Any thoughts?
>
> lapply(1:nrow(SIS), function(x) 
> readxl::read_excel(SIS$FULL_FILEPATH[x], sheet='Tables', .name_repair 
> = fixColNames))
>
> Thanks in advance for your response.
>
> Cheers
> Ravi
>
>
>
> --
> This email has been checked for viruses by AVG.
> https://www.avg.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.
>
> --
>
> 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.


-- 
This email has been checked for viruses by AVG.
https://www.avg.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.


Re: [R] iterators : checkFunc with ireadLines

2020-05-27 Thread Ivan Krylov
On Wed, 27 May 2020 10:56:42 +0200
Laurent Rhelp  wrote:

> May be it is because I work with MS windows ?

That is probably the case.

On Windows, pipe() invokes "%COMSPEC% /c ", and the
rules of command line quoting are different between POSIX shell and
cmd.exe + runtimes of Windows applications [*].

Can you run raku / perl / grep / awk from the cmd prompt? If not, check
your %PATH% variable. Either way, shQuote() is supposed to be able to
handle the quoting madness for us, the inner call performing the
quoting for the runtime and the outer call escaping for the cmd.exe
itself:

pipe(shQuote(
 paste(
  'raku', '-e',
  shQuote('.put for lines.grep( / ^^N053 | ^^N163 /, :p );'),
  'Laurents.txt'
 ),
 type = 'cmd2'
))

pipe(shQuote(
 paste('grep', '-E', shQuote('^(N053|N163)'), 'test.txt'),
 'cmd2'
))

pipe(shQuote(
 paste('awk', shQuote('($1 ~ "^(N053|N163)")'), 'test.txt'),
 'cmd2'
))

pipe(shQuote(
 paste(
  'perl', '-CSD', '-F', shQuote('\\s+'), '-lE',
  shQuote('print join qq{\\t}, @F if $F[0] =~ /^(N053|N163)$/'),
  'test.txt'
 ), 'cmd2'
))

This way, we can even pretend that we are passing an _array_ of command
line arguments to the child process, like K&R intended, and not
building a command _line_ to be interpreted by the command line
interpreter and application runtime.

-- 
Best regards,
Ivan

[*] In POSIX, the command line is an array of NUL-terminated C strings.
In Windows, the command line is a single NUL-terminated C string, so
the runtime of the application is responsible for obtaining an array of
command line arguments from that:
https://docs.microsoft.com/ru-ru/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way

__
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 in generating an "Orthogonal fractional design"

2020-05-27 Thread Bert Gunter
Sorry, Off topic. This list deals with R programming questions, not
statistical questions. Try stats.stackexchange.com for those.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 1:25 AM Rahul Chakraborty 
wrote:

> Dear all,
>
> Presently I am working on designing a questionnaire for my discrete choice
> experiment. I want to generate an orthogonal fractional factorial design
> for the following problem-
>
> The respondent has to choose one out of 4 objects (*X1, X2, X3, X4*). Each
> of the 4 objects are classified by 10 different attributes. However, the
> levels are not the same under each of the objects. The table below displays
> the situation.
>
> Attributes No. of Levels  Choices and values
> X1 X2 X3 X4
> A 5 1 1,2,3 3,4,5 3,4,5
> B 4 1 1 1,2 3,4
> C 4 1 1 2,4 3,4
> D 5 1 1,2,3 1,2,3 1,4,5
> E  5 1,2 2,3 3,4 5
> F 2 1 1 1,2 1,2
> G 2 1 1 1,2 2
> H 2 1 1 1,2 1,2
> I 4 1 2,3,4 2,3,4 2,3,4
> J 3 1 2,3 2,3 2,3
> *X* 4 1 2 3 4
>
> The last row denotes the 4 objects.
>
> Now I want to generate the choice sets for my questionnaire. I would like
> to use *orthogonal fractional factorial design*. I kept the row with *X* in
> order to sort out the redundant combinations from the choice sets.
>
> I have the following questions-
> 1. *How to decide on the number of runs that one has to chose for
> fractional factorial design?*  I used *AlgDesign* to generate the full
> factorial which consists of 0.768 million combinations. So, I need a modest
> number of runs, but how much should I target? I do not see any document
> where one explains how to choose the number of trials/experimental runs.
> The papers I am following only tell that they have used N number of runs
> instead of the full factorial.
>
> 2. Out of 0.768 million combinations in the full factorial, there will be
> many which are redundant. For example- I don't want those rows where (X=X1)
> and A=(2 or 3 or 4 or 5). There are many other such cases which I don't
> want in my design. I have coded all levels for each attribute and that's
> why they are in the full factorial. *How do I generate an orthogonal
> fractional factorial so that it does not contain such redundant
> combinations?* I included the X attribute with the purpose of dropping
> those combinations conditioned upon specific values of X and other factors.
> Should I execute that and then generate the fractional factorial using
> *optFederov* from the remaining data in the dataframe?
>
> I would be highly obliged if you can kindly help me in this regard. I am a
> student of Economics, so I do not have very deep understanding of the
> statistical procedure of such algorithms. So, my question might sound
> extremely naive for which I am sorry.
>
>
> -- Regards,
> Rahul Chakraborty
> Research Fellow
> National Institute of Public Finance and Policy
> New Delhi- 110067
>
> [[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.


[R] struggling with apply

2020-05-27 Thread Michael Ashton
Hi -

I have a matrix of n rows and 4 columns.

I want to cap the value in each column by a different upper bound. So, suppose 
my matrix is

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> somematrix
 [,1] [,2] [,3] [,4]
[1,]16   127
[2,]438   11
[3,]395   11

Now I want to have the maximum value in each column described by
UB=c(2.5, 5.5, 8.5, 10.5)

So that the right answer will look like:
 [,1]  [,2][,3]   [,4]
[1,]1  5.5 8.57
[2,]2.538 10.5
[3,]2.5   5.5  510.5

I've tried a few things, like:
newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))

but I can't figure out to apply the relevant element of the UB list to the 
right element of the matrix. When I run the above, for example, it takes 
min(UB,x) over all UB, so I get:

newmatrix
 [,1] [,2] [,3] [,4]
[1,]  1.0  2.5  2.5  2.5
[2,]  2.5  2.5  2.5  2.5
[3,]  2.5  2.5  2.5  2.5

I'm sure there's a simple and elegant solution but I don't know what it is!

Thanks in advance,

Mike

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


[[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] struggling with apply

2020-05-27 Thread Jeff Newmiller
Use the pmin function, not min, for this purpose.

On May 27, 2020 10:46:13 AM PDT, Michael Ashton 
 wrote:
>Hi -
>
>I have a matrix of n rows and 4 columns.
>
>I want to cap the value in each column by a different upper bound. So,
>suppose my matrix is
>
>somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
> [,1] [,2] [,3] [,4]
>[1,]16   127
>[2,]438   11
>[3,]395   11
>
>Now I want to have the maximum value in each column described by
>UB=c(2.5, 5.5, 8.5, 10.5)
>
>So that the right answer will look like:
> [,1]  [,2][,3]   [,4]
>[1,]1  5.5 8.57
>[2,]2.538 10.5
>[3,]2.5   5.5  510.5
>
>I've tried a few things, like:
>newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
>but I can't figure out to apply the relevant element of the UB list to
>the right element of the matrix. When I run the above, for example, it
>takes min(UB,x) over all UB, so I get:
>
>newmatrix
> [,1] [,2] [,3] [,4]
>[1,]  1.0  2.5  2.5  2.5
>[2,]  2.5  2.5  2.5  2.5
>[3,]  2.5  2.5  2.5  2.5
>
>I'm sure there's a simple and elegant solution but I don't know what it
>is!
>
>Thanks in advance,
>
>Mike
>
>Michael Ashton, CFA
>Managing Principal
>
>Enduring Investments LLC
>W: 973.457.4602
>C: 551.655.8006
>Schedule a Call: https://calendly.com/m-ashton
>
>
>   [[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.


Re: [R] struggling with apply

2020-05-27 Thread Rui Barradas

Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
# [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:

Hi -

I have a matrix of n rows and 4 columns.

I want to cap the value in each column by a different upper bound. So, suppose 
my matrix is

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)

somematrix

  [,1] [,2] [,3] [,4]
[1,]16   127
[2,]438   11
[3,]395   11

Now I want to have the maximum value in each column described by
UB=c(2.5, 5.5, 8.5, 10.5)

So that the right answer will look like:
  [,1]  [,2][,3]   [,4]
[1,]1  5.5 8.57
[2,]2.538 10.5
[3,]2.5   5.5  510.5

I've tried a few things, like:
newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))

but I can't figure out to apply the relevant element of the UB list to the 
right element of the matrix. When I run the above, for example, it takes 
min(UB,x) over all UB, so I get:

newmatrix
  [,1] [,2] [,3] [,4]
[1,]  1.0  2.5  2.5  2.5
[2,]  2.5  2.5  2.5  2.5
[3,]  2.5  2.5  2.5  2.5

I'm sure there's a simple and elegant solution but I don't know what it is!

Thanks in advance,

Mike

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


[[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] struggling with apply

2020-05-27 Thread Michael Ashton
That's it! Thanks. Learn something new every day!

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


-Original Message-
From: Rui Barradas [mailto:ruipbarra...@sapo.pt] 
Sent: Wednesday, May 27, 2020 1:51 PM
To: Michael Ashton; r-help@r-project.org
Subject: Re: [R] struggling with apply

Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
# [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:
> Hi -
> 
> I have a matrix of n rows and 4 columns.
> 
> I want to cap the value in each column by a different upper bound. So, 
> suppose my matrix is
> 
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>   [,1] [,2] [,3] [,4]
> [1,]16   127
> [2,]438   11
> [3,]395   11
> 
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
> 
> So that the right answer will look like:
>   [,1]  [,2][,3]   [,4]
> [1,]1  5.5 8.57
> [2,]2.538 10.5
> [3,]2.5   5.5  510.5
> 
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> 
> but I can't figure out to apply the relevant element of the UB list to the 
> right element of the matrix. When I run the above, for example, it takes 
> min(UB,x) over all UB, so I get:
> 
> newmatrix
>   [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
> 
> I'm sure there's a simple and elegant solution but I don't know what it is!
> 
> Thanks in advance,
> 
> Mike
> 
> Michael Ashton, CFA
> Managing Principal
> 
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
> 
> 
>   [[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] Get a result but an error message as well ?

2020-05-27 Thread varin sacha via R-help
Hi Rui, Richard and other,

Many thanks for your responses that solve my problem.

Best,
SV







Le mercredi 20 mai 2020 à 09:09:49 UTC+2, Rui Barradas  a 
écrit : 





Hello,

I get an error but when running colnames(), not mean().
Notes:

1. I have changed the way the residuals are extracted, if there is a 
function resid(), the recommended practice is to use it.
2. c("MSE_OLS") and "MSE_OLS" are the identical() objects. Likewise, to 
return( c(MSE_OLS) ) is to have only MSE_OLS as the last function line. 
I find the latter clearer.


my.experiment <- function() {
  OLS <- lm(a ~ b + d)
  #MSE_OLS <- mean(OLS$residuals^2)
  MSE_OLS <- mean(resid(OLS)^2)
  MSE_OLS
}

my.data <- replicate( 500, my.experiment() )

class(my.data)  # it's a vector, not a matrix
#[1] "numeric"

colnames(my.data) <- "MSE_OLS"
#Error in `colnames<-`(`*tmp*`, value = "MSE_OLS") :
#  attempt to set 'colnames' on an object with less than two dimensions

mean(my.data)
#[1] 105.6951


Hope this helps,

Rui Barradas

Às 22:25 de 19/05/20, varin sacha escreveu:
> Hi Rui,
> 
> If I don't transpose t() the output of the replicate (my R code here below) I 
> still get an error message !!
> 
> 
> a=c(2,4,3,4,6,5,3,1,2,3,4,3,4,5,65)
> b=c(23,45,32,12,23,43,56,44,33,11,12,54,23,34,54)
> d=c(9,4,5,3,2,1,3,4,5,6,4,9,10,11,18)
> 
> my.experiment <- function() {
> 
> OLS <- lm( a ~ b+d )
> 
> MSE_OLS<-mean(OLS$residuals^2)
> 
> return( c(MSE_OLS) )
> }
> 
> my.data = replicate( 500, my.experiment() )
> colnames(my.data) <- c("MSE_OLS")
> mean(my.data)
> 
> 
> 
> 
> 
> 
> 
> Le mardi 19 mai 2020 à 23:14:21 UTC+2, Rui Barradas  a 
> écrit :
> 
> 
> 
> 
> 
> Hello,
> 
> Inline.
> 
> Às 21:38 de 19/05/20, varin sacha via R-help escreveu:
>>
>> Hi Richard,
>>
>> Thanks for your response.
>> However, how can I correct my R code knowing that I want, as a result, only 
>> one value : the mean of the 500 MSE_OLS values ?
> 
> Just don't transpose the output of replicate?
> 
> Hope this helps,
> 
> Rui Barradas
> 
>>
>>
>>
>>
>>
>>
>>
>> Le mardi 19 mai 2020 à 21:59:07 UTC+2, Richard M. Heiberger 
>>  a écrit :
>>
>>
>>
>>
>>
>>> dim(my.data)
>> [1]   1 500
>>
>> you have a matrix with a single row and 500 columns.
>> you gave a name to only the first column.
>>
>> Look at the result of replicate().  it is a vector.  You transposed it into 
>> a one-row matrix.
>>
>>>     tmp <- replicate( 500, my.experiment() )
>>> dim(tmp)
>> NULL
>>> length(tmp)
>> [1] 500
>>> dim(t(tmp))
>> [1]   1 500
>>
>>
>> On Tue, May 19, 2020 at 3:51 PM varin sacha via R-help 
>>  wrote:
>>> Dear R-experts,
>>>
>>> Here is my R code, I get a result but I also get an error message so I 
>>> doubt I can trust the result I get.
>>> What is going wrong ? Many thanks.
>>>
>>> 
>>> a<-c(2,4,3,4,6,5,3,1,2,3,4,3,4,5,65)
>>> b<-c(23,45,32,12,23,43,56,44,33,11,12,54,23,34,54)
>>> d<-c(9,4,5,3,2,1,3,4,5,6,4,9,10,11,18)
>>>
>>> my.experiment <- function( ) {
>>>
>>> OLS <- lm( a ~ b+d )
>>> MSE_OLS<-mean(OLS$residuals^2)
>>> return( c(MSE_OLS) )
>>> }
>>>
>>> my.data = t(replicate( 500, my.experiment() ))
>>> colnames(my.data) <- c("MSE_OLS")
>>> mean(my.data)
>>> 
>>>    
>>>
>>> __
>>> 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] [External] struggling with apply

2020-05-27 Thread Richard M. Heiberger
sapply(1:4, FUN=function(i, x, UB=c(2.5, 5.5, 8.5, 10.5)) {result <-
x[,i]; result[result > UB[i]] <- UB[i]; result}, x=somematrix)

On Wed, May 27, 2020 at 1:46 PM Michael Ashton
 wrote:
>
> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, 
> suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> > somematrix
>  [,1] [,2] [,3] [,4]
> [1,]16   127
> [2,]438   11
> [3,]395   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>  [,1]  [,2][,3]   [,4]
> [1,]1  5.5 8.57
> [2,]2.538 10.5
> [3,]2.5   5.5  510.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the 
> right element of the matrix. When I run the above, for example, it takes 
> min(UB,x) over all UB, so I get:
>
> newmatrix
>  [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
> [[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] struggling with apply

2020-05-27 Thread Bert Gunter
Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas  wrote:

> Hello,
>
> Try pmin. And loop by column/UB index with sapply/seq_along.
>
>
> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> # [,1] [,2] [,3] [,4]
> #[1,]  1.0  5.5  8.5  7.0
> #[2,]  2.5  3.0  8.0 10.5
> #[3,]  2.5  5.5  5.0 10.5
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 18:46 de 27/05/20, Michael Ashton escreveu:
> > Hi -
> >
> > I have a matrix of n rows and 4 columns.
> >
> > I want to cap the value in each column by a different upper bound. So,
> suppose my matrix is
> >
> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> >> somematrix
> >   [,1] [,2] [,3] [,4]
> > [1,]16   127
> > [2,]438   11
> > [3,]395   11
> >
> > Now I want to have the maximum value in each column described by
> > UB=c(2.5, 5.5, 8.5, 10.5)
> >
> > So that the right answer will look like:
> >   [,1]  [,2][,3]   [,4]
> > [1,]1  5.5 8.57
> > [2,]2.538 10.5
> > [3,]2.5   5.5  510.5
> >
> > I've tried a few things, like:
> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> >
> > but I can't figure out to apply the relevant element of the UB list to
> the right element of the matrix. When I run the above, for example, it
> takes min(UB,x) over all UB, so I get:
> >
> > newmatrix
> >   [,1] [,2] [,3] [,4]
> > [1,]  1.0  2.5  2.5  2.5
> > [2,]  2.5  2.5  2.5  2.5
> > [3,]  2.5  2.5  2.5  2.5
> >
> > I'm sure there's a simple and elegant solution but I don't know what it
> is!
> >
> > Thanks in advance,
> >
> > Mike
> >
> > Michael Ashton, CFA
> > Managing Principal
> >
> > Enduring Investments LLC
> > W: 973.457.4602
> > C: 551.655.8006
> > Schedule a Call: https://calendly.com/m-ashton
> >
> >
> >   [[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.
>

[[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] struggling with apply

2020-05-27 Thread Michael Ashton
Always amazes me how many ways there are to do these things, none of which I 
was able to find myself. Thanks! I think the key here was ‘pmin,’ which I 
didn’t know before.

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton

From: Bert Gunter [mailto:bgunter.4...@gmail.com]
Sent: Wednesday, May 27, 2020 2:22 PM
To: Rui Barradas
Cc: Michael Ashton; r-help@r-project.org
Subject: Re: [R] struggling with apply

Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along and 
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
mailto:ruipbarra...@sapo.pt>> wrote:
Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
# [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:
> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound. So, 
> suppose my matrix is
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>   [,1] [,2] [,3] [,4]
> [1,]16   127
> [2,]438   11
> [3,]395   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>   [,1]  [,2][,3]   [,4]
> [1,]1  5.5 8.57
> [2,]2.538 10.5
> [3,]2.5   5.5  510.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list to the 
> right element of the matrix. When I run the above, for example, it takes 
> min(UB,x) over all UB, so I get:
>
> newmatrix
>   [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know what it is!
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
>   [[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.

[[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] struggling with apply

2020-05-27 Thread Jeff Newmiller
Sigh. Transpose?

apply( somematrix, 2, function( x ) pmin( x, UB ) )

On May 27, 2020 11:22:06 AM PDT, Bert Gunter  wrote:
>Better, I think (no indexing):
>
>t(apply(somematrix,1,function(x)pmin(x,UB)))
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and
>sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
>wrote:
>
>> Hello,
>>
>> Try pmin. And loop by column/UB index with sapply/seq_along.
>>
>>
>> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
>> # [,1] [,2] [,3] [,4]
>> #[1,]  1.0  5.5  8.5  7.0
>> #[2,]  2.5  3.0  8.0 10.5
>> #[3,]  2.5  5.5  5.0 10.5
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> Às 18:46 de 27/05/20, Michael Ashton escreveu:
>> > Hi -
>> >
>> > I have a matrix of n rows and 4 columns.
>> >
>> > I want to cap the value in each column by a different upper bound.
>So,
>> suppose my matrix is
>> >
>> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> >> somematrix
>> >   [,1] [,2] [,3] [,4]
>> > [1,]16   127
>> > [2,]438   11
>> > [3,]395   11
>> >
>> > Now I want to have the maximum value in each column described by
>> > UB=c(2.5, 5.5, 8.5, 10.5)
>> >
>> > So that the right answer will look like:
>> >   [,1]  [,2][,3]   [,4]
>> > [1,]1  5.5 8.57
>> > [2,]2.538 10.5
>> > [3,]2.5   5.5  510.5
>> >
>> > I've tried a few things, like:
>> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>> >
>> > but I can't figure out to apply the relevant element of the UB list
>to
>> the right element of the matrix. When I run the above, for example,
>it
>> takes min(UB,x) over all UB, so I get:
>> >
>> > newmatrix
>> >   [,1] [,2] [,3] [,4]
>> > [1,]  1.0  2.5  2.5  2.5
>> > [2,]  2.5  2.5  2.5  2.5
>> > [3,]  2.5  2.5  2.5  2.5
>> >
>> > I'm sure there's a simple and elegant solution but I don't know
>what it
>> is!
>> >
>> > Thanks in advance,
>> >
>> > Mike
>> >
>> > Michael Ashton, CFA
>> > Managing Principal
>> >
>> > Enduring Investments LLC
>> > W: 973.457.4602
>> > C: 551.655.8006
>> > Schedule a Call: https://calendly.com/m-ashton
>> >
>> >
>> >   [[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.
>>
>
>   [[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] How to fix an error in nonlinear regression

2020-05-27 Thread Vahid Borji
Hello my R friends,
I am using the below commands in R:

attach(Puromycin)
Puromycin
plot(Puromycin$conc,Puromycin$rate)
mm=function(conc,vmax,k) vmax*conc/(k+conc)
mm
mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”treated”)
mm1

Unfortunately,  I receive the below error:

Error: unexpected input in
"mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”"

How can I fix this error?

[[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] struggling with apply

2020-05-27 Thread Bert Gunter
Jeff: Check it!

> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> UB=c(2.5, 5.5, 8.5, 10.5)
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
 [,1] [,2] [,3] [,4]
[1,]1  2.5  2.5  2.5
[2,]4  3.0  5.5  5.5
[3,]3  8.5  5.0  8.5
[4,]1  6.0 10.5  7.0

Not what was wanted.
Am I missing something?

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller 
wrote:

> Sigh. Transpose?
>
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
> On May 27, 2020 11:22:06 AM PDT, Bert Gunter 
> wrote:
> >Better, I think (no indexing):
> >
> >t(apply(somematrix,1,function(x)pmin(x,UB)))
> >
> >
> >Bert Gunter
> >
> >"The trouble with having an open mind is that people keep coming along
> >and
> >sticking things into it."
> >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> >On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
> >wrote:
> >
> >> Hello,
> >>
> >> Try pmin. And loop by column/UB index with sapply/seq_along.
> >>
> >>
> >> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> >> # [,1] [,2] [,3] [,4]
> >> #[1,]  1.0  5.5  8.5  7.0
> >> #[2,]  2.5  3.0  8.0 10.5
> >> #[3,]  2.5  5.5  5.0 10.5
> >>
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >>
> >> Às 18:46 de 27/05/20, Michael Ashton escreveu:
> >> > Hi -
> >> >
> >> > I have a matrix of n rows and 4 columns.
> >> >
> >> > I want to cap the value in each column by a different upper bound.
> >So,
> >> suppose my matrix is
> >> >
> >> > somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> >> >> somematrix
> >> >   [,1] [,2] [,3] [,4]
> >> > [1,]16   127
> >> > [2,]438   11
> >> > [3,]395   11
> >> >
> >> > Now I want to have the maximum value in each column described by
> >> > UB=c(2.5, 5.5, 8.5, 10.5)
> >> >
> >> > So that the right answer will look like:
> >> >   [,1]  [,2][,3]   [,4]
> >> > [1,]1  5.5 8.57
> >> > [2,]2.538 10.5
> >> > [3,]2.5   5.5  510.5
> >> >
> >> > I've tried a few things, like:
> >> > newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> >> >
> >> > but I can't figure out to apply the relevant element of the UB list
> >to
> >> the right element of the matrix. When I run the above, for example,
> >it
> >> takes min(UB,x) over all UB, so I get:
> >> >
> >> > newmatrix
> >> >   [,1] [,2] [,3] [,4]
> >> > [1,]  1.0  2.5  2.5  2.5
> >> > [2,]  2.5  2.5  2.5  2.5
> >> > [3,]  2.5  2.5  2.5  2.5
> >> >
> >> > I'm sure there's a simple and elegant solution but I don't know
> >what it
> >> is!
> >> >
> >> > Thanks in advance,
> >> >
> >> > Mike
> >> >
> >> > Michael Ashton, CFA
> >> > Managing Principal
> >> >
> >> > Enduring Investments LLC
> >> > W: 973.457.4602
> >> > C: 551.655.8006
> >> > Schedule a Call: https://calendly.com/m-ashton
> >> >
> >> >
> >> >   [[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.
> >>
> >
> >   [[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.
>

[[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] Partial matching list elements in R 4.0

2020-05-27 Thread John Harrold
Thankyou Bert and Bill.

I have one last question. Is there a tool that will recursively compare two
lists to find differences in both their structure and contents?

I'm afraid that in the process of converting code from $ to [[]] formats I
may inadvertently introduce some errors. And I'd like to QC it in some way.

Thanks
John

On Tue, May 26, 2020 at 11:33 AM William Dunlap  wrote:

> Another symptom of this problem is:
>
> > {x <- list(Abc=list(Pqr="Old Abc$Pqr")); x$Ab$Pqr <- "New Ab$Pqr" ; x}
> R version 3.6.2 (2019-12-12) | R version 4.0.0 (2020-04-24)
> List of 2| List of 2
>  $ Abc:List of 1 |  $ Abc:List of 1
>   ..$ Pqr: chr "Old Abc$Pqr" |   ..$ Pqr: chr "New Ab$Pqr"
>  $ Ab :List of 1 |  $ Ab :List of 1
>   ..$ Pqr: chr "New Ab$Pqr"  |   ..$ Pqr: chr "New Ab$Pqr"
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Tue, May 26, 2020 at 10:45 AM John Harrold 
> wrote:
>
>> Hello,
>>
>>
>> I'm testing some code in R 4.0, and I'm having an issue with the
>> following"
>>
>> # -
>> rm(list=ls())
>> graphics.off()
>> #load("/tmp/post.RData")
>> var = list();
>> # If I uncomment this it fixes things:
>> # var$options = list(mi   = list(),
>> #misc = list())
>> #
>> var$options$misc$abc = "123"
>> var$options$mi$something= 13
>> #
>>
>> This is a stripped down example but it exhibits the issue I"m having.
>> Basically when I create the list element var$options$mi the contents of
>> var$options$misc move over to var$options$mi. And what was in
>> var$options$misc become NULL:
>>
>> So now var$options looks like:
>>
>> var$options
>> $misc
>> $misc$abc
>> NULL
>>
>> $mi
>> $mi$abc
>> [1] "123"
>> $mi$something
>> [1] 13
>>
>> This worked (still works) in R 3.5.1. I understand partial matching, but
>> is
>> this normal lists moving over to elements like this? I can uncomment the
>> text mentioned in the example and it seems to fix it, but I'm wondering if
>> this is a bug or just my poor programming coming back to bite me.
>>
>> I've included my sessionInfo() at the bottom.
>>
>> Thanks
>> John
>> :wq
>>
>>
>> sessionInfo()
>>
>> R version 4.0.0 (2020-04-24)
>>
>> Platform: x86_64-apple-darwin17.0 (64-bit)
>>
>> Running under: macOS Mojave 10.14.5
>>
>>
>> Matrix products: default
>>
>> BLAS:
>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
>>
>> LAPACK:
>>
>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
>>
>>
>> locale:
>>
>> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>
>>
>> attached base packages:
>>
>> [1] stats graphics  grDevices utils datasets  methods   base
>>
>>
>> other attached packages:
>>
>> [1] gdata_2.18.0  ggplot2_3.3.0 deSolve_1.28
>>
>>
>> loaded via a namespace (and not attached):
>>
>>  [1] Rcpp_1.0.4.6 gtools_3.8.2 withr_2.2.0  assertthat_0.2.1
>>
>>  [5] dplyr_0.8.5  digest_0.6.25crayon_1.3.4 grid_4.0.0
>>
>>  [9] R6_2.4.1 lifecycle_0.2.0  gtable_0.3.0 magrittr_1.5
>>
>> [13] scales_1.1.1 pillar_1.4.4 rlang_0.4.6  vctrs_0.3.0
>>
>> [17] ellipsis_0.3.1   glue_1.4.1   purrr_0.3.4  munsell_0.5.0
>>
>> [21] compiler_4.0.0   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_1.1.0
>>
>> [25] tibble_3.0.1
>>
>> [[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.
>>
>

-- 
John
:wq

[[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] Partial matching list elements in R 4.0

2020-05-27 Thread William Dunlap via R-help
all.equal()

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, May 27, 2020 at 1:13 PM John Harrold 
wrote:

> Thankyou Bert and Bill.
>
> I have one last question. Is there a tool that will recursively compare
> two lists to find differences in both their structure and contents?
>
> I'm afraid that in the process of converting code from $ to [[]] formats I
> may inadvertently introduce some errors. And I'd like to QC it in some way.
>
> Thanks
> John
>
> On Tue, May 26, 2020 at 11:33 AM William Dunlap  wrote:
>
>> Another symptom of this problem is:
>>
>> > {x <- list(Abc=list(Pqr="Old Abc$Pqr")); x$Ab$Pqr <- "New Ab$Pqr" ; x}
>> R version 3.6.2 (2019-12-12) | R version 4.0.0 (2020-04-24)
>> List of 2| List of 2
>>  $ Abc:List of 1 |  $ Abc:List of 1
>>   ..$ Pqr: chr "Old Abc$Pqr" |   ..$ Pqr: chr "New Ab$Pqr"
>>  $ Ab :List of 1 |  $ Ab :List of 1
>>   ..$ Pqr: chr "New Ab$Pqr"  |   ..$ Pqr: chr "New Ab$Pqr"
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>>
>> On Tue, May 26, 2020 at 10:45 AM John Harrold 
>> wrote:
>>
>>> Hello,
>>>
>>>
>>> I'm testing some code in R 4.0, and I'm having an issue with the
>>> following"
>>>
>>> # -
>>> rm(list=ls())
>>> graphics.off()
>>> #load("/tmp/post.RData")
>>> var = list();
>>> # If I uncomment this it fixes things:
>>> # var$options = list(mi   = list(),
>>> #misc = list())
>>> #
>>> var$options$misc$abc = "123"
>>> var$options$mi$something= 13
>>> #
>>>
>>> This is a stripped down example but it exhibits the issue I"m having.
>>> Basically when I create the list element var$options$mi the contents of
>>> var$options$misc move over to var$options$mi. And what was in
>>> var$options$misc become NULL:
>>>
>>> So now var$options looks like:
>>>
>>> var$options
>>> $misc
>>> $misc$abc
>>> NULL
>>>
>>> $mi
>>> $mi$abc
>>> [1] "123"
>>> $mi$something
>>> [1] 13
>>>
>>> This worked (still works) in R 3.5.1. I understand partial matching, but
>>> is
>>> this normal lists moving over to elements like this? I can uncomment the
>>> text mentioned in the example and it seems to fix it, but I'm wondering
>>> if
>>> this is a bug or just my poor programming coming back to bite me.
>>>
>>> I've included my sessionInfo() at the bottom.
>>>
>>> Thanks
>>> John
>>> :wq
>>>
>>>
>>> sessionInfo()
>>>
>>> R version 4.0.0 (2020-04-24)
>>>
>>> Platform: x86_64-apple-darwin17.0 (64-bit)
>>>
>>> Running under: macOS Mojave 10.14.5
>>>
>>>
>>> Matrix products: default
>>>
>>> BLAS:
>>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
>>>
>>> LAPACK:
>>>
>>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
>>>
>>>
>>> locale:
>>>
>>> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>>
>>>
>>> attached base packages:
>>>
>>> [1] stats graphics  grDevices utils datasets  methods   base
>>>
>>>
>>> other attached packages:
>>>
>>> [1] gdata_2.18.0  ggplot2_3.3.0 deSolve_1.28
>>>
>>>
>>> loaded via a namespace (and not attached):
>>>
>>>  [1] Rcpp_1.0.4.6 gtools_3.8.2 withr_2.2.0  assertthat_0.2.1
>>>
>>>  [5] dplyr_0.8.5  digest_0.6.25crayon_1.3.4 grid_4.0.0
>>>
>>>  [9] R6_2.4.1 lifecycle_0.2.0  gtable_0.3.0 magrittr_1.5
>>>
>>> [13] scales_1.1.1 pillar_1.4.4 rlang_0.4.6  vctrs_0.3.0
>>>
>>> [17] ellipsis_0.3.1   glue_1.4.1   purrr_0.3.4  munsell_0.5.0
>>>
>>> [21] compiler_4.0.0   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_1.1.0
>>>
>>> [25] tibble_3.0.1
>>>
>>> [[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.
>>>
>>
>
> --
> John
> :wq
>

[[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] struggling with apply

2020-05-27 Thread Mathew Guilfoyle
A bit quicker:

t(pmin(t(somematrix), UB))



> On 27 May 2020, at 20:56, Bert Gunter  wrote:
> 
> Jeff: Check it!
> 
>> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> UB=c(2.5, 5.5, 8.5, 10.5)
>> apply( somematrix, 2, function( x ) pmin( x, UB ) )
> [,1] [,2] [,3] [,4]
> [1,]1  2.5  2.5  2.5
> [2,]4  3.0  5.5  5.5
> [3,]3  8.5  5.0  8.5
> [4,]1  6.0 10.5  7.0
> 
> Not what was wanted.
> Am I missing something?
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller  >
> wrote:
> 
>> Sigh. Transpose?
>> 
>> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>> 
>> On May 27, 2020 11:22:06 AM PDT, Bert Gunter 
>> wrote:
>>> Better, I think (no indexing):
>>> 
>>> t(apply(somematrix,1,function(x)pmin(x,UB)))
>>> 
>>> 
>>> Bert Gunter
>>> 
>>> "The trouble with having an open mind is that people keep coming along
>>> and
>>> sticking things into it."
>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>> 
>>> 
>>> On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
>>> wrote:
>>> 
 Hello,
 
 Try pmin. And loop by column/UB index with sapply/seq_along.
 
 
 sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
 # [,1] [,2] [,3] [,4]
 #[1,]  1.0  5.5  8.5  7.0
 #[2,]  2.5  3.0  8.0 10.5
 #[3,]  2.5  5.5  5.0 10.5
 
 
 Hope this helps,
 
 Rui Barradas
 
 
 Às 18:46 de 27/05/20, Michael Ashton escreveu:
> Hi -
> 
> I have a matrix of n rows and 4 columns.
> 
> I want to cap the value in each column by a different upper bound.
>>> So,
 suppose my matrix is
> 
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>> somematrix
>  [,1] [,2] [,3] [,4]
> [1,]16   127
> [2,]438   11
> [3,]395   11
> 
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
> 
> So that the right answer will look like:
>  [,1]  [,2][,3]   [,4]
> [1,]1  5.5 8.57
> [2,]2.538 10.5
> [3,]2.5   5.5  510.5
> 
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
> 
> but I can't figure out to apply the relevant element of the UB list
>>> to
 the right element of the matrix. When I run the above, for example,
>>> it
 takes min(UB,x) over all UB, so I get:
> 
> newmatrix
>  [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
> 
> I'm sure there's a simple and elegant solution but I don't know
>>> what it
 is!
> 
> Thanks in advance,
> 
> Mike
> 
> Michael Ashton, CFA
> Managing Principal
> 
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
> 
> 
>  [[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.
 
>>> 
>>>  [[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.
>> 
> 
>   [[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]]

_

Re: [R] Partial matching list elements in R 4.0

2020-05-27 Thread John Harrold
Awesome thanks.

On Wed, May 27, 2020 at 1:31 PM William Dunlap  wrote:

> all.equal()
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Wed, May 27, 2020 at 1:13 PM John Harrold 
> wrote:
>
>> Thankyou Bert and Bill.
>>
>> I have one last question. Is there a tool that will recursively compare
>> two lists to find differences in both their structure and contents?
>>
>> I'm afraid that in the process of converting code from $ to [[]] formats
>> I may inadvertently introduce some errors. And I'd like to QC it in some
>> way.
>>
>> Thanks
>> John
>>
>> On Tue, May 26, 2020 at 11:33 AM William Dunlap 
>> wrote:
>>
>>> Another symptom of this problem is:
>>>
>>> > {x <- list(Abc=list(Pqr="Old Abc$Pqr")); x$Ab$Pqr <- "New Ab$Pqr" ; x}
>>> R version 3.6.2 (2019-12-12) | R version 4.0.0 (2020-04-24)
>>> List of 2| List of 2
>>>  $ Abc:List of 1 |  $ Abc:List of 1
>>>   ..$ Pqr: chr "Old Abc$Pqr" |   ..$ Pqr: chr "New Ab$Pqr"
>>>  $ Ab :List of 1 |  $ Ab :List of 1
>>>   ..$ Pqr: chr "New Ab$Pqr"  |   ..$ Pqr: chr "New Ab$Pqr"
>>>
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>>
>>>
>>> On Tue, May 26, 2020 at 10:45 AM John Harrold 
>>> wrote:
>>>
 Hello,


 I'm testing some code in R 4.0, and I'm having an issue with the
 following"

 # -
 rm(list=ls())
 graphics.off()
 #load("/tmp/post.RData")
 var = list();
 # If I uncomment this it fixes things:
 # var$options = list(mi   = list(),
 #misc = list())
 #
 var$options$misc$abc = "123"
 var$options$mi$something= 13
 #

 This is a stripped down example but it exhibits the issue I"m having.
 Basically when I create the list element var$options$mi the contents of
 var$options$misc move over to var$options$mi. And what was in
 var$options$misc become NULL:

 So now var$options looks like:

 var$options
 $misc
 $misc$abc
 NULL

 $mi
 $mi$abc
 [1] "123"
 $mi$something
 [1] 13

 This worked (still works) in R 3.5.1. I understand partial matching,
 but is
 this normal lists moving over to elements like this? I can uncomment the
 text mentioned in the example and it seems to fix it, but I'm wondering
 if
 this is a bug or just my poor programming coming back to bite me.

 I've included my sessionInfo() at the bottom.

 Thanks
 John
 :wq


 sessionInfo()

 R version 4.0.0 (2020-04-24)

 Platform: x86_64-apple-darwin17.0 (64-bit)

 Running under: macOS Mojave 10.14.5


 Matrix products: default

 BLAS:

 /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib

 LAPACK:

 /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib


 locale:

 [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8


 attached base packages:

 [1] stats graphics  grDevices utils datasets  methods   base


 other attached packages:

 [1] gdata_2.18.0  ggplot2_3.3.0 deSolve_1.28


 loaded via a namespace (and not attached):

  [1] Rcpp_1.0.4.6 gtools_3.8.2 withr_2.2.0  assertthat_0.2.1

  [5] dplyr_0.8.5  digest_0.6.25crayon_1.3.4 grid_4.0.0

  [9] R6_2.4.1 lifecycle_0.2.0  gtable_0.3.0 magrittr_1.5

 [13] scales_1.1.1 pillar_1.4.4 rlang_0.4.6  vctrs_0.3.0

 [17] ellipsis_0.3.1   glue_1.4.1   purrr_0.3.4  munsell_0.5.0

 [21] compiler_4.0.0   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_1.1.0

 [25] tibble_3.0.1

 [[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.

>>>
>>
>> --
>> John
>> :wq
>>
>

-- 
John
:wq

[[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] How to fix an error in nonlinear regression

2020-05-27 Thread John Kane
You have subset=state==”treated”). You need to change "treated” to
"treated". Tho " " are formatted in your example.

On Wed, 27 May 2020 at 15:48, Vahid Borji  wrote:

> Hello my R friends,
> I am using the below commands in R:
>
> attach(Puromycin)
> Puromycin
> plot(Puromycin$conc,Puromycin$rate)
> mm=function(conc,vmax,k) vmax*conc/(k+conc)
> mm
>
> mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”treated”)
> mm1
>
> Unfortunately,  I receive the below error:
>
> Error: unexpected input in
>
> "mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”"
>
> How can I fix this error?
>
> [[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.
>


-- 
John Kane
Kingston ON Canada

[[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] struggling with apply

2020-05-27 Thread Michael Ashton
This is like "Name that Tune." Can anyone do it in FEWER characters? :-)

On May 27, 2020, at 4:32 PM, Mathew Guilfoyle  wrote:

 A bit quicker:

t(pmin(t(somematrix), UB))



On 27 May 2020, at 20:56, Bert Gunter 
mailto:bgunter.4...@gmail.com>> wrote:

Jeff: Check it!

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
UB=c(2.5, 5.5, 8.5, 10.5)
apply( somematrix, 2, function( x ) pmin( x, UB ) )
[,1] [,2] [,3] [,4]
[1,]1  2.5  2.5  2.5
[2,]4  3.0  5.5  5.5
[3,]3  8.5  5.0  8.5
[4,]1  6.0 10.5  7.0

Not what was wanted.
Am I missing something?

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller 
mailto:jdnew...@dcn.davis.ca.us>>
wrote:

Sigh. Transpose?

apply( somematrix, 2, function( x ) pmin( x, UB ) )

On May 27, 2020 11:22:06 AM PDT, Bert Gunter 
mailto:bgunter.4...@gmail.com>>
wrote:
Better, I think (no indexing):

t(apply(somematrix,1,function(x)pmin(x,UB)))


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
mailto:ruipbarra...@sapo.pt>>
wrote:

Hello,

Try pmin. And loop by column/UB index with sapply/seq_along.


sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
# [,1] [,2] [,3] [,4]
#[1,]  1.0  5.5  8.5  7.0
#[2,]  2.5  3.0  8.0 10.5
#[3,]  2.5  5.5  5.0 10.5


Hope this helps,

Rui Barradas


Às 18:46 de 27/05/20, Michael Ashton escreveu:
Hi -

I have a matrix of n rows and 4 columns.

I want to cap the value in each column by a different upper bound.
So,
suppose my matrix is

somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
somematrix
 [,1] [,2] [,3] [,4]
[1,]16   127
[2,]438   11
[3,]395   11

Now I want to have the maximum value in each column described by
UB=c(2.5, 5.5, 8.5, 10.5)

So that the right answer will look like:
 [,1]  [,2][,3]   [,4]
[1,]1  5.5 8.57
[2,]2.538 10.5
[3,]2.5   5.5  510.5

I've tried a few things, like:
newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))

but I can't figure out to apply the relevant element of the UB list
to
the right element of the matrix. When I run the above, for example,
it
takes min(UB,x) over all UB, so I get:

newmatrix
 [,1] [,2] [,3] [,4]
[1,]  1.0  2.5  2.5  2.5
[2,]  2.5  2.5  2.5  2.5
[3,]  2.5  2.5  2.5  2.5

I'm sure there's a simple and elegant solution but I don't know
what it
is!

Thanks in advance,

Mike

Michael Ashton, CFA
Managing Principal

Enduring Investments LLC
W: 973.457.4602
C: 551.655.8006
Schedule a Call: https://calendly.com/m-ashton


 [[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.


 [[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.


[[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] Partial matching list elements in R 4.0

2020-05-27 Thread Bert Gunter
?identical
?rapply might also be useful to get more granular diagnostics. (??)


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 1:13 PM John Harrold 
wrote:

> Thankyou Bert and Bill.
>
> I have one last question. Is there a tool that will recursively compare two
> lists to find differences in both their structure and contents?
>
> I'm afraid that in the process of converting code from $ to [[]] formats I
> may inadvertently introduce some errors. And I'd like to QC it in some way.
>
> Thanks
> John
>
> On Tue, May 26, 2020 at 11:33 AM William Dunlap  wrote:
>
> > Another symptom of this problem is:
> >
> > > {x <- list(Abc=list(Pqr="Old Abc$Pqr")); x$Ab$Pqr <- "New Ab$Pqr" ; x}
> > R version 3.6.2 (2019-12-12) | R version 4.0.0 (2020-04-24)
> > List of 2| List of 2
> >  $ Abc:List of 1 |  $ Abc:List of 1
> >   ..$ Pqr: chr "Old Abc$Pqr" |   ..$ Pqr: chr "New Ab$Pqr"
> >  $ Ab :List of 1 |  $ Ab :List of 1
> >   ..$ Pqr: chr "New Ab$Pqr"  |   ..$ Pqr: chr "New Ab$Pqr"
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> >
> > On Tue, May 26, 2020 at 10:45 AM John Harrold 
> > wrote:
> >
> >> Hello,
> >>
> >>
> >> I'm testing some code in R 4.0, and I'm having an issue with the
> >> following"
> >>
> >> # -
> >> rm(list=ls())
> >> graphics.off()
> >> #load("/tmp/post.RData")
> >> var = list();
> >> # If I uncomment this it fixes things:
> >> # var$options = list(mi   = list(),
> >> #misc = list())
> >> #
> >> var$options$misc$abc = "123"
> >> var$options$mi$something= 13
> >> #
> >>
> >> This is a stripped down example but it exhibits the issue I"m having.
> >> Basically when I create the list element var$options$mi the contents of
> >> var$options$misc move over to var$options$mi. And what was in
> >> var$options$misc become NULL:
> >>
> >> So now var$options looks like:
> >>
> >> var$options
> >> $misc
> >> $misc$abc
> >> NULL
> >>
> >> $mi
> >> $mi$abc
> >> [1] "123"
> >> $mi$something
> >> [1] 13
> >>
> >> This worked (still works) in R 3.5.1. I understand partial matching, but
> >> is
> >> this normal lists moving over to elements like this? I can uncomment the
> >> text mentioned in the example and it seems to fix it, but I'm wondering
> if
> >> this is a bug or just my poor programming coming back to bite me.
> >>
> >> I've included my sessionInfo() at the bottom.
> >>
> >> Thanks
> >> John
> >> :wq
> >>
> >>
> >> sessionInfo()
> >>
> >> R version 4.0.0 (2020-04-24)
> >>
> >> Platform: x86_64-apple-darwin17.0 (64-bit)
> >>
> >> Running under: macOS Mojave 10.14.5
> >>
> >>
> >> Matrix products: default
> >>
> >> BLAS:
> >>
> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
> >>
> >> LAPACK:
> >>
> >>
> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
> >>
> >>
> >> locale:
> >>
> >> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> >>
> >>
> >> attached base packages:
> >>
> >> [1] stats graphics  grDevices utils datasets  methods   base
> >>
> >>
> >> other attached packages:
> >>
> >> [1] gdata_2.18.0  ggplot2_3.3.0 deSolve_1.28
> >>
> >>
> >> loaded via a namespace (and not attached):
> >>
> >>  [1] Rcpp_1.0.4.6 gtools_3.8.2 withr_2.2.0  assertthat_0.2.1
> >>
> >>  [5] dplyr_0.8.5  digest_0.6.25crayon_1.3.4 grid_4.0.0
> >>
> >>  [9] R6_2.4.1 lifecycle_0.2.0  gtable_0.3.0 magrittr_1.5
> >>
> >> [13] scales_1.1.1 pillar_1.4.4 rlang_0.4.6  vctrs_0.3.0
> >>
> >> [17] ellipsis_0.3.1   glue_1.4.1   purrr_0.3.4  munsell_0.5.0
> >>
> >> [21] compiler_4.0.0   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_1.1.0
> >>
> >> [25] tibble_3.0.1
> >>
> >> [[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.
> >>
> >
>
> --
> John
> :wq
>
> [[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, minim

Re: [R] How to fix an error in nonlinear regression

2020-05-27 Thread Vahid Borji
Thanks a lot.

On Thu, May 28, 2020 at 1:15 AM John Kane  wrote:

> You have subset=state==”treated”). You need to change "treated” to
> "treated". Tho " " are formatted in your example.
>
> On Wed, 27 May 2020 at 15:48, Vahid Borji  wrote:
>
>> Hello my R friends,
>> I am using the below commands in R:
>>
>> attach(Puromycin)
>> Puromycin
>> plot(Puromycin$conc,Puromycin$rate)
>> mm=function(conc,vmax,k) vmax*conc/(k+conc)
>> mm
>>
>> mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”treated”)
>> mm1
>>
>> Unfortunately,  I receive the below error:
>>
>> Error: unexpected input in
>>
>> "mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”"
>>
>> How can I fix this error?
>>
>> [[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.
>>
>
>
> --
> John Kane
> Kingston ON Canada
>

[[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] struggling with apply

2020-05-27 Thread Bert Gunter
Yes, that's better --- no looping at the interpreted level.

Another version without transposing is:

nr <- 3
matrix(pmin(c(somematrix),rep(UB, e = nr)), nrow = nr)

Both treat the matrix as a vector stored in column major order.

Cheers,
Bert


On Wed, May 27, 2020 at 1:32 PM Mathew Guilfoyle 
wrote:

> A bit quicker:
>
> t(pmin(t(somematrix), UB))
>
>
>
> On 27 May 2020, at 20:56, Bert Gunter  wrote:
>
> Jeff: Check it!
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
> UB=c(2.5, 5.5, 8.5, 10.5)
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
> [,1] [,2] [,3] [,4]
> [1,]1  2.5  2.5  2.5
> [2,]4  3.0  5.5  5.5
> [3,]3  8.5  5.0  8.5
> [4,]1  6.0 10.5  7.0
>
> Not what was wanted.
> Am I missing something?
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 12:38 PM Jeff Newmiller 
> wrote:
>
> Sigh. Transpose?
>
> apply( somematrix, 2, function( x ) pmin( x, UB ) )
>
> On May 27, 2020 11:22:06 AM PDT, Bert Gunter 
> wrote:
>
> Better, I think (no indexing):
>
> t(apply(somematrix,1,function(x)pmin(x,UB)))
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 10:56 AM Rui Barradas 
> wrote:
>
> Hello,
>
> Try pmin. And loop by column/UB index with sapply/seq_along.
>
>
> sapply(seq_along(UB), function(i) pmin(UB[i], somematrix[,i]))
> # [,1] [,2] [,3] [,4]
> #[1,]  1.0  5.5  8.5  7.0
> #[2,]  2.5  3.0  8.0 10.5
> #[3,]  2.5  5.5  5.0 10.5
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 18:46 de 27/05/20, Michael Ashton escreveu:
>
> Hi -
>
> I have a matrix of n rows and 4 columns.
>
> I want to cap the value in each column by a different upper bound.
>
> So,
>
> suppose my matrix is
>
>
> somematrix <- matrix(c(1,4,3,6,3,9,12,8,5,7,11,11),nrow=3,ncol=4)
>
> somematrix
>
>  [,1] [,2] [,3] [,4]
> [1,]16   127
> [2,]438   11
> [3,]395   11
>
> Now I want to have the maximum value in each column described by
> UB=c(2.5, 5.5, 8.5, 10.5)
>
> So that the right answer will look like:
>  [,1]  [,2][,3]   [,4]
> [1,]1  5.5 8.57
> [2,]2.538 10.5
> [3,]2.5   5.5  510.5
>
> I've tried a few things, like:
> newmatrix <- apply(somematrix,c(1,2),function(x) min(UB,x))
>
> but I can't figure out to apply the relevant element of the UB list
>
> to
>
> the right element of the matrix. When I run the above, for example,
>
> it
>
> takes min(UB,x) over all UB, so I get:
>
>
> newmatrix
>  [,1] [,2] [,3] [,4]
> [1,]  1.0  2.5  2.5  2.5
> [2,]  2.5  2.5  2.5  2.5
> [3,]  2.5  2.5  2.5  2.5
>
> I'm sure there's a simple and elegant solution but I don't know
>
> what it
>
> is!
>
>
> Thanks in advance,
>
> Mike
>
> Michael Ashton, CFA
> Managing Principal
>
> Enduring Investments LLC
> W: 973.457.4602
> C: 551.655.8006
> Schedule a Call: https://calendly.com/m-ashton
>
>
>  [[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.
>
>
>  [[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.
>
>
> [[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 p

Re: [R] How to fix an error in nonlinear regression

2020-05-27 Thread Rui Barradas

Hello,

This is cross posted [1] which is not well seen. Post and wait for an 
answer, which you got and are now asking the same, why? Maybe with time 
other answer will be posted to the first, original, question.



[1] 
https://stackoverflow.com/questions/62051714/how-can-i-fix-an-error-in-nonlinear-regression-nls-function



Rui Barradas

Às 20:43 de 27/05/20, Vahid Borji escreveu:

Hello my R friends,
I am using the below commands in R:

attach(Puromycin)
Puromycin
plot(Puromycin$conc,Puromycin$rate)
mm=function(conc,vmax,k) vmax*conc/(k+conc)
mm
mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”treated”)
mm1

Unfortunately,  I receive the below error:

Error: unexpected input in
"mm1=nls(rate~mm(conc,vmax,k),data=Puromycin,start=c(vmax=50,k=0.05),subset=state==”"

How can I fix this error?

[[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] iterators : checkFunc with ireadLines

2020-05-27 Thread William Michels via R-help
Hi Laurent,

Off the bat I would have guessed that the problem you're seeing has to
do with 'command line quoting' differences between the Windows system
and the Linux/Mac systems. I've noticed people using Windows having
better command line success with "exterior double-quotes / interior
single-quotes" while Linux/Mac tend to have more success with
"exterior single- quotes / interior double-quotes". The problem is
exacerbated in R by system() or pipe() calls which require another
(exterior) set of quotations.

1. You can print out your connection object to make sure that the
interior code was read properly into R. Also, take a look at the
'connections' help page to see if there are other parameters you need
to explicitly set (like encoding). Here's the first (working) example
from my last post to you:

> ?connections
> con_obj1
  description
"raku -e '.put for lines.grep( / ^^N053 | ^^N163 /, :p );'  Laurents.txt"
class
   "pipe"
 mode
 "rt"
 text
   "text"
   opened
 "opened"
 can read
"yes"
can write
 "no"
>

2. You can try 'backslash-escaping' interior quotes in your system()
or pipe() calls. Also, in two of my previous examples I use paste() to
break up complicated quoting into more manageable chunks. You can try
these calls with 'backslash-escaped' interior quotes, and without
paste():

> con_obj1 <- pipe("raku -e \'.put for lines.grep( / ^^N053 | ^^N163 /, :p );\' 
> Laurents.txt", open="rt");
> con_obj1
 description
"raku -e '.put for lines.grep( / ^^N053 | ^^N163 /, :p );' Laurents.txt"
   class
  "pipe"
mode
"rt"
text
  "text"
  opened
"opened"
can read
   "yes"
   can write
"no"
>

3. If R creates your 'con_obj' without throwing an error, then you
should try the most basic functions for reading data into R, something
like readLines(). Again, recreate our 'con_obj' with different
encodings, if necessary. Be careful of reading from the same
connection object with multiple R functions (an unlikely scenario, but
one that should be mentioned). Below it appears that 'con_obj1' gets
consumed by readLines() before the second call to scan():

> rm(con_obj1)
> # note: dropped ':p' adverb below to simplify
> con_obj1 <- pipe("raku -e \'.put for lines.grep( / ^^N053 | ^^N163 / );\' 
> Laurents.txt", open="rt");
> scan(con_obj1)
Error in scan(con_obj1) : scan() expected 'a real', got 'N053'
> con_obj1 <- pipe("raku -e \'.put for lines.grep( / ^^N053 | ^^N163 / );\' 
> Laurents.txt", open="rt");
> readLines(con_obj1)
[1] "N053-0.014083-0.0047410.001443-0.010152 -0.012996
   -0.005337-0.008738-0.015094-0.012104"
[2] "N163-0.054023-0.049345-0.037158-0.04112 -0.044612
   -0.036953-0.036061-0.044516-0.046436"
> scan(con_obj1)
Read 0 items
numeric(0)

>

Other than that, you can post here again and we'll try to help. If you
become convinced it's a raku problem, you can check the 'raku-grep'
help page at https://docs.raku.org/routine/grep, or post a question to
the perl6-users mailing list at perl6-us...@perl.org .

HTH, Bill.

W. Michels, Ph.D.
On Wed, May 27, 2020 at 1:56 AM Laurent Rhelp  wrote:
>
> I installed raku on my PC to test your solution:
>
> The command raku -e '.put for lines.grep( / ^^N053 | ^^N163 /, :p );'
> Laurents.txt works fine when I write it in the bash command but when I
> use 

[R] Plotting shipment routs on a world map

2020-05-27 Thread Bernard McGarvey
I have used the ggplot2 package to create a world map, the png file of the 
output is attached. The code I use is below:

library(ggplot2)
library(mapproj)
long_Min <- -180.0
long_Max <- 180.0
lat_Min <- -50.0
lat_Max <- 80.0
Width <- 12.0
Height <- 2.0*Width*(lat_Max-lat_Min)/(long_Max-long_Min)
windows(width = Width, height = Height,record = TRUE)
World_map_data <- map_data("world")
World_map_dataS <- subset(World_map_data, (long >= long_Min & long <= long_Max) 
& (lat >= lat_Min & lat <= lat_Max))
#Europe_data <- map_data("europe")
O_lat <- runif(10, min=lat_Min, max=lat_Max)
O_long <- runif(10, min=long_Min, max=long_Max)
D_lat <- runif(10, min=lat_Min, max=lat_Max)
D_long <- runif(10, min=long_Min, max=long_Max)
Routes = data.frame(O_lat,O_long, D_lat,D_long)
g1 <- ggplot(World_map_dataS, aes(x=long, y=lat, group=group, fill=region)) +
  geom_polygon(fill="white", colour="blue") +
  coord_map("mercator")
g1


I have a list of shipments with the lat/long of the Origin cities and 
destination cities in a dataframe Routes. A test example is shown below:

structure(list(O_lat = c(57.8440704662353, 43.4692783257924, 
34.197948181536, -13.9658199064434, 28.1358464458026, 54.8644948145375, 
54.7105941944756, 0.960986674763262, -9.04949014307931, 67.3708150233142
), O_long = c(-135.800734693184, -139.97953729704, 130.949327526614, 
120.016278969124, -81.3588178250939, 107.188451411203, -166.396527299657, 
6.01017326116562, 119.992827912793, 68.8450227119029), D_lat = 
c(13.1392757641152, 
-48.4003935428336, -18.0977397086099, 7.47306475648656, 38.8207479682751, 
-6.95962310535833, 21.4299688511528, 77.7941568545066, -39.2934810533188, 
-5.45942842029035), D_long = c(-161.594757176936, -8.55208304710686, 
59.6322040446103, 16.3289373647422, -82.6615255884826, -150.326664168388, 
-86.496180742979, -162.877350552008, -119.206758281216, -110.952316028997
)), class = "data.frame", row.names = c(NA, -10L))


this dataframe consists of 10 shipments. How do I add these shipments as 
individual lines onto the world map. If I need to I can convert from lat & long 
to map coordinates using the mercator projection equations. My issue is what 
ggplot commands to use.

Thanks

Bernard McGarvey


Director, Fort Myers Beach Lions Foundation, Inc.


Retired (Lilly Engineering Fellow).__
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] Extracting data from list of lists of dataframes

2020-05-27 Thread H
I am a newcomer to R and have been struggling with the following problem:

I have a list1(1:N) containing a list2(1:2) each containing dataframes with the 
same column names but different number of rows. Both lists are named and the 
rows and the columns in the dataframe are named. I then would like to use 
select() and filter() on the dataframes to select data.

I would like to: (1) select among the top-level lists, (2) select among the 
second-level lists and (3) finally use select() and filter() on the dataframe 
data.

My attempt so far is:

list1[grepl("myfirstselection", names(list1)][grepl("mysecondselection", 
names(list1[[]]))]

Needless to say it does not work... And, I have not even gotten to the 
dataframes yet... ;-)

__
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 data from list of lists of dataframes

2020-05-27 Thread Bert Gunter
?reproducible example (see posting guide below)
or
https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

However, I would suggest that you spend some time with an R tutorial or two
rather than flailing about with such queries here.  R has basic tools --
indexing by logical expressions, for example -- for such tasks that you
seem completely ignorant about.  You would do better to invest the time to
learn them. Just my opinion of course. Feel free to ignore.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 6:38 PM H  wrote:

> I am a newcomer to R and have been struggling with the following problem:
>
> I have a list1(1:N) containing a list2(1:2) each containing dataframes
> with the same column names but different number of rows. Both lists are
> named and the rows and the columns in the dataframe are named. I then would
> like to use select() and filter() on the dataframes to select data.
>
> I would like to: (1) select among the top-level lists, (2) select among
> the second-level lists and (3) finally use select() and filter() on the
> dataframe data.
>
> My attempt so far is:
>
> list1[grepl("myfirstselection", names(list1)][grepl("mysecondselection",
> names(list1[[]]))]
>
> Needless to say it does not work... And, I have not even gotten to the
> dataframes yet... ;-)
>
> __
> 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] all.equal and use.names

2020-05-27 Thread John Harrold
Howdy Folks,

I believe I'm having trouble understanding the documentation for all.equal.
If I have two lists like this:

t1 = list(a = c(1,2,3),
  b = c("1", "2", "3"))
t2 = list( b = c("1", "2", "3"),
   a = c(1,2,3))

If I read the documentation correctly, by setting use.names equal to TRUE I
believe this comparison should evaluate as true:

all.equal(t1,t2, use.names=TRUE)

However, I get the following output:

which appears as though it is performing the comparison based on walking
through indices and comparing that way.

[1] "Names: 2 string mismatches"
[2] "Component 1: Modes: numeric, character"
[3] "Component 1: target is numeric, current is character"
[4] "Component 2: Modes: character, numeric"
[5] "Component 2: target is character, current is numeric"

Can someone tell me what I'm doing wrong here?
-- 
John
:wq

[[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] all.equal and use.names

2020-05-27 Thread Bert Gunter
Nope. You misread I think. It says that use.names = TRUE causes mismatches
to be **reported** by name rather than index, not that it is recursing by
name. It still recurses by component indices.

However, I still think that is wrong. It is not reporting mismatches **by**
name -- it is reporting mismatches **in** names as well as in value.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 27, 2020 at 8:23 PM John Harrold 
wrote:

> Howdy Folks,
>
> I believe I'm having trouble understanding the documentation for all.equal.
> If I have two lists like this:
>
> t1 = list(a = c(1,2,3),
>   b = c("1", "2", "3"))
> t2 = list( b = c("1", "2", "3"),
>a = c(1,2,3))
>
> If I read the documentation correctly, by setting use.names equal to TRUE I
> believe this comparison should evaluate as true:
>
> all.equal(t1,t2, use.names=TRUE)
>
> However, I get the following output:
>
> which appears as though it is performing the comparison based on walking
> through indices and comparing that way.
>
> [1] "Names: 2 string mismatches"
> [2] "Component 1: Modes: numeric, character"
> [3] "Component 1: target is numeric, current is character"
> [4] "Component 2: Modes: character, numeric"
> [5] "Component 2: target is character, current is numeric"
>
> Can someone tell me what I'm doing wrong here?
> --
> John
> :wq
>
> [[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] all.equal and use.names

2020-05-27 Thread John Harrold
Is there a way to compare t1 and t2 above such that the name is used
instead of the index?

On Wed, May 27, 2020 at 9:14 PM Bert Gunter  wrote:

> Nope. You misread I think. It says that use.names = TRUE causes mismatches
> to be **reported** by name rather than index, not that it is recursing by
> name. It still recurses by component indices.
>
> However, I still think that is wrong. It is not reporting mismatches
> **by** name -- it is reporting mismatches **in** names as well as in value.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, May 27, 2020 at 8:23 PM John Harrold 
> wrote:
>
>> Howdy Folks,
>>
>> I believe I'm having trouble understanding the documentation for
>> all.equal.
>> If I have two lists like this:
>>
>> t1 = list(a = c(1,2,3),
>>   b = c("1", "2", "3"))
>> t2 = list( b = c("1", "2", "3"),
>>a = c(1,2,3))
>>
>> If I read the documentation correctly, by setting use.names equal to TRUE
>> I
>> believe this comparison should evaluate as true:
>>
>> all.equal(t1,t2, use.names=TRUE)
>>
>> However, I get the following output:
>>
>> which appears as though it is performing the comparison based on walking
>> through indices and comparing that way.
>>
>> [1] "Names: 2 string mismatches"
>> [2] "Component 1: Modes: numeric, character"
>> [3] "Component 1: target is numeric, current is character"
>> [4] "Component 2: Modes: character, numeric"
>> [5] "Component 2: target is character, current is numeric"
>>
>> Can someone tell me what I'm doing wrong here?
>> --
>> John
>> :wq
>>
>> [[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.
>>
>

-- 
John
:wq

[[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.