Re: [R] as.factor and floating point numbers
Hello Tobias, A factor is basically a way to get a character to behave like an integer. It consists of an integer with values from 1 to nlev, and a character vector levels, specifying for each value a level name. But this means that factors only really make sense with characters, and anything that is not a character will be forced to be a character. Thus two values that are represented by the same value in as.character will be treated as the same. Now this is probably reasonable most of the time, as numeric values will usually represent metric data, which tends to make little sense as factor. But if we want to do this we can easily build or own factors from floats, and even write some convenience wrapper around table, as shown in the appended file. Best regards, Valentin Am Mittwoch, 25. Jänner 2023, 10:03:01 CET schrieb Tobias Fellinger: > Hello, > > I'm encountering the following error: > > In a package for survival analysis I use a data.frame is created, one column > is created by applying unique on the event times while others are created > by running table on the event times and the treatment arm. > > When there are event times very close together they are put in the same > factor level when coerced to factor while unique outputs both values, > leading to different lengths of the columns. > > Try this to reproduce: > x <- c(1, 1+.Machine$double.eps) > unique(x) > table(x) > > Is there a general best practice to deal with such issues? > > Should calling table on floats be avoided in general? > > What can one use instead? > > One could easily iterate over the unique values and compare all values with > the whole vector but this are N*N comparisons, compared to N*log(N) when > sorting first and taking into account that the vector is sorted. > > I think for my purposes I'll round to a hundredth of a day before calling > the function, but any advice on avoiding this issue an writing more fault > tolerant code is greatly appreciated. > > all the best, Tobias > > > [[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. signature.asc Description: This is a digitally signed message part. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R emulation of FindRoot in Mathematica
Hello Troels, As fair as I understand you attempt to numerically solve a system of non linear equations in multiple variables in R. R does not provide this functionality natively, but have you tried multiroot from the rootSolve package: https://cran.r-project.org/web/packages/rootSolve/rootSolve.pdf[1] multiroot is called like multiroot(f, start, ...) where f is a function of one argument which is a vector of n values (representing the n variables) and returning a vector of d values (symbolising the d equations) and start is a vector of length n. E.g. if we want so solve x^2 + y^2 + z^2 = 1 x^3-y^3 = 0 x - z = 0 (which is of course equivalent to x = y = z, x^2 + y^2 + z^2 = 1, so x = y = z = ±sqrt(1/3) ~ 0.577) we’d enter f <- function(x) c(x[1]**2 + x[2]**2 + x[3]**2 - 1, x[1]**3 - x[2]**3, x[1] - x[3]) multiroot(f, c(0,0,0)) which yields $root [1] 0.5773502 0.5773505 0.5773502 $f.root [1] 1.412261e-07 -2.197939e-07 0.00e+00 $iter [1] 31 $estim.precis [1] 1.2034e-07 Best regards, Valentin Am Donnerstag, 19. Jänner 2023, 10:41:22 CET schrieb Troels Ring: > Hi friends - I hope this is not a misplaced question. From the > literature (Kushmerick AJP 1997;272:C1739-C1747) I have a series of > Mathematica equations which are solved together to yield over different > pH values the concentrations of metabolites in skeletal muscle using the > Mathematica function FindRoot((E1,E2...),(V2,V2..)] where E is a list of > equations and V list of variables. Most of the equations are individual > binding reactions of the form 10^6.494*atp*h == hatp and next > 10^9.944*hatp*h ==hhatp describing binding of singe protons or Mg or K > to ATP or creatin for example, but we also have constraints giving total > concentrations of say ATP i.e. ATP + ATPH, ATPH2..ATP.Mg > > I have, without success, tried to find ways to do this in R - I have 36 > equations on 36 variables and 8 equations on total concentrations. As > far as I can see from the definition of FindRoot in Wolfram, Newton > search or secant search is employed. > > I'm on Windows R 4.2.2 > > Best wishes > Troels Ring, MD > Aalborg, Denmark > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. [1] https://cran.r-project.org/web/packages/rootSolve/rootSolve.pdf signature.asc Description: This is a digitally signed message part. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Removing variables from data frame with a wile card
Hello Avi, while something like d$something <- ... may seem like you're directly modifying the data it does not actually do so. Most R objects try to be immutable, that is, the object may not change after creation. This guarantees that if you have a binding for same object the object won't change sneakily. There is a data structure that is in fact mutable which are environments. For example compare L <- list() local({L$a <- 3}) L$a with E <- new.env() local({E$a <- 3}) E$a The latter will in fact work, as the same Environment is modified, while in the first one a modified copy of the list is made. Under the hood we have a parser trick: If R sees something like f(a) <- ... it will look for a function f<- and call a <- f<-(a, ...) (this also happens for example when you do names(x) <- ...) So in fact in our case this is equivalent to creating a copy with removed columns and rebind the symbol in the current environment to the result. The data.table package breaks with this convention and uses C based routines that allow changing of data without copying the object. Doing d[, (cols_to_remove) := NULL] will actually change the data. Regards, Valentin 14.01.2023 18:28:33 avi.e.gr...@gmail.com: > Steven, > > Just want to add a few things to what people wrote. > > In base R, the methods mentioned will let you make a copy of your original DF > that is missing the items you are selecting that match your pattern. > > That is fine. > > For some purposes, you want to keep the original data.frame and remove a > column within it. You can do that in several ways but the simplest is > something where you sat the column to NULL as in: > > mydata$NAME <- NULL > > using the mydata["NAME"] notation can do that for you by using a loop of > unctional programming method that does that with all components of your grep. > > R does have optimizations that make this less useful as a partial copy of a > data.frame retains common parts till things change. > > For those who like to use the tidyverse, it comes with lots of tools that let > you select columns that start with or end with or contain some pattern and I > find that way easier. > > > > -Original Message- > From: R-help On Behalf Of Steven Yen > Sent: Saturday, January 14, 2023 7:49 AM > To: Andrew Simmons > Cc: R-help Mailing List > Subject: Re: [R] Removing variables from data frame with a wile card > > Thanks to all. Very helpful. > > Steven from iPhone > >> On Jan 14, 2023, at 3:08 PM, Andrew Simmons wrote: >> >> You'll want to use grep() or grepl(). By default, grep() uses >> extended regular expressions to find matches, but you can also use >> perl regular expressions and globbing (after converting to a regular >> expression). >> For example: >> >> grepl("^yr", colnames(mydata)) >> >> will tell you which 'colnames' start with "yr". If you'd rather you >> use globbing: >> >> grepl(glob2rx("yr*"), colnames(mydata)) >> >> Then you might write something like this to remove the columns starting with >> yr: >> >> mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] >> >>> On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen wrote: >>> >>> I have a data frame containing variables "yr3",...,"yr28". >>> >>> How do I remove them with a wild cardsomething similar to "del yr*" >>> in Windows/doc? Thank you. >>> colnames(mydata) >>> [1] "year" "weight" "confeduc" "confothr" "college" >>> [6] ... >>> [41] "yr3" "yr4" "yr5" "yr6" "yr7" >>> [46] "yr8" "yr9" "yr10" "yr11" "yr12" >>> [51] "yr13" "yr14" "yr15" "yr16" "yr17" >>> [56] "yr18" "yr19" "yr20" "yr21" "yr22" >>> [61] "yr23" "yr24" "yr25" "yr26" "yr27" >>> [66] "yr28"... >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/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 posti
Re: [R] return value of {....}
Hello Akshay, R is quite inspired by LISP, where this is a common thing. It is not in fact that {...} returned something, rather any expression evalulates to some value, and for a compound statement that is the last evaluated expression. {...} might be seen as similar to LISPs (begin ...). Now this is a very different thing compared to {...} in something like C, even if it looks or behaves similarly. But in R {...} is in fact an expression and thus has evaluate to some value. This also comes with some nice benefits. You do not need to use {...} for anything that is a single statement. But you can in each possible place use {...} to turn multiple statements into one. Now think about a statement like this f <- function(n) { x <- runif(n) x**2 } Then we can do y <- f(10) Now, you suggested way would look like this: f <- function(n) { x <- runif(n) y <- x**2 } And we'd need to do something like: f(10) y <- somehow_get_last_env_of_f$y So having a compound statement evaluate to a value clearly has a benefit. Best Regards, Valentin 09.01.2023 18:05:58 akshay kulkarni : > Dear Valentin, > But why should {} "return" a value? It could > just as well evaluate all the expressions and store the resulting objects in > whatever environment the interpreter chooses, and then it would be left to > the user to manipulate any object he chooses. Don't you think returning the > last, or any value, is redundant? We are living in the 21st century world, > and the R-core team might,I suppose, have a definite reason for"returning" > the last value. Any comments? > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > *From:* Valentin Petzel > *Sent:* Monday, January 9, 2023 9:18 PM > *To:* akshay kulkarni > *Cc:* R help Mailing list > *Subject:* Re: [R] return value of {} > > Hello Akshai, > > I think you are confusing {...} with local({...}). This one will evaluate the > expression in a separate environment, returning the last expression. > > {...} simply evaluates multiple expressions as one and returns the result of > the last line, but it still evaluates each expression. > > Assignment returns the assigned value, so we can chain assignments like this > > a <- 1 + (b <- 2) > > conveniently. > > So when is {...} useful? Well, anyplace where you want to execute complex > stuff in a function argument. E.g. you might do: > > data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else mean(y)}) > > Regards, > Valentin Petzel > > 09.01.2023 15:47:53 akshay kulkarni : > >> Dear members, >> I have the following code: >> >>> TB <- {x <- 3;y <- 5} >>> TB >> [1] 5 >> >> It is consistent with the documentation: For {, the result of the last >> expression evaluated. This has the visibility of the last evaluation. >> >> But both x AND y are created, but the "return value" is y. How can this be >> advantageous for solving practical problems? Specifically, consider the >> following code: >> >> F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} >> >> Both expr5 and expr7 are created, and are accessible by the code outside of >> the nested braces right? But the "return value" of the nested braces is >> expr7. So doesn't this mean that only expr7 should be accessible? Please >> help me entangle this (of course the return value of F is expr10, and all >> the other objects created by the preceding expressions are deleted. But >> expr5 is not, after the control passes outside of the nested braces!) >> >> Thanking you, >> Yours sincerely, >> AKSHAY M KULKARNI >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] return value of {....}
Hello Akshai, I think you are confusing {...} with local({...}). This one will evaluate the expression in a separate environment, returning the last expression. {...} simply evaluates multiple expressions as one and returns the result of the last line, but it still evaluates each expression. Assignment returns the assigned value, so we can chain assignments like this a <- 1 + (b <- 2) conveniently. So when is {...} useful? Well, anyplace where you want to execute complex stuff in a function argument. E.g. you might do: data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else mean(y)}) Regards, Valentin Petzel 09.01.2023 15:47:53 akshay kulkarni : > Dear members, > I have the following code: > >> TB <- {x <- 3;y <- 5} >> TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last > expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be > advantageous for solving practical problems? Specifically, consider the > following code: > > F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside of > the nested braces right? But the "return value" of the nested braces is > expr7. So doesn't this mean that only expr7 should be accessible? Please help > me entangle this (of course the return value of F is expr10, and all the > other objects created by the preceding expressions are deleted. But expr5 is > not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] datatable using dt not able to print background colors
Hello Matt, I do not think that this is the best place for troubleshooting Chrome issues. If your html file shows the colors but print doesn't then the page probably uses different stylesheets for display and print. You might want to inspect the html for something like this. If you need help, the answers to this should get you there: https://superuser.com/questions/456700/how-do-i-print-with-the-screen-stylesheet Regards, Valentin 17.12.2022 01:21:25 Matthew Pirritano : > Hey, all! > > I've got a report that uses datatable from DT to create an rmarkdown html > that looks great as an html but when I try to print it, to a printer, or to a > pdf the colors I've assigned to cells are not displaying. I'm using chrome > and I've clicked on the Background graphics button there, but that doesn't > help print the colors. I have tried to run the datatable section of the code > using results = 'asis' and eliminating results = 'asis'. Neither seems to > help with the > > My css style at the top of the rmarkdown is > > > .main-container { > max-width: 1500px; > margin-left: auto; > margin-right: auto; > table.display td { white-space: wrap; } > > } > td{ > -webkit-print-color-adjust:exact !important; > print-color-adjust:exact !important; > } > > > I added the webkit bit based on what I've found online. Maybe I have > something set up incorrectly there? Any ideas or thoughts on how to get this > to print the background colors? > > Thanks > matt > > > > > > > This communication is intended for the use of the individual or entity to > which it is addressed and may contain information that is privileged, > confidential or otherwise exempt from disclosure under applicable law. If you > are not the intended recipient, you are hereby notified that any > dissemination, distribution or copying of this communication is strictly > prohibited. If you have received this communication in error, please notify > the sender and delete any copies. Thank you. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Enabling s3 methods by attaching environment
Hello everyone! if we define some S3 method in some environment like this E <- new.env() evalq({ test <- function(x) UseMethod("test") test.character <- function(x) 1 }, envir = E) this works evalq(test("a"), envir = E) but this doesn’t: attach(E) test("a") According to some remarks this now requires the use of .S3method to register the method to a method data base. This does work, but if we create two different environments with different implementations of the same S3 method this will always default to the last defined method (as the method data base is only updated on declaration). This makes a workflow where we use environments to enable and disable certain functions impossible. So I want to ask if there is currently any way to use S3 methods in a currently attached environment while allowing to switch the method by attaching a different environment? Kind Regards, Valentin signature.asc Description: This is a digitally signed message part. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.