Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
I understand the idea but I did not succeed. Here is what I tried: ## 1. middles of classes calculation m <- tapply(DF$x, groups, mean) ## 2. create a new factor columns with the levels deduced from ## the values of the middles of the classes ## DF$m <- DF$groups levels(DF$m) <- as.character(m) DF$m <- as.numeric(levels(DF$m))[DF$m] ## 3. I verify with xyplot xyplot( y ~ m , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## 4. I use the panel.groups and panel.bwplot to display the boxplots without success xyplot( y ~ m , groups = groups , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) , panel = panel.superpose , panel.groups=function(x,y, group.number,...){ panel.xyplot(x,y,...) panel.bwplot(x,y,...) } , horizontal = FALSE , box.width = .0001 ) ) thx Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit : Log-scales for the "factor" variable in bwplot() is not allowed. You could, however, use the panel function panel.bwplot() with xyplot(num ~ num). The potential problem with that is the box widths, which panel.bwplot() will not know how to compute. See if the following gives you a reasonable starting point: DF <- within(DF, m <- tapply(y, groups, mean)) xyplot(y ~ m, DF, scales = list(log = TRUE), panel = panel.bwplot, horizontal = FALSE, box.width = .0001) Best, -Deepayan On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp wrote: Dear R-Help list, I would like to use bwplot from the lattice package with a log scale both on the x-axis and the y-axis but I do not know how to do that because I do not know how to change the factor x-axis in a numeric x-axis. Here is my example: library(lattice) # the mock data y <- runif(10,min=0, max=500) x <- seq(0,500,length=length(y)) # I cut the x variable to create a factor variable in order to calculate the boxes groups <- cut(x,10,ordered_result = TRUE) # creating the dataframe for the lattice functions DF <- data.frame( x= x , y = y, groups = groups) ## ok for xyplot xyplot( y ~ x , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## ok for bwplot with the log scale for the y-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) # , x = list(log=T) ) ) ## Non ok for bwplot with the log scale for the x-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) which gives an error because the x-axis is a factor, I would like to replace it for the display by the meddle of every class for example and put a log scale on the x-axis. Thank you for your help Best regards Laurent -- Cet e-mail a été vérifié par le logiciel antivirus d'Avast. www.avast.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. -- Cet e-mail a été vérifié par le logiciel antivirus d'Avast. www.avast.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] lattice: how to use a log scale on the x-axis with the bwplot function
Ok for the labels but the x-axis is not displayed in log scale ? Le 10/12/2022 à 22:36, Bert Gunter a écrit : > ... and here's a version where the x variable is different than y. > It's basically the same. > > > set.seed(123) > y <- runif(40,min=0, max= 10) > x <- seq(0,10, length = 40) > ## 5 equally spaced groups labeled by log10 of the center > ## of the intervals > xrng <- range(x) > eps <- .0001*c(-1,1)*diff(xrng) ## see ?cut > xrng <-xrng + eps > delta <- diff(xrng)/5 # 5 = number of intervals > brks <- seq(xrng[1], xrng[2],length.out =6) > labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) > groups <- cut(x,breaks = brks, ordered_result = TRUE, > labels = paste0('10^',labs)) > # creating the dataframe for the lattice functions > DF <- data.frame( y = y, groups = groups) > > bwplot( y ~ groups > , data=DF > , scales = list( > y = list(log=T) > ), > ) > > On Sat, Dec 10, 2022 at 10:20 AM Bert Gunter > wrote: > > If Deepayan's suggestion does not suit and especially *if* I > understand what you want to do correctly, then it seems to me that > it is straightforward to create the groups and group labels manually: > > ## in verbose detail to hopefully improve clarity > set.seed(123) ## for reprex > y <- runif(40,min=0, max= 10) ##*small* reprex > ## cut y into 5 equally spaced groups labeled by log10 of the center > ## of the intervals > yrng <- range(y) > eps <- .0001*c(-1,1)*diff(yrng) ## see ?cut > yrng <-yrng + eps > delta <- diff(yrng)/5 # 5 = number of intervals > brks <- seq(yrng[1], yrng[2],length.out =6) > labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) > groups <- cut(y,breaks = brks, > labels = paste0('10^',labs)) > # creating the dataframe for the lattice functions > DF <- data.frame( y = y, groups = groups) > > bwplot( y ~ groups > , data=DF > , scales = list( > y = list(log=T) > ), > ) > > ## Of course, ignore if I have misunderstood. > > Cheers, > Bert > > On Sat, Dec 10, 2022 at 8:03 AM Deepayan Sarkar > wrote: > > Log-scales for the "factor" variable in bwplot() is not allowed. > > You could, however, use the panel function panel.bwplot() with > xyplot(num ~ num). The potential problem with that is the box > widths, > which panel.bwplot() will not know how to compute. > > See if the following gives you a reasonable starting point: > > DF <- within(DF, m <- tapply(y, groups, mean)) > xyplot(y ~ m, DF, scales = list(log = TRUE), > panel = panel.bwplot, horizontal = FALSE, > box.width = .0001) > > Best, > -Deepayan > > On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp > wrote: > > > > Dear R-Help list, > > > > I would like to use bwplot from the lattice package with > a log scale > > both on > > the x-axis and the y-axis but I do not know how to do that > because I do > > not know > > how to change the factor x-axis in a numeric x-axis. > > > > Here is my example: > > > > > > library(lattice) > > > > # the mock data > > y <- runif(10,min=0, max=500) > > x <- seq(0,500,length=length(y)) > > # I cut the x variable to create a factor variable in order > to calculate > > the boxes > > groups <- cut(x,10,ordered_result = TRUE) > > # creating the dataframe for the lattice functions > > DF <- data.frame( x= x , y = y, groups = groups) > > > > > > ## ok for xyplot > > xyplot( y ~ x > > , data=DF > > , scales = list( > > y = list(log=T) > > , x = list(log=T) > > > > ) > > ) > > > > ## ok for bwplot with the log scale for the y-axis > > bwplot( y ~ groups > > , data=DF > > , scales = list( > > y = list(log=T) > > # , x = list(log=T) > > > > ) > > ) > > > > > > > > ## Non ok for bwplot with the log scale for the x-axis > > bwplot( y ~ groups > > , data=DF > > , scales = list( > > y = list(log=T) > > , x = list(log=T) > > > > ) > > ) > > which gives an error because the x-axis is a factor, I would > like to > > replace it > > for the display by the meddle of every class for example and > put a log > > scale on the x-axis. > > > > Thank
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
There can be **no log scale** for the x axis, only labels, if x is a factor (Groups). If what you mean is that there are no tick marks o the x-axis, they can be added in my code in the scales list (for my reprex with 5 levels of group): scales = list( alternating = 1 ## ticls only on bottom , y = list(log=T, tck = c(1,0)) , x = list(at = 1:5, tck = 1) ) If this is not what you meant, you'll need to clarify ... or just move on. -- Bert On Sun, Dec 11, 2022 at 1:06 AM Laurent Rhelp wrote: > Ok for the labels but the x-axis is not displayed in log scale ? > > > > Le 10/12/2022 à 22:36, Bert Gunter a écrit : > > ... and here's a version where the x variable is different than y. It's > basically the same. > > > set.seed(123) > y <- runif(40,min=0, max= 10) > x <- seq(0,10, length = 40) > ## 5 equally spaced groups labeled by log10 of the center > ## of the intervals > xrng <- range(x) > eps <- .0001*c(-1,1)*diff(xrng) ## see ?cut > xrng <-xrng + eps > delta <- diff(xrng)/5 # 5 = number of intervals > brks <- seq(xrng[1], xrng[2],length.out =6) > labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) > groups <- cut(x,breaks = brks, ordered_result = TRUE, > labels = paste0('10^',labs)) > # creating the dataframe for the lattice functions > DF <- data.frame( y = y, groups = groups) > > bwplot( y ~ groups > , data=DF > , scales = list( >y = list(log=T) > ), > ) > > On Sat, Dec 10, 2022 at 10:20 AM Bert Gunter > wrote: > >> If Deepayan's suggestion does not suit and especially *if* I understand >> what you want to do correctly, then it seems to me that it is >> straightforward to create the groups and group labels manually: >> >> ## in verbose detail to hopefully improve clarity >> set.seed(123) ## for reprex >> y <- runif(40,min=0, max= 10) ##*small* reprex >> ## cut y into 5 equally spaced groups labeled by log10 of the center >> ## of the intervals >> yrng <- range(y) >> eps <- .0001*c(-1,1)*diff(yrng) ## see ?cut >> yrng <-yrng + eps >> delta <- diff(yrng)/5 # 5 = number of intervals >> brks <- seq(yrng[1], yrng[2],length.out =6) >> labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) >> groups <- cut(y,breaks = brks, >> labels = paste0('10^',labs)) >> # creating the dataframe for the lattice functions >> DF <- data.frame( y = y, groups = groups) >> >> bwplot( y ~ groups >> , data=DF >> , scales = list( >>y = list(log=T) >> ), >> ) >> >> ## Of course, ignore if I have misunderstood. >> >> Cheers, >> Bert >> >> On Sat, Dec 10, 2022 at 8:03 AM Deepayan Sarkar < >> deepayan.sar...@gmail.com> wrote: >> >>> Log-scales for the "factor" variable in bwplot() is not allowed. >>> >>> You could, however, use the panel function panel.bwplot() with >>> xyplot(num ~ num). The potential problem with that is the box widths, >>> which panel.bwplot() will not know how to compute. >>> >>> See if the following gives you a reasonable starting point: >>> >>> DF <- within(DF, m <- tapply(y, groups, mean)) >>> xyplot(y ~ m, DF, scales = list(log = TRUE), >>>panel = panel.bwplot, horizontal = FALSE, >>>box.width = .0001) >>> >>> Best, >>> -Deepayan >>> >>> On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp >>> wrote: >>> > >>> > Dear R-Help list, >>> > >>> > I would like to use bwplot from the lattice package with a log >>> scale >>> > both on >>> > the x-axis and the y-axis but I do not know how to do that because I do >>> > not know >>> > how to change the factor x-axis in a numeric x-axis. >>> > >>> > Here is my example: >>> > >>> > >>> > library(lattice) >>> > >>> > # the mock data >>> > y <- runif(10,min=0, max=500) >>> > x <- seq(0,500,length=length(y)) >>> > # I cut the x variable to create a factor variable in order to >>> calculate >>> > the boxes >>> > groups <- cut(x,10,ordered_result = TRUE) >>> > # creating the dataframe for the lattice functions >>> > DF <- data.frame( x= x , y = y, groups = groups) >>> > >>> > >>> > ## ok for xyplot >>> > xyplot( y ~ x >>> >, data=DF >>> >, scales = list( >>> > y = list(log=T) >>> > , x = list(log=T) >>> > >>> >) >>> > ) >>> > >>> > ## ok for bwplot with the log scale for the y-axis >>> > bwplot( y ~ groups >>> >, data=DF >>> >, scales = list( >>> > y = list(log=T) >>> > # , x = list(log=T) >>> > >>> >) >>> > ) >>> > >>> > >>> > >>> > ## Non ok for bwplot with the log scale for the x-axis >>> > bwplot( y ~ groups >>> > , data=DF >>> > , scales = list( >>> >y = list(log=T) >>> > , x = list(log=T) >>> > >>> > ) >>> > ) >>> > which gives an error because the x-axis is a factor, I would like to >>> > replace it >>> > for the display by the meddle of every class for example and put a log >>> > scale on the x-axis. >>> > >>> > Th
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
Indeed, I have to clarify my problem. I have to display my physical measurements in a log-log representation. I can do that with xyplot from the lattice package. But I would like to show the dispersion of my measurement. So I wanted to use the bwplot function but it is not possible because the x-axis has to be a factor (as you say). In fact I would like to be able to draw boxplots on a graph according to a numerical location on a x-axis. I understood that I cannot use the lattice functions. Le 11/12/2022 à 16:12, Bert Gunter a écrit : > There can be **no log scale** for the x axis, only labels, if x is a > factor (Groups). > > If what you mean is that there are no tick marks o the x-axis, they > can be added in my code in the scales list (for my reprex with 5 > levels of group): > > scales = list( > alternating = 1 ## ticls only on bottom > , y = list(log=T, tck = c(1,0)) > , x = list(at = 1:5, tck = 1) > ) > > If this is not what you meant, you'll need to clarify ... or just move on. > > -- Bert > > On Sun, Dec 11, 2022 at 1:06 AM Laurent Rhelp > wrote: > > Ok for the labels but the x-axis is not displayed in log scale ? > > > > Le 10/12/2022 à 22:36, Bert Gunter a écrit : >> ... and here's a version where the x variable is different than >> y. It's basically the same. >> >> >> set.seed(123) >> y <- runif(40,min=0, max= 10) >> x <- seq(0,10, length = 40) >> ## 5 equally spaced groups labeled by log10 of the center >> ## of the intervals >> xrng <- range(x) >> eps <- .0001*c(-1,1)*diff(xrng) ## see ?cut >> xrng <-xrng + eps >> delta <- diff(xrng)/5 # 5 = number of intervals >> brks <- seq(xrng[1], xrng[2],length.out =6) >> labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) >> groups <- cut(x,breaks = brks, ordered_result = TRUE, >> labels = paste0('10^',labs)) >> # creating the dataframe for the lattice functions >> DF <- data.frame( y = y, groups = groups) >> >> bwplot( y ~ groups >> , data=DF >> , scales = list( >> y = list(log=T) >> ), >> ) >> >> On Sat, Dec 10, 2022 at 10:20 AM Bert Gunter >> wrote: >> >> If Deepayan's suggestion does not suit and especially *if* I >> understand what you want to do correctly, then it seems to me >> that it is straightforward to create the groups and group >> labels manually: >> >> ## in verbose detail to hopefully improve clarity >> set.seed(123) ## for reprex >> y <- runif(40,min=0, max= 10) ##*small* reprex >> ## cut y into 5 equally spaced groups labeled by log10 of the >> center >> ## of the intervals >> yrng <- range(y) >> eps <- .0001*c(-1,1)*diff(yrng) ## see ?cut >> yrng <-yrng + eps >> delta <- diff(yrng)/5 # 5 = number of intervals >> brks <- seq(yrng[1], yrng[2],length.out =6) >> labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers) >> groups <- cut(y,breaks = brks, >> labels = paste0('10^',labs)) >> # creating the dataframe for the lattice functions >> DF <- data.frame( y = y, groups = groups) >> >> bwplot( y ~ groups >> , data=DF >> , scales = list( >> y = list(log=T) >> ), >> ) >> >> ## Of course, ignore if I have misunderstood. >> >> Cheers, >> Bert >> >> On Sat, Dec 10, 2022 at 8:03 AM Deepayan Sarkar >> wrote: >> >> Log-scales for the "factor" variable in bwplot() is not >> allowed. >> >> You could, however, use the panel function panel.bwplot() >> with >> xyplot(num ~ num). The potential problem with that is the >> box widths, >> which panel.bwplot() will not know how to compute. >> >> See if the following gives you a reasonable starting point: >> >> DF <- within(DF, m <- tapply(y, groups, mean)) >> xyplot(y ~ m, DF, scales = list(log = TRUE), >> panel = panel.bwplot, horizontal = FALSE, >> box.width = .0001) >> >> Best, >> -Deepayan >> >> On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp >> wrote: >> > >> > Dear R-Help list, >> > >> > I would like to use bwplot from the lattice package >> with a log scale >> > both on >> > the x-axis and the y-axis but I do not know how to do >> that because I do >> > not know >> > how to change the factor x-axis in a numeric x-axis. >> > >> > Here is my example: >> > >> > >> > library(lattice) >> > >>
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp wrote: > > I understand the idea but I did not succeed. > > Here is what I tried: > > ## 1.middles of classes calculation > > m <- tapply(DF$x, groups, mean) > > ## 2. create a new factor columns with the levels deduced from > ## the values of the middles of the classes > ## > > DF$m <- DF$groups > levels(DF$m) <- as.character(m) > DF$m <- as.numeric(levels(DF$m))[DF$m] > > ## 3. I verify with xyplot > > xyplot( y ~ m >, type = "p" >, data=DF >, scales = list( > y = list(log=T) > , x = list(log=T) > >) > ) > ## 4. I use the panel.groups and panel.bwplot to display the boxplots > without success > > xyplot( y ~ m >, groups = groups >, type = "p" >, data=DF >, scales = list( > y = list(log=T) > , x = list(log=T) > , panel = panel.superpose > , panel.groups=function(x,y, group.number,...){ >panel.xyplot(x,y,...) >panel.bwplot(x,y,...) > } > , horizontal = FALSE > , box.width = .0001 >) > ) You have a typo here: x = list(log=T) should be followed by a close-paren to complete scales. But I don't understand from your specification why you need groups and panel.superpose etc. What is wrong with xyplot( y ~ m ## , groups = groups # why? , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) , panel = panel.bwplot , horizontal = FALSE , box.width = .0001 ) ? And sorry for my wrong calculation of m earlier, but you can simplify your version to m <- tapply(DF$x, groups, mean) DF$m <- as.vector(m[DF$groups]) Best, -Deepayan > > > thx > > > > Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit : > > Log-scales for the "factor" variable in bwplot() is not allowed. > > > > You could, however, use the panel function panel.bwplot() with > > xyplot(num ~ num). The potential problem with that is the box widths, > > which panel.bwplot() will not know how to compute. > > > > See if the following gives you a reasonable starting point: > > > > DF <- within(DF, m <- tapply(y, groups, mean)) > > xyplot(y ~ m, DF, scales = list(log = TRUE), > > panel = panel.bwplot, horizontal = FALSE, > > box.width = .0001) > > > > Best, > > -Deepayan > > > > On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp wrote: > >> Dear R-Help list, > >> > >> I would like to use bwplot from the lattice package with a log scale > >> both on > >> the x-axis and the y-axis but I do not know how to do that because I do > >> not know > >> how to change the factor x-axis in a numeric x-axis. > >> > >>Here is my example: > >> > >> > >> library(lattice) > >> > >> # the mock data > >> y <- runif(10,min=0, max=500) > >> x <- seq(0,500,length=length(y)) > >> # I cut the x variable to create a factor variable in order to calculate > >> the boxes > >> groups <- cut(x,10,ordered_result = TRUE) > >> # creating the dataframe for the lattice functions > >> DF <- data.frame( x= x , y = y, groups = groups) > >> > >> > >> ## ok for xyplot > >> xyplot( y ~ x > >> , data=DF > >> , scales = list( > >> y = list(log=T) > >> , x = list(log=T) > >> > >> ) > >> ) > >> > >> ## ok for bwplot with the log scale for the y-axis > >> bwplot( y ~ groups > >> , data=DF > >> , scales = list( > >> y = list(log=T) > >> # , x = list(log=T) > >> > >> ) > >> ) > >> > >> > >> > >> ## Non ok for bwplot with the log scale for the x-axis > >> bwplot( y ~ groups > >> , data=DF > >> , scales = list( > >> y = list(log=T) > >> , x = list(log=T) > >> > >> ) > >> ) > >> which gives an error because the x-axis is a factor, I would like to > >> replace it > >> for the display by the meddle of every class for example and put a log > >> scale on the x-axis. > >> > >> Thank you for your help > >> Best regards > >> Laurent > >> > >> > >> > >> -- > >> Cet e-mail a été vérifié par le logiciel antivirus d'Avast. > >> www.avast.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. > > > > -- > Cet e-mail a été vérifié par le logiciel antivirus d'Avast. > www.avast.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
[R] remembering the state of an action in R....
Dear members, I am a stock trader and using R for my research. I am monitoring stock prices in real time. I have the following code: > if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches Q. I thought of something like this: > if( q >= Q ) { expr1; expr2; expr3;} But expressions keep repeating as long as q >= Q, NOT when q breaches Q for the first time. I did some research on myself and came up with this: > f <- function() {expr1; expr2; expr3; j <<- j + 1} > j <- 1; counter[[j]] <- 1; > if ((q >= Q) && length(counter) == 1) {f} I just want your help to know whether it is logically right or not. ARe not there any other possibility other than using the superassignment operator? Also, any way how to remember when the real time price q, breaches a value Q, for the first time ( the price may come back to Q again afterwards) ? 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.
Re: [R] remembering the state of an action in R....
Às 17:11 de 11/12/2022, akshay kulkarni escreveu: Dear members, I am a stock trader and using R for my research. I am monitoring stock prices in real time. I have the following code: if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches Q. I thought of something like this: if( q >= Q ) { expr1; expr2; expr3;} But expressions keep repeating as long as q >= Q, NOT when q breaches Q for the first time. I did some research on myself and came up with this: f <- function() {expr1; expr2; expr3; j <<- j + 1} j <- 1; counter[[j]] <- 1; if ((q >= Q) && length(counter) == 1) {f} I just want your help to know whether it is logically right or not. ARe not there any other possibility other than using the superassignment operator? Also, any way how to remember when the real time price q, breaches a value Q, for the first time ( the price may come back to Q again afterwards) ? 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. Hello, Use environments. You will assign to a variable living in a special place, protected from the rest of the code, that you can access any time you want. Something like the following. f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e)} Hope this helps, Rui Barradas __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
Excellent, it works. But, may you please explain me how xyplot knows that it has to apply panel.bwplot on every groups according to the groups factor ? Because there is only one panel. I introduced the groups argument in order to apply the bwplot function only on the values of every group. Le 11/12/2022 à 17:48, Deepayan Sarkar a écrit : On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp wrote: I understand the idea but I did not succeed. Here is what I tried: ## 1.middles of classes calculation m <- tapply(DF$x, groups, mean) ## 2. create a new factor columns with the levels deduced from ## the values of the middles of the classes ## DF$m <- DF$groups levels(DF$m) <- as.character(m) DF$m <- as.numeric(levels(DF$m))[DF$m] ## 3. I verify with xyplot xyplot( y ~ m , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## 4. I use the panel.groups and panel.bwplot to display the boxplots without success xyplot( y ~ m , groups = groups , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) , panel = panel.superpose , panel.groups=function(x,y, group.number,...){ panel.xyplot(x,y,...) panel.bwplot(x,y,...) } , horizontal = FALSE , box.width = .0001 ) ) You have a typo here: x = list(log=T) should be followed by a close-paren to complete scales. But I don't understand from your specification why you need groups and panel.superpose etc. What is wrong with xyplot( y ~ m ## , groups = groups # why? , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) , panel = panel.bwplot , horizontal = FALSE , box.width = .0001 ) ? And sorry for my wrong calculation of m earlier, but you can simplify your version to m <- tapply(DF$x, groups, mean) DF$m <- as.vector(m[DF$groups]) Best, -Deepayan thx Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit : Log-scales for the "factor" variable in bwplot() is not allowed. You could, however, use the panel function panel.bwplot() with xyplot(num ~ num). The potential problem with that is the box widths, which panel.bwplot() will not know how to compute. See if the following gives you a reasonable starting point: DF <- within(DF, m <- tapply(y, groups, mean)) xyplot(y ~ m, DF, scales = list(log = TRUE), panel = panel.bwplot, horizontal = FALSE, box.width = .0001) Best, -Deepayan On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp wrote: Dear R-Help list, I would like to use bwplot from the lattice package with a log scale both on the x-axis and the y-axis but I do not know how to do that because I do not know how to change the factor x-axis in a numeric x-axis. Here is my example: library(lattice) # the mock data y <- runif(10,min=0, max=500) x <- seq(0,500,length=length(y)) # I cut the x variable to create a factor variable in order to calculate the boxes groups <- cut(x,10,ordered_result = TRUE) # creating the dataframe for the lattice functions DF <- data.frame( x= x , y = y, groups = groups) ## ok for xyplot xyplot( y ~ x , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## ok for bwplot with the log scale for the y-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) # , x = list(log=T) ) ) ## Non ok for bwplot with the log scale for the x-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) which gives an error because the x-axis is a factor, I would like to replace it for the display by the meddle of every class for example and put a log scale on the x-axis. Thank you for your help Best regards Laurent -- Cet e-mail a été vérifié par le logiciel antivirus d'Avast. www.avast.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. -- Cet e-mail a été vérifié par le logiciel antivirus d'Avast. www.avast.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] remembering the state of an action in R....
Às 17:28 de 11/12/2022, Rui Barradas escreveu: Às 17:11 de 11/12/2022, akshay kulkarni escreveu: Dear members, I am a stock trader and using R for my research. I am monitoring stock prices in real time. I have the following code: if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches Q. I thought of something like this: if( q >= Q ) { expr1; expr2; expr3;} But expressions keep repeating as long as q >= Q, NOT when q breaches Q for the first time. I did some research on myself and came up with this: f <- function() {expr1; expr2; expr3; j <<- j + 1} j <- 1; counter[[j]] <- 1; if ((q >= Q) && length(counter) == 1) {f} I just want your help to know whether it is logically right or not. ARe not there any other possibility other than using the superassignment operator? Also, any way how to remember when the real time price q, breaches a value Q, for the first time ( the price may come back to Q again afterwards) ? 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. Hello, Use environments. You will assign to a variable living in a special place, protected from the rest of the code, that you can access any time you want. Something like the following. f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e)} Hope this helps, Rui Barradas __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Hello, Or with a counter. f <- function(envir) { envir$counter <- envir$counter + 1L x <- pi x } q <- 10 Q <- 5 e <- new.env() e$counter <- 0L # if ((q >= Q) && e$counter == 0L) { print(f(e)) } #> [1] 3.141593 if ((q >= Q) && e$counter == 0L) { print(f(e)) } else message("it worked!") #> it worked! Hope this helps, Rui Barradas __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remembering the state of an action in R....
Dear Rui, Thanks for your replyyour reply covers the first part of my question. What about the second part? i.e remembering the state when the price q breaches Q? Will some thing like this work: f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e); C1 <- "the price Q has been breached!"} if(C1 != "the price Q has been breached!") { do something } ANy other method? Thanking you, Yours sincerely, AKSHAY M KULKARNI From: Rui Barradas Sent: Sunday, December 11, 2022 10:58 PM To: akshay kulkarni ; R help Mailing list Subject: Re: [R] remembering the state of an action in R �s 17:11 de 11/12/2022, akshay kulkarni escreveu: > Dear members, > I am a stock trader and using R for my research. > I am monitoring stock prices in real time. I have the following code: > >> if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} > > THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches > Q. I thought of something like this: > >> if( q >= Q ) { expr1; expr2; expr3;} > > But expressions keep repeating as long as q >= Q, NOT when q breaches Q for > the first time. I did some research on myself and came up with this: > >> f <- function() {expr1; expr2; expr3; j <<- j + 1} >> j <- 1; counter[[j]] <- 1; >> if ((q >= Q) && length(counter) == 1) {f} > > I just want your help to know whether it is logically right or not. ARe not > there any other possibility other than using the superassignment operator? > > Also, any way how to remember when the real time price q, breaches a value Q, > for the first time ( the price may come back to Q again afterwards) ? > > 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. Hello, Use environments. You will assign to a variable living in a special place, protected from the rest of the code, that you can access any time you want. Something like the following. f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e)} Hope this helps, Rui Barradas [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remembering the state of an action in R....
Às 17:46 de 11/12/2022, akshay kulkarni escreveu: Dear Rui, Thanks for your replyyour reply covers the first part of my question. What about the second part? i.e remembering the state when the price q breaches Q? Will some thing like this work: f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e); C1 <- "the price Q has been breached!"} if(C1 != "the price Q has been breached!") { do something } ANy other method? Thanking you, Yours sincerely, AKSHAY M KULKARNI From: Rui Barradas Sent: Sunday, December 11, 2022 10:58 PM To: akshay kulkarni ; R help Mailing list Subject: Re: [R] remembering the state of an action in R Às 17:11 de 11/12/2022, akshay kulkarni escreveu: Dear members, I am a stock trader and using R for my research. I am monitoring stock prices in real time. I have the following code: if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches Q. I thought of something like this: if( q >= Q ) { expr1; expr2; expr3;} But expressions keep repeating as long as q >= Q, NOT when q breaches Q for the first time. I did some research on myself and came up with this: f <- function() {expr1; expr2; expr3; j <<- j + 1} j <- 1; counter[[j]] <- 1; if ((q >= Q) && length(counter) == 1) {f} I just want your help to know whether it is logically right or not. ARe not there any other possibility other than using the superassignment operator? Also, any way how to remember when the real time price q, breaches a value Q, for the first time ( the price may come back to Q again afterwards) ? 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. Hello, Use environments. You will assign to a variable living in a special place, protected from the rest of the code, that you can access any time you want. Something like the following. f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} e <- new.env() e$j <- 1L # do you need counter for something else? # if not delete these two lines counter <- list() counter[[e$j]] <- 1L # this seems to be all you need, not counter if ((q >= Q) && e$j == 1L) {f(e)} Hope this helps, Rui Barradas Hello, Envirnments are still the solution for this. You can hold another variable in the environment passed to f. f <- function(real_time_price, Q_breach = Q, envir) { envir$counter <- envir$counter + 1L envir$breached <- "the price Q has been breached" x <- pi x } q <- 10 Q <- 5 e <- new.env() e$counter <- 0L # this seems to be all you need, not counter if ((q >= Q) && e$counter == 0L) { print(f(q, Q, envir = e)) } if(e$breached != "the price Q has been breached!") { message("do something...") } Hope this helps, Rui Barradas __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
Thank you very much Deepayan, I will see the help of panel.bwplot. Best regards L. Le 11/12/2022 à 18:53, Deepayan Sarkar a écrit : On Sun, Dec 11, 2022 at 11:05 PM Laurent Rhelp wrote: Excellent, it works. But, may you please explain me how xyplot knows that it has to apply panel.bwplot on every groups according to the groups factor ? Because there is only one panel. I introduced the groups argument in order to apply the bwplot function only on the values of every group. xyplot() doesn't know that, but panel.bwplot() does. It chooses one of the variables as the grouping variable (depending on horizontal=TRUE|FALSE); see ?panel.bwplot. Best, -Deepayan Le 11/12/2022 à 17:48, Deepayan Sarkar a écrit : On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp wrote: I understand the idea but I did not succeed. Here is what I tried: ## 1.middles of classes calculation m <- tapply(DF$x, groups, mean) ## 2. create a new factor columns with the levels deduced from ## the values of the middles of the classes ## DF$m <- DF$groups levels(DF$m) <- as.character(m) DF$m <- as.numeric(levels(DF$m))[DF$m] ## 3. I verify with xyplot xyplot( y ~ m , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## 4. I use the panel.groups and panel.bwplot to display the boxplots without success xyplot( y ~ m , groups = groups , type = "p" , data=DF , scales = list( y = list(log=T) , x = list(log=T) , panel = panel.superpose , panel.groups=function(x,y, group.number,...){ panel.xyplot(x,y,...) panel.bwplot(x,y,...) } , horizontal = FALSE , box.width = .0001 ) ) You have a typo here: x = list(log=T) should be followed by a close-paren to complete scales. But I don't understand from your specification why you need groups and panel.superpose etc. What is wrong with xyplot( y ~ m ## , groups = groups # why? , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) , panel = panel.bwplot , horizontal = FALSE , box.width = .0001 ) ? And sorry for my wrong calculation of m earlier, but you can simplify your version to m <- tapply(DF$x, groups, mean) DF$m <- as.vector(m[DF$groups]) Best, -Deepayan thx Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit : Log-scales for the "factor" variable in bwplot() is not allowed. You could, however, use the panel function panel.bwplot() with xyplot(num ~ num). The potential problem with that is the box widths, which panel.bwplot() will not know how to compute. See if the following gives you a reasonable starting point: DF <- within(DF, m <- tapply(y, groups, mean)) xyplot(y ~ m, DF, scales = list(log = TRUE), panel = panel.bwplot, horizontal = FALSE, box.width = .0001) Best, -Deepayan On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp wrote: Dear R-Help list, I would like to use bwplot from the lattice package with a log scale both on the x-axis and the y-axis but I do not know how to do that because I do not know how to change the factor x-axis in a numeric x-axis. Here is my example: library(lattice) # the mock data y <- runif(10,min=0, max=500) x <- seq(0,500,length=length(y)) # I cut the x variable to create a factor variable in order to calculate the boxes groups <- cut(x,10,ordered_result = TRUE) # creating the dataframe for the lattice functions DF <- data.frame( x= x , y = y, groups = groups) ## ok for xyplot xyplot( y ~ x , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) ## ok for bwplot with the log scale for the y-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) # , x = list(log=T) ) ) ## Non ok for bwplot with the log scale for the x-axis bwplot( y ~ groups , data=DF , scales = list( y = list(log=T) , x = list(log=T) ) ) which gives an error because the x-axis is a factor, I would like to replace it for the display by the meddle of every class for example and put a log scale on the x-axis. Thank you for your help Best regards Laurent -- Cet e-mail a été vérifié par le logiciel antivirus d'Avast. www.avast.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. -- Cet e-mail a été vérifié par le logiciel a
Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function
On Sun, Dec 11, 2022 at 11:05 PM Laurent Rhelp wrote: > > Excellent, it works. > > But, may you please explain me how xyplot knows that it has to apply > panel.bwplot on every groups according to the groups factor ? Because > there is only one panel. I introduced the groups argument in order to > apply the bwplot function only on the values of every group. xyplot() doesn't know that, but panel.bwplot() does. It chooses one of the variables as the grouping variable (depending on horizontal=TRUE|FALSE); see ?panel.bwplot. Best, -Deepayan > > > Le 11/12/2022 à 17:48, Deepayan Sarkar a écrit : > > On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp wrote: > >> I understand the idea but I did not succeed. > >> > >> Here is what I tried: > >> > >> ## 1.middles of classes calculation > >> > >> m <- tapply(DF$x, groups, mean) > >> > >> ## 2. create a new factor columns with the levels deduced from > >> ## the values of the middles of the classes > >> ## > >> > >> DF$m <- DF$groups > >> levels(DF$m) <- as.character(m) > >> DF$m <- as.numeric(levels(DF$m))[DF$m] > >> > >> ## 3. I verify with xyplot > >> > >> xyplot( y ~ m > >> , type = "p" > >> , data=DF > >> , scales = list( > >> y = list(log=T) > >> , x = list(log=T) > >> > >> ) > >> ) > >> ## 4. I use the panel.groups and panel.bwplot to display the boxplots > >> without success > >> > >> xyplot( y ~ m > >> , groups = groups > >> , type = "p" > >> , data=DF > >> , scales = list( > >> y = list(log=T) > >> , x = list(log=T) > >> , panel = panel.superpose > >> , panel.groups=function(x,y, group.number,...){ > >> panel.xyplot(x,y,...) > >> panel.bwplot(x,y,...) > >> } > >> , horizontal = FALSE > >> , box.width = .0001 > >> ) > >> ) > > You have a typo here: x = list(log=T) should be followed by a > > close-paren to complete scales. > > > > But I don't understand from your specification why you need groups and > > panel.superpose etc. What is wrong with > > > > xyplot( y ~ m > > ## , groups = groups # why? > > , data=DF > > , scales = list( > > y = list(log=T) > > , x = list(log=T) > > ) > > , panel = panel.bwplot > > , horizontal = FALSE > > , box.width = .0001 > > ) > > > > ? > > > > And sorry for my wrong calculation of m earlier, but you can simplify > > your version to > > > > m <- tapply(DF$x, groups, mean) > > DF$m <- as.vector(m[DF$groups]) > > > > Best, > > -Deepayan > > > >> > >> thx > >> > >> > >> > >> Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit : > >>> Log-scales for the "factor" variable in bwplot() is not allowed. > >>> > >>> You could, however, use the panel function panel.bwplot() with > >>> xyplot(num ~ num). The potential problem with that is the box widths, > >>> which panel.bwplot() will not know how to compute. > >>> > >>> See if the following gives you a reasonable starting point: > >>> > >>> DF <- within(DF, m <- tapply(y, groups, mean)) > >>> xyplot(y ~ m, DF, scales = list(log = TRUE), > >>> panel = panel.bwplot, horizontal = FALSE, > >>> box.width = .0001) > >>> > >>> Best, > >>> -Deepayan > >>> > >>> On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp > >>> wrote: > Dear R-Help list, > > I would like to use bwplot from the lattice package with a log > scale > both on > the x-axis and the y-axis but I do not know how to do that because I do > not know > how to change the factor x-axis in a numeric x-axis. > > Here is my example: > > > library(lattice) > > # the mock data > y <- runif(10,min=0, max=500) > x <- seq(0,500,length=length(y)) > # I cut the x variable to create a factor variable in order to calculate > the boxes > groups <- cut(x,10,ordered_result = TRUE) > # creating the dataframe for the lattice functions > DF <- data.frame( x= x , y = y, groups = groups) > > > ## ok for xyplot > xyplot( y ~ x > , data=DF > , scales = list( > y = list(log=T) > , x = list(log=T) > > ) > ) > > ## ok for bwplot with the log scale for the y-axis > bwplot( y ~ groups > , data=DF > , scales = list( > y = list(log=T) > # , x = list(log=T) > > ) > ) > > > > ## Non ok for bwplot with the log scale for the x-axis > bwplot( y ~ groups > , data=DF > , scales = list( > y = list(log=T) > , x = list(log=T) > >
Re: [R] remembering the state of an action in R....
Dear Rui, Thanks a lot.. Thanking you, Yours sincerely, AKSHAY M KULKARNI From: Rui Barradas Sent: Sunday, December 11, 2022 11:27 PM To: akshay kulkarni ; R help Mailing list Subject: Re: [R] remembering the state of an action in R �s 17:46 de 11/12/2022, akshay kulkarni escreveu: > Dear Rui, > Thanks for your replyyour reply covers the first part > of my question. What about the second part? i.e remembering the state when > the price q breaches Q? Will some thing like this work: > > f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} > > e <- new.env() > e$j <- 1L > # do you need counter for something else? > # if not delete these two lines > counter <- list() > counter[[e$j]] <- 1L > # this seems to be all you need, not counter > if ((q >= Q) && e$j == 1L) {f(e); C1 <- "the price Q has been breached!"} > > if(C1 != "the price Q has been breached!") { do something } > > ANy other method? > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > > > From: Rui Barradas > Sent: Sunday, December 11, 2022 10:58 PM > To: akshay kulkarni ; R help Mailing list > > Subject: Re: [R] remembering the state of an action in R > > �s 17:11 de 11/12/2022, akshay kulkarni escreveu: >> Dear members, >> I am a stock trader and using R for my >> research. I am monitoring stock prices in real time. I have the following >> code: >> >>> if (sock price q, breaches a certain value Q) { expr1; expr2; expr3} >> >> THe point is, expr1,expr2,expr3 should execute only once, i.e when q >> breaches Q. I thought of something like this: >> >>> if( q >= Q ) { expr1; expr2; expr3;} >> >> But expressions keep repeating as long as q >= Q, NOT when q breaches Q for >> the first time. I did some research on myself and came up with this: >> >>> f <- function() {expr1; expr2; expr3; j <<- j + 1} >>> j <- 1; counter[[j]] <- 1; >>> if ((q >= Q) && length(counter) == 1) {f} >> >> I just want your help to know whether it is logically right or not. ARe not >> there any other possibility other than using the superassignment operator? >> >> Also, any way how to remember when the real time price q, breaches a value >> Q, for the first time ( the price may come back to Q again afterwards) ? >> >> 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. > Hello, > > Use environments. You will assign to a variable living in a special > place, protected from the rest of the code, that you can access any time > you want. > Something like the following. > > > > f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L} > > e <- new.env() > e$j <- 1L > # do you need counter for something else? > # if not delete these two lines > counter <- list() > counter[[e$j]] <- 1L > # this seems to be all you need, not counter > if ((q >= Q) && e$j == 1L) {f(e)} > > > > Hope this helps, > > Rui Barradas > > Hello, Envirnments are still the solution for this. You can hold another variable in the environment passed to f. f <- function(real_time_price, Q_breach = Q, envir) { envir$counter <- envir$counter + 1L envir$breached <- "the price Q has been breached" x <- pi x } q <- 10 Q <- 5 e <- new.env() e$counter <- 0L # this seems to be all you need, not counter if ((q >= Q) && e$counter == 0L) { print(f(q, Q, envir = e)) } if(e$breached != "the price Q has been breached!") { message("do something...") } Hope this helps, Rui Barradas [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Format printing with R
And you will probably want to read the details of the ?round help, so you understand how it handles 5 rounding. It is a little more complicated than some of us learned in school. On 11/22/2022 4:24 AM, Steven T. Yen wrote: Thanks to all. And yes, Ivan, round() did it: > dput(head(Mean)) c(afactfem = 0.310796641158209, afactblk = 0.188030178893171, age = 45.3185794338312, nodiscfem = 0.506637018185968, discfem = 0.493362981814032, notradgrol = 0.702915000493879) > dput(head(Std.dev)) c(afactfem = 0.462819715443265, afactblk = 0.390736267472797, age = 16.3136348021933, nodiscfem = 0.499955948049025, discfem = 0.499955948049025, notradgrol = 0.456974290933931) > round(cbind(Mean,Std.dev),2)[1:10,] Mean Std.dev afactfem 0.31 0.46 afactblk 0.19 0.39 age 45.32 16.31 nodiscfem 0.51 0.50 discfem 0.49 0.50 notradgrol 0.70 0.46 tradgrol 0.30 0.46 nofemnopol 0.80 0.40 femnopol 0.20 0.40 nopreshurt 0.66 0.47 On 11/22/2022 3:08 PM, Ivan Krylov wrote: On Tue, 22 Nov 2022 08:15:57 +0800 "Steven T. Yen" wrote: Thanks to all, but no, signif() did not work: It worked, just didn't do what you wanted it to do. I think you want round(), not signif(). Some of your numbers (45.3185794) will be rounded to 4 significant digits and others (0.096) will be rounded to 1 significant digit, but the number of decimal places will be 2. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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.