Re: [R] Unicode chars
Thank you for the reply I tried xeLatex at least once and possibly twice and it failed to compile I now tried it again and found that I had missed the inputenc error when I tried before. After removing the line it now compiles. Thank you Regards Duncan -- Original Message -- From: "Jeff Newmiller" To: r-help@r-project.org; "dulcalma dulcalma" ; r-help@R-project.org Sent: Thursday, 25 Aug, 2022 At 1:25 PM Subject: Re: [R] Unicode chars Are you aware that pdfLatex does not support Unicode? You need to use xeLatex. But I don't use Sweave, so I don't know how you go about making that choice. On August 24, 2022 8:03:02 PM PDT, dulcalma dulcalma wrote: Dear All I was trying the supplementary file GS_main.R from https://esajournals.onlinelibrary.wiley.com/doi/abs/10.1002/ecy.3475 I have tried to prevent latex compilation from failing using Sweave after trying all the online fixes I could find including using Rterm I could fix it if it was in the input but not in the output I am using R version 4.2 on windows 11 with 64 GB memory Sweave code \begin{small} <>= library(emdbook) # version 1.3.12 library(bbmle) # version 1.0.23.1 library(pbmcapply) # version 1.5.0 library(tidyverse) # version 1.3.0 library(ggpubr) # version 0.4.0 @ %% <>= summaryTable <- tibble(model = m.names, dim = m.dims[model], score = m.loo[model], delScore = score - min(score), se_ose = se_ose[model], se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = 1:length(dim)) summaryTable @ %% Output \begin{Schunk} \begin{Sinput} summaryTable <- tibble(model = m.names, dim = m.dims[model], score = m.loo[model], delScore = score - min(score), se_ose = se_ose[model], se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = 1:length(dim)) summaryTable \end{Sinput} \begin{Soutput} # A tibble: 10 × 7 model dim score delScore se_ose se_mod index 1 zero 2 908. 5.84 40.1 4.14 1 2 d 3 904. 1.71 40.6 2.52 2 3 q 3 907. 4.92 40.2 3.80 3 4 qd 4 902. 0 40.7 0 4 5 qdi 5 903. 0.632 40.5 1.60 5 6 x 6 908. 5.58 40.2 5.53 6 7 xq 7 907. 4.81 40.3 5.36 7 8 xd 7 905. 2.96 40.5 5.04 8 9 xqd 8 903. 0.908 40.5 4.52 9 10 xqdi 9 904. 1.89 40.4 4.70 10 \end{Soutput} \end{Schunk} The problem is the output from tibble # A tibble: 10 × 7 the \times character is Unicode U+00D7 or hex \xd7 and pdflatex lualatex etc fail where this occurs Is there a way of adding "sanitizing" code in the output before compiling Or do I have to change it manually before compiling I do not want to switch to knitr. Regards Duncan Mackay [[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] Getting minimum value of a column according a factor column of a dataframe
The requirements keep being clarified and it would have been very useful to know more in advance. To be clear. My earlier suggestion was based on JUST wanting the minimum for each unique version of Code. Then you wanted it in the original order so that was handled by carefully making that a factor column in the order you wanted the output. Now the request is to throw back in ALL the columns for as many rows as are deemed minimums. So, not in any way demeaning the various methods others offer, I suggest you look at database style joins. R has some in the core with names like merge() and other packages such as dplyr have all kinds of variations on a theme. In my case, you can extend the part of my code that makes this as a second data.frame/tibble: Code minQ 1 41003 0.160 2 8 0.160 3 41005 0.210 4 41009 0.218 5 41017 0.240 Call it df2 as in: mydf.min <- mydf %>% group_by(Code) %>% summarize(minQ = min(Q)) You now have the original thing I called mydf that has a column called Code and lots of other columns and you have a smaller one with fewer rows and columns called mydf.min and they share a single common column. You want to merge these two using whatever kind of join makes sense. Dplyr offers an inner_join(), left_join(), right_join() and full_join() and you can tweak merge() and others to do similar things. What you seem to want is to find all rows that share both a particular value for Code and at the same time a particular value for minQ in one versus Q in the other. You want to ignore all others. What gets returned can have all the original columns and perhaps also the minQ column (which can be removed) and if two or more rows in one grouping share exactly the same minimum, may get slightly similar but different lines matched. Is that what you want? Do note matching floating point equally can be dangerous but in this case since the numbers were all just read in, should be fine. You can play with it but I tried this: test <- left_join(mydf.min, mydf, by=c("Code", "minQ" = "Q")) The values returned (and not I added an 8 category earlier in my data) look like this: Code minQ Y M DNO 1 41003 0.160 81 1 19 7.17 2.50 2 8 0.160 81 1 19 7.17 2.50 3 41005 0.210 79 8 17 5.50 7.20 4 41005 0.210 80 10 30 6.84 2.60 5 41005 0.210 80 12 20 6.84 2.40 6 41009 0.218 79 2 21 5.56 4.04 7 41009 0.218 79 5 27 6.40 3.12 8 41017 0.240 79 10 20 5.30 7.10 9 41017 0.240 80 7 30 6.73 2.60 You can see there are three minimum rows for Code 41005 for example and the join keeps them all. You can trivially remove the minQ column by naming the original as Q or just removing it. You can again reorder things if you wish including by sorting on other columns ascending or descending using other functions/verbs. As noted, this has to work for you with your larger code set and sometimes complex enough such requirements can be done many ways as it can be as much art as science. I personally would probably write most of the above as one long pipeline looking a bit like: mydf <- read.table(…) %>% mutate(Code=factor(..., levels=unique(...))) result <- mydf %>% group_by(Code) %>% summarize(...) %>% left_join(mydf, by=c("Code", "minQ" = "Q")) %>% select(-minQ) And of course it may be better to use the new R pipe operator if your version is new and so on, filling in whatever details make sense to you. At the end of the pipeline, you might want to use verbs that sort the data as described above. My guess is you will now tell us about yet another condition suggestions like mine do not fulfill and I will likely then ignore … From: javad bayat Sent: Thursday, August 25, 2022 2:02 PM To: avi.e.gr...@gmail.com Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe ;Dear all First of all I appreciate you for the answers you have sent. I did the codes that Rui provided and I got what I wanted. " res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),]) res <- do.call(rbind, res) i <- order(unique(df1$Code)) res[order(i), ] " I think I should explain more about my request. I had a large data frame (11059 rows and 16 columns). The Code column represented the stations code, totally the number of stations were 128. At each station I had many measured variables, like Q and N and O, and these variables were measured in different years and days. The days that data were measured were different for each station, and due to this reason I had different rows for stations. For example, station number one (41009) had 158 rows and station number 2 (41011) had 113 rows. Note that the station's codes are not in order format (e.g smallest to largest). Back to my request, I wanted to extract the minimum value of the Q for each station (based on the Code column). The problem was that I
Re: [R] Getting minimum value of a column according a factor column of a dataframe
Hi Javad, In that case, just modify the function to extract the rows with both the minimum and maximum Q from each station df1<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", stringsAsFactors=FALSE,header=TRUE) # define a function that returns the desired rows minmaxQrow<-function(x) return(x[c(which.min(x$Q),which.max(x$Q)),]) # apply the function to the data frame df1a<-by(df1,df1$Code,minmaxQrow) # set the result to the first element of the list df1b<-df1a[[1]] # rbind the remaining rows for(i in 2:length(df1a)) df1b<-rbind(df1b,df1a[[i]]) # display the result df1b Jim On Fri, Aug 26, 2022 at 5:25 AM javad bayat wrote: > ... > I think I should explain more about my request. I had a large data frame > (11059 rows and 16 columns). The Code column represented the stations code, > totally the number of stations were 128. At each station I had many > measured variables, like Q and N and O, and these variables were measured > in different years and days. The days that data were measured were > different for each station, and due to this reason I had different rows for > stations. For example, station number one (41009) had 158 rows and station > number 2 (41011) had 113 rows. Note that the station's codes are not in > order format (e.g smallest to largest). > Back to my request, I wanted to extract the minimum value of the Q for each > station (based on the Code column). The problem was that I wanted to have > other column values which were measured for this minimum of the Q. > I hope my explanation was clear enough. As I said before, I used the Rui's > codes and I got what I wanted. Although, other solutions provided by others > were all correct. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Fixing the size of R's Graphic Device
I really dont know what could be the right value. So I am trying to get the right value based on trial and error. All I want is that the graphic windows' height should occupy the height of my screen and width should be ~70% of width of my scream I am using Mac desktop (21.5 inch 2017 model) but running Windows. On Fri, Aug 26, 2022 at 1:02 AM Jeff Newmiller wrote: > > Please read ?windows. There is a limit to the default size, which I am > guessing is related to the default window stacking behavior. > > Also, units of width and height are in inches ... do you really have a 300 > inch display? > > On August 25, 2022 12:05:00 PM PDT, Christofer Bogaso > wrote: > >Thanks. > > > >But this is woking upto some number > > > >For example > > > >options(device=function()windows(width=303,height=354,xpos=-5,ypos=9)) > >graphics.off() > >plot(1:10) > > > >and > > > >options(device=function()windows(width=303,height=394,xpos=-5,ypos=9)) > >graphics.off() > >plot(1:10) > > > >do not change the height of device > > > >On Fri, Aug 26, 2022 at 12:14 AM Bill Dunlap > >wrote: > >> > >> In one of your R startup files you can set options("device") to a function > >> that dev.new() will call when a new plot window is requested. E.g., > >> > >> options(device=function()windows(width=3,height=4.5,xpos=-200,ypos=100)) > >> graphics.off() > >> plot(1:10) > >> > >> > >> -Bill > >> > >> > >> On Thu, Aug 25, 2022 at 11:11 AM Christofer Bogaso > >> wrote: > >>> > >>> Hi, > >>> > >>> I am wondering if there is any way to fix the size (i.e. height and > >>> width) of R's graphic device permanently. Every time I open R, and > >>> create my first plot, the default size of the graphic device is fairly > >>> small, and I need to adjust it manually to make it of comfortable > >>> size. > >>> > >>> Any help is really appreciated. > >>> > >>> Thanks for your time. > >>> > >>> __ > >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >>> https://stat.ethz.ch/mailman/listinfo/r-help > >>> PLEASE do read the posting guide > >>> http://www.R-project.org/posting-guide.html > >>> and provide commented, minimal, self-contained, reproducible code. > > > >__ > >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > > -- > 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] Fixing the size of R's Graphic Device
Thanks. But this is woking upto some number For example options(device=function()windows(width=303,height=354,xpos=-5,ypos=9)) graphics.off() plot(1:10) and options(device=function()windows(width=303,height=394,xpos=-5,ypos=9)) graphics.off() plot(1:10) do not change the height of device On Fri, Aug 26, 2022 at 12:14 AM Bill Dunlap wrote: > > In one of your R startup files you can set options("device") to a function > that dev.new() will call when a new plot window is requested. E.g., > > options(device=function()windows(width=3,height=4.5,xpos=-200,ypos=100)) > graphics.off() > plot(1:10) > > > -Bill > > > On Thu, Aug 25, 2022 at 11:11 AM Christofer Bogaso > wrote: >> >> Hi, >> >> I am wondering if there is any way to fix the size (i.e. height and >> width) of R's graphic device permanently. Every time I open R, and >> create my first plot, the default size of the graphic device is fairly >> small, and I need to adjust it manually to make it of comfortable >> size. >> >> Any help is really appreciated. >> >> Thanks for your time. >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fixing the size of R's Graphic Device
Hi, I am using a large screen monitor, the default size comes with fairly small maybe smaller than a quarter. Is there any to get exact measure of the default size? I am using R in WIndows 11 OS On Thu, Aug 25, 2022 at 11:54 PM Jeff Newmiller wrote: > > What is your default graphics device? That is, what OS are you using? > > On August 25, 2022 11:10:45 AM PDT, Christofer Bogaso > wrote: > >Hi, > > > >I am wondering if there is any way to fix the size (i.e. height and > >width) of R's graphic device permanently. Every time I open R, and > >create my first plot, the default size of the graphic device is fairly > >small, and I need to adjust it manually to make it of comfortable > >size. > > > >Any help is really appreciated. > > > >Thanks for your time. > > > >__ > >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > > -- > 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] Fixing the size of R's Graphic Device
See ?Startup for various ways of automatically executing custom code at R start up. See ?setHook and ?.onLoad for how to run custom code when packages (like grDevices) are loaded. (But Jeff may be able to help you avoid even this if you respond to his queries). Cheers, Bert On Thu, Aug 25, 2022 at 11:11 AM Christofer Bogaso wrote: > > Hi, > > I am wondering if there is any way to fix the size (i.e. height and > width) of R's graphic device permanently. Every time I open R, and > create my first plot, the default size of the graphic device is fairly > small, and I need to adjust it manually to make it of comfortable > size. > > Any help is really appreciated. > > Thanks for your time. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Fixing the size of R's Graphic Device
Hi, I am wondering if there is any way to fix the size (i.e. height and width) of R's graphic device permanently. Every time I open R, and create my first plot, the default size of the graphic device is fairly small, and I need to adjust it manually to make it of comfortable size. Any help is really appreciated. Thanks for your time. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting minimum value of a column according a factor column of a dataframe
Yes, Timothy, the request was not seen by all of us as the same. Indeed if the request was to show a subset of the original data consisting of only the rows that were the minimum for each Code and also showed ties, then the solution is a tad more complex. I would then do something along the lines of what others showed such as generating another column showing the minimum for each row and then showing only rows that matched their value in two columns or whatever was needed. As noted, keeping the output in a specific order was not initially requested. Keeping the data in some order is a common enough request but in this situation, I suspect the order many might want would be the one showing the minimums in order, not the codes in the original order. -Original Message- From: Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 11:59 AM To: avi.e.gr...@gmail.com Cc: R-help@r-project.org Subject: RE: [R] Getting minimum value of a column according a factor column of a dataframe My assumption (maybe wrong) was that we needed to keep the other variables. I want to find the values of Y, M, D, N, and O for the minimum value of Q within each unique value of Code, keeping the data in the original order. All one need to do is filter Q in the original dataframe by your answer for minQ. Keeping the data in the original order seems unnecessary, but that is what was asked in a later post. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting minimum value of a column according a factor column of a dataframe
The order of the rows returned by summarize is controlled by the levels of the factors given to group_by. If group_by is given character columns instead of factors, it converts them to factors with the levels being the sorted unique values of the character columns. Convert you character columns to factors with the desired levels to get the order you want. E.g., > d <- data.frame(charGroup=rep(c("Small","Medium","Large"),3:1), x=101:106) > d$factorGroup <- factor(d$charGroup, levels=c("Small","Medium","Large","X Large")) > d charGroup x factorGroup 1 Small 101 Small 2 Small 102 Small 3 Small 103 Small 4Medium 104 Medium 5Medium 105 Medium 6 Large 106 Large > d |> group_by(charGroup) |> summarize(minX=min(x)) # A tibble: 3 × 2 charGroup minX 1 Large 106 2 Medium 104 3 Small 101 > d |> group_by(factorGroup) |> summarize(minX=min(x)) # A tibble: 3 × 2 factorGroup minX 1 Small 101 2 Medium104 3 Large 106 > # .drop=FALSE outputs a row for each unused level as well > d |> group_by(factorGroup, .drop=FALSE) |> summarize(minX=min(x)) # A tibble: 4 × 2 factorGroup minX 1 Small 101 2 Medium104 3 Large 106 4 X Large Inf Warning message: In min(x) : no non-missing arguments to min; returning Inf -Bill On Thu, Aug 25, 2022 at 4:05 AM javad bayat wrote: > Dear Rui; > Thank you very much. Both of your codes worked correctly. Now I can see the > whole row's value. > But I found a problem in the results. When I run your codes, the results > are shown in a sorted table. I do not know why the results have been sorted > according to the "Code" column, smallest to largest. Is there any way to > get the results like their order in the first data frame (bilan2)? I used > your codes as follow: > > > bilan3 <- lapply(split(bilan2, bilan2$Code), \(x) x[which.min(x$Q),]) > > bilan3 = data.frame(do.call(rbind, bilan3)) > Sincerely > > > > On Thu, Aug 25, 2022 at 11:52 AM Rui Barradas > wrote: > > > Hello, > > > > OK, what about > > > > > > res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),]) > > do.call(rbind, res) > > # Code Y M D QNO > > # 41003 41003 81 1 19 0.160 7.17 2.50 > > # 41005 41005 79 8 17 0.210 5.50 7.20 > > # 41009 41009 79 2 21 0.218 5.56 4.04 > > # 41017 41017 79 10 20 0.240 5.30 7.10 > > > > > > A dplyr solution. > > > > > > > > suppressPackageStartupMessages(library(dplyr)) > > > > df1 %>% > >group_by(Code) %>% > >slice_min(Q) %>% > >slice_head(n = 1) > > # # A tibble: 4 × 7 > > # # Groups: Code [4] > > #Code Y M D Q N O > > # > > # 1 4100381 119 0.16 7.17 2.5 > > # 2 4100579 817 0.21 5.5 7.2 > > # 3 4100979 221 0.218 5.56 4.04 > > # 4 41017791020 0.24 5.3 7.1 > > > > > > > > Hope this helps, > > > > Rui Barradas > > > > > > Às 05:56 de 25/08/2022, javad bayat escreveu: > > > Dear all, > > > Many thanks for your suggested methods and codes, but unfortunately > they > > > did not give the desired results. > > > All the codes you have provided are correct but they did not represent > > the > > > whole row which is related to the minimum of "Q". > > > The code must result in 4 rows, with the minimum value of "Q" and other > > > column values, as below: > > > > > > Code > > > > > >Y > > > > > >M > > > > > > D > > > > > > Q > > > > > > N > > > > > > O > > > > > > 41003 > > > > > > 81 > > > > > > 1 > > > > > > 19 > > > > > > 0.16 > > > > > > 7.17 > > > > > > 2.5 > > > > > > 41005 > > > > > > 79 > > > > > > 8 > > > > > > 17 > > > > > > 0.21 > > > > > > 5.5 > > > > > > 7.2 > > > > > > 41009 > > > > > > 79 > > > > > > 2 > > > > > > 21 > > > > > > 0.218 > > > > > > 5.56 > > > > > > 4.04 > > > 41017 79 10 20 0.24 5.3 7.1 > > > > > > > > > > > > > > > > > > > > > Sincerely > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > 41017 79 10 20 0.24 5.3 7.1 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas > > wrote: > > > > > >> Hello, > > >> > > >> Here are two options, the 1st outputs a vector, the 2nd a data.frame. > > >> > > >> > > >> x<-'41003 81 1 19 0.16 7.17 2.5 > > >> 41003 77 9 22 0.197 6.8 2.2 > > >> 41003 79 7 28
Re: [R] Getting minimum value of a column according a factor column of a dataframe
Hello, Inline. Às 16:37 de 25/08/2022, avi.e.gr...@gmail.com escreveu: I read all the replies and am not sure why nobody used what I see as simpler and more direct. Assuming the ORDER of the output matters, it tends to be controlled by the order of the factor called Code so I have simple code like this: --- # Load required libraries library(dplyr) # Simulate reading in from a file by using in-line text version file_contents <- 'Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6' mydf <- read.table(text=file_contents, header=TRUE) # Group the results and provide a summary of anything you # want calculated by group: mydf %>% group_by(Code) %>% summarize(minQ=min(Q), meanQ=mean(Q), maxQ=max(Q), sdQ=sd(Q)) -- Shown as a data.frame, the result is: Code minQ meanQ maxQ sdQ 1 41003 0.160 0.189 0.210 0.025942244 2 41005 0.210 0.2117500 0.217 0.00350 3 41009 0.218 0.2268333 0.240 0.009389711 4 41017 0.240 0.240 0.240 0.0 You can remove all my extra code to just get the minimum: mydf %>% group_by(Code) %>% summarize(minQ=min(Q)) %>% as.data.frame Code minQ 1 41003 0.160 2 41005 0.210 3 41009 0.218 4 41017 0.240 The codes are shown in the same order as the data as it was made into a factor from raw data in that order. I may have misunderstood something as others provided an assortment of methods that, to me, seem less direct. The summarise/summarize function in dplyr was specifically designed to make a summary as requested, and grouped if preceded by a request. Yes, I agree with you. In the raw data the Code is already ordered so it doesn't matter if it is not addressed in the aggregation code. But from the OP's last post I conclude (wrongly?) that in the original data this need not be the case. Hence the double order() in my last post. I wonder (a) if it's really necessary and (b) if it would make a difference if the aggregated output is sorted. Rui Barradas -Original Message- From: R-help On Behalf Of Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 8:28 AM To: Ebert,Timothy Aaron ; javad bayat ; Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe My mistake, I did not change the sort order back to the original order. If you do not like the additional variables they can be dropped using select() or dat2[,-c(RN, MinByCodeQ)] syntax. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2$RN <- rownames(dat2) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) dat2<-arrange(dat2,as.numeric(RN)) -Original Message- From: R-help On Behalf Of Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 8:18 AM To: javad bayat ; Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] I missed where you explained how to choose a minimum value if there are several values within a group that are equal to the minimum value. Here is a dplyr code that returns eight values because there are ties for minimum values in Q. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) Tim -Original Message- From: R-help On Behalf Of javad bayat Sent: Thursday, August 25, 2022 12:56 AM To: Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column
Re: [R] Getting minimum value of a column according a factor column of a dataframe
I read all the replies and am not sure why nobody used what I see as simpler and more direct. Assuming the ORDER of the output matters, it tends to be controlled by the order of the factor called Code so I have simple code like this: --- # Load required libraries library(dplyr) # Simulate reading in from a file by using in-line text version file_contents <- 'Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6' mydf <- read.table(text=file_contents, header=TRUE) # Group the results and provide a summary of anything you # want calculated by group: mydf %>% group_by(Code) %>% summarize(minQ=min(Q), meanQ=mean(Q), maxQ=max(Q), sdQ=sd(Q)) -- Shown as a data.frame, the result is: Code minQ meanQ maxQ sdQ 1 41003 0.160 0.189 0.210 0.025942244 2 41005 0.210 0.2117500 0.217 0.00350 3 41009 0.218 0.2268333 0.240 0.009389711 4 41017 0.240 0.240 0.240 0.0 You can remove all my extra code to just get the minimum: mydf %>% group_by(Code) %>% summarize(minQ=min(Q)) %>% as.data.frame Code minQ 1 41003 0.160 2 41005 0.210 3 41009 0.218 4 41017 0.240 The codes are shown in the same order as the data as it was made into a factor from raw data in that order. I may have misunderstood something as others provided an assortment of methods that, to me, seem less direct. The summarise/summarize function in dplyr was specifically designed to make a summary as requested, and grouped if preceded by a request. -Original Message- From: R-help On Behalf Of Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 8:28 AM To: Ebert,Timothy Aaron ; javad bayat ; Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe My mistake, I did not change the sort order back to the original order. If you do not like the additional variables they can be dropped using select() or dat2[,-c(RN, MinByCodeQ)] syntax. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2$RN <- rownames(dat2) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) dat2<-arrange(dat2,as.numeric(RN)) -Original Message- From: R-help On Behalf Of Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 8:18 AM To: javad bayat ; Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] I missed where you explained how to choose a minimum value if there are several values within a group that are equal to the minimum value. Here is a dplyr code that returns eight values because there are ties for minimum values in Q. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) Tim -Original Message- From: R-help On Behalf Of javad bayat Sent: Thursday, August 25, 2022 12:56 AM To: Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] Dear all, Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results. All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q". The code must result in 4 rows, with the minimum value of "Q" and other column values, as below: Code Y M D Q N O 41003
Re: [R] Getting minimum value of a column according a factor column of a dataframe
My mistake, I did not change the sort order back to the original order. If you do not like the additional variables they can be dropped using select() or dat2[,-c(RN, MinByCodeQ)] syntax. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2$RN <- rownames(dat2) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) dat2<-arrange(dat2,as.numeric(RN)) -Original Message- From: R-help On Behalf Of Ebert,Timothy Aaron Sent: Thursday, August 25, 2022 8:18 AM To: javad bayat ; Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] I missed where you explained how to choose a minimum value if there are several values within a group that are equal to the minimum value. Here is a dplyr code that returns eight values because there are ties for minimum values in Q. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) Tim -Original Message- From: R-help On Behalf Of javad bayat Sent: Thursday, August 25, 2022 12:56 AM To: Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] Dear all, Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results. All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q". The code must result in 4 rows, with the minimum value of "Q" and other column values, as below: Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41005 79 8 17 0.21 5.5 7.2 41009 79 2 21 0.218 5.56 4.04 41017 79 10 20 0.24 5.3 7.1 Sincerely 41017 79 10 20 0.24 5.3 7.1 On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas wrote: > Hello, > > Here are two options, the 1st outputs a vector, the 2nd a data.frame. > > > x<-'41003 81 1 19 0.16 7.17 2.5 > 41003 77 9 22 0.197 6.8 2.2 > 41003 79 7 28 0.21 4.7 6.2 > 41005 79 8 17 0.21 5.5 7.2 > 41005 80 10 30 0.21 6.84 2.6 > 41005 80 12 20 0.21 6.84 2.4 > 41005 79 6 14 0.217 5.61 3.55 > 41009 79 2 21 0.218 5.56 4.04 > 41009 79 5 27 0.218 6.4 3.12 > 41009 80 11 29 0.22 6.84 2.8 > 41009 78 5 28 0.232 6 3.2 > 41009 81 8 20 0.233 6.39 1.6 > 41009 79 9 30 0.24 5.6 7.5 > 41017 79 10 20 0.24 5.3 7.1 > 41017 80 7 30 0.24 6.73 2.6' > df1 <- read.table(textConnection(x)) > names(df1) <- scan(what = character(), > text = 'Code Y M D Q N O') df1$Code <- > factor(df1$Code) > > # 1st option > with(df1, tapply(Q, Code, min)) > # 41003 41005 41009 41017 > # 0.160 0.210 0.218 0.240 > > # 2nd option > aggregate(Q ~ Code, df1, min) > # Code Q > # 1 41003 0.160 > # 2 41005 0.210 > # 3 41009 0.218 > # 4 41017 0.240 > > > Hope this helps, > > Rui Barradas > > Às 08:44 de 24/08/2022, javad bayat escreveu: > > Dear all; > > I am trying to get the minimum value of a column based on a factor > > column of the same data frame. My data frame is like the below: > > Code Y MD > > Q > > N O > > 41003 81 1 19 0.16 7.17 2.5 > > 41003 77 9 22 0.197 6.8 2.2 > > 41003 79 7 28 0.21 4.7 6.2 > > 41005 79 8 17 0.21 5.5 7.2 > > 41005 80 10 30 0.21 6.84 2.6 > > 41005 80 12 20 0.21 6.84 2.4 > > 41005 79 6 14 0.217 5.61 3.55 > > 41009 79 2 21 0.218 5.56 4.04 > > 41009 79 5 27 0.218 6.4 3.12 > > 41009 80 11 29 0.22 6.84 2.8 > > 41009 78 5 28 0.232 6 3.2 > > 41009 81 8 20 0.233 6.39 1.6 > > 41009 79 9 30 0.24 5.6 7.5 > > 41017 79 10 20 0.24 5.3 7.1 > > 41017 80 7 30 0.24 6.73 2.6 > > > > I want to get the minimum value of the "Q" column with the
Re: [R] Unicode chars
PDFLaTeX does support Latin-1, and this is a Latin-1 character. On Thu, 25 Aug 2022 at 15:35, Jeff Newmiller wrote: > Are you aware that pdfLatex does not support Unicode? You need to use > xeLatex. But I don't use Sweave, so I don't know how you go about making > that choice. > > On August 24, 2022 8:03:02 PM PDT, dulcalma dulcalma > wrote: > > > >Dear All > > > > > >I was trying the supplementary file GS_main.R from > >https://esajournals.onlinelibrary.wiley.com/doi/abs/10.1002/ecy.3475 > > > >I have tried to prevent latex compilation from failing using Sweave > >after trying all the online fixes I could find including using Rterm > >I could fix it if it was in the input but not in the output > >I am using R version 4.2 on windows 11 with 64 GB memory > > > > > >Sweave code > > > >\begin{small} > ><>= > >library(emdbook) # version 1.3.12 > >library(bbmle) # version 1.0.23.1 > >library(pbmcapply) # version 1.5.0 > >library(tidyverse) # version 1.3.0 > >library(ggpubr) # version 0.4.0 > >@ %% > > > > > ><>= > >summaryTable <- > >tibble(model = m.names, > > dim = m.dims[model], > > score = m.loo[model], > > delScore = score - min(score), > > se_ose = se_ose[model], > > se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = > >1:length(dim)) > >summaryTable > >@ %% > > > > > >Output > >\begin{Schunk} > >\begin{Sinput} > > summaryTable <- > > tibble(model = m.names, > > dim = m.dims[model], > > score = m.loo[model], > > delScore = score - min(score), > > se_ose = se_ose[model], > > se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = > >1:length(dim)) > > summaryTable > >\end{Sinput} > >\begin{Soutput} > ># A tibble: 10 × 7 > > model dim score delScore se_ose se_mod index > > > > 1 zero 2 908.5.8440.1 4.14 1 > > 2 d 3 904.1.7140.6 2.52 2 > > 3 q 3 907.4.9240.2 3.80 3 > > 4 qd4 902.0 40.7 04 > > 5 qdi 5 903.0.632 40.5 1.60 5 > > 6 x 6 908.5.5840.2 5.53 6 > > 7 xq7 907.4.8140.3 5.36 7 > > 8 xd7 905.2.9640.5 5.04 8 > > 9 xqd 8 903.0.908 40.5 4.52 9 > >10 xqdi 9 904.1.8940.4 4.7010 > >\end{Soutput} > >\end{Schunk} > > > > > >The problem is the output from tibble > ># A tibble: 10 × 7 > > > > > >the \times character is Unicode U+00D7 or hex \xd7 and pdflatex lualatex > >etc fail where this occurs > >Is there a way of adding "sanitizing" code in the output before > >compiling > >Or do I have to change it manually before compiling > > > > > >I do not want to switch to knitr. > > > > > >Regards > > > > > >Duncan Mackay > > > > > > > > [[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. > [[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] Getting minimum value of a column according a factor column of a dataframe
I missed where you explained how to choose a minimum value if there are several values within a group that are equal to the minimum value. Here is a dplyr code that returns eight values because there are ties for minimum values in Q. library(dplyr) library(magrittr) dat2<-read.table(text="Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2 <- dat2 %>% group_by(Code) %>% mutate( MinByCodeQ = min(Q, na.rm = T), ) %>% arrange(Code) dat2<-filter(dat2,Q==MinByCodeQ) Tim -Original Message- From: R-help On Behalf Of javad bayat Sent: Thursday, August 25, 2022 12:56 AM To: Rui Barradas Cc: R-help@r-project.org Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe [External Email] Dear all, Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results. All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q". The code must result in 4 rows, with the minimum value of "Q" and other column values, as below: Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41005 79 8 17 0.21 5.5 7.2 41009 79 2 21 0.218 5.56 4.04 41017 79 10 20 0.24 5.3 7.1 Sincerely 41017 79 10 20 0.24 5.3 7.1 On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas wrote: > Hello, > > Here are two options, the 1st outputs a vector, the 2nd a data.frame. > > > x<-'41003 81 1 19 0.16 7.17 2.5 > 41003 77 9 22 0.197 6.8 2.2 > 41003 79 7 28 0.21 4.7 6.2 > 41005 79 8 17 0.21 5.5 7.2 > 41005 80 10 30 0.21 6.84 2.6 > 41005 80 12 20 0.21 6.84 2.4 > 41005 79 6 14 0.217 5.61 3.55 > 41009 79 2 21 0.218 5.56 4.04 > 41009 79 5 27 0.218 6.4 3.12 > 41009 80 11 29 0.22 6.84 2.8 > 41009 78 5 28 0.232 6 3.2 > 41009 81 8 20 0.233 6.39 1.6 > 41009 79 9 30 0.24 5.6 7.5 > 41017 79 10 20 0.24 5.3 7.1 > 41017 80 7 30 0.24 6.73 2.6' > df1 <- read.table(textConnection(x)) > names(df1) <- scan(what = character(), > text = 'Code Y M D Q N O') df1$Code <- > factor(df1$Code) > > # 1st option > with(df1, tapply(Q, Code, min)) > # 41003 41005 41009 41017 > # 0.160 0.210 0.218 0.240 > > # 2nd option > aggregate(Q ~ Code, df1, min) > # Code Q > # 1 41003 0.160 > # 2 41005 0.210 > # 3 41009 0.218 > # 4 41017 0.240 > > > Hope this helps, > > Rui Barradas > > Às 08:44 de 24/08/2022, javad bayat escreveu: > > Dear all; > > I am trying to get the minimum value of a column based on a factor > > column of the same data frame. My data frame is like the below: > > Code Y MD > > Q > > N O > > 41003 81 1 19 0.16 7.17 2.5 > > 41003 77 9 22 0.197 6.8 2.2 > > 41003 79 7 28 0.21 4.7 6.2 > > 41005 79 8 17 0.21 5.5 7.2 > > 41005 80 10 30 0.21 6.84 2.6 > > 41005 80 12 20 0.21 6.84 2.4 > > 41005 79 6 14 0.217 5.61 3.55 > > 41009 79 2 21 0.218 5.56 4.04 > > 41009 79 5 27 0.218 6.4 3.12 > > 41009 80 11 29 0.22 6.84 2.8 > > 41009 78 5 28 0.232 6 3.2 > > 41009 81 8 20 0.233 6.39 1.6 > > 41009 79 9 30 0.24 5.6 7.5 > > 41017 79 10 20 0.24 5.3 7.1 > > 41017 80 7 30 0.24 6.73 2.6 > > > > I want to get the minimum value of the "Q" column with the whole row > > values, according to the "Code" column which is a factor. Overall > > it > will > > give me 4 rows, with the value of "Q". Below is a code that I used > > but it did not give me what I wanted. > > > >> x[which(x$Q == min(x$Q)),] > > > > Sincerely > > > > > > > -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat...@yahoo.com [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7Caf44a3eea239431cdd9808da8679e392%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970155341721228%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=bSKbIIx0Ce14UZCxV3foH%2FtDjAbc3leysR6Uu6mTSCk%3Dreserved=0 PLEASE do read the posting guide
Re: [R] Getting minimum value of a column according a factor column of a dataframe
Hello, OK, what about res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),]) do.call(rbind, res) # Code Y M D QNO # 41003 41003 81 1 19 0.160 7.17 2.50 # 41005 41005 79 8 17 0.210 5.50 7.20 # 41009 41009 79 2 21 0.218 5.56 4.04 # 41017 41017 79 10 20 0.240 5.30 7.10 A dplyr solution. suppressPackageStartupMessages(library(dplyr)) df1 %>% group_by(Code) %>% slice_min(Q) %>% slice_head(n = 1) # # A tibble: 4 × 7 # # Groups: Code [4] #Code Y M D Q N O # # 1 4100381 119 0.16 7.17 2.5 # 2 4100579 817 0.21 5.5 7.2 # 3 4100979 221 0.218 5.56 4.04 # 4 41017791020 0.24 5.3 7.1 Hope this helps, Rui Barradas Às 05:56 de 25/08/2022, javad bayat escreveu: Dear all, Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results. All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q". The code must result in 4 rows, with the minimum value of "Q" and other column values, as below: Code Y M D Q N O 41003 81 1 19 0.16 7.17 2.5 41005 79 8 17 0.21 5.5 7.2 41009 79 2 21 0.218 5.56 4.04 41017 79 10 20 0.24 5.3 7.1 Sincerely 41017 79 10 20 0.24 5.3 7.1 On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas wrote: Hello, Here are two options, the 1st outputs a vector, the 2nd a data.frame. x<-'41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6' df1 <- read.table(textConnection(x)) names(df1) <- scan(what = character(), text = 'Code Y M D Q N O') df1$Code <- factor(df1$Code) # 1st option with(df1, tapply(Q, Code, min)) # 41003 41005 41009 41017 # 0.160 0.210 0.218 0.240 # 2nd option aggregate(Q ~ Code, df1, min) # Code Q # 1 41003 0.160 # 2 41005 0.210 # 3 41009 0.218 # 4 41017 0.240 Hope this helps, Rui Barradas Às 08:44 de 24/08/2022, javad bayat escreveu: Dear all; I am trying to get the minimum value of a column based on a factor column of the same data frame. My data frame is like the below: Code Y MD Q N O 41003 81 1 19 0.16 7.17 2.5 41003 77 9 22 0.197 6.8 2.2 41003 79 7 28 0.21 4.7 6.2 41005 79 8 17 0.21 5.5 7.2 41005 80 10 30 0.21 6.84 2.6 41005 80 12 20 0.21 6.84 2.4 41005 79 6 14 0.217 5.61 3.55 41009 79 2 21 0.218 5.56 4.04 41009 79 5 27 0.218 6.4 3.12 41009 80 11 29 0.22 6.84 2.8 41009 78 5 28 0.232 6 3.2 41009 81 8 20 0.233 6.39 1.6 41009 79 9 30 0.24 5.6 7.5 41017 79 10 20 0.24 5.3 7.1 41017 80 7 30 0.24 6.73 2.6 I want to get the minimum value of the "Q" column with the whole row values, according to the "Code" column which is a factor. Overall it will give me 4 rows, with the value of "Q". Below is a code that I used but it did not give me what I wanted. x[which(x$Q == min(x$Q)),] Sincerely __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Unicode chars
On Thu, 25 Aug 2022, dulcalma dulcalma writes: > Dear All > > > I was trying the supplementary file GS_main.R from > https://esajournals.onlinelibrary.wiley.com/doi/abs/10.1002/ecy.3475 > > I have tried to prevent latex compilation from failing using Sweave > after trying all the online fixes I could find including using Rterm > I could fix it if it was in the input but not in the output > I am using R version 4.2 on windows 11 with 64 GB memory > > > Sweave code > > \begin{small} > <>= > library(emdbook) # version 1.3.12 > library(bbmle) # version 1.0.23.1 > library(pbmcapply) # version 1.5.0 > library(tidyverse) # version 1.3.0 > library(ggpubr) # version 0.4.0 > @ %% > > > <>= > summaryTable <- > tibble(model = m.names, > dim = m.dims[model], > score = m.loo[model], > delScore = score - min(score), > se_ose = se_ose[model], > se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = > 1:length(dim)) > summaryTable > @ %% > > > Output > \begin{Schunk} > \begin{Sinput} > summaryTable <- > tibble(model = m.names, > dim = m.dims[model], > score = m.loo[model], > delScore = score - min(score), > se_ose = se_ose[model], > se_mod = se_mod[model]) %>% arrange(dim) %>% mutate(index = > 1:length(dim)) > summaryTable > \end{Sinput} > \begin{Soutput} > # A tibble: 10 × 7 > model dim score delScore se_ose se_mod index > > 1 zero 2 908. 5.84 40.1 4.14 1 > 2 d 3 904. 1.71 40.6 2.52 2 > 3 q 3 907. 4.92 40.2 3.80 3 > 4 qd 4 902. 0 40.7 0 4 > 5 qdi 5 903. 0.632 40.5 1.60 5 > 6 x 6 908. 5.58 40.2 5.53 6 > 7 xq 7 907. 4.81 40.3 5.36 7 > 8 xd 7 905. 2.96 40.5 5.04 8 > 9 xqd 8 903. 0.908 40.5 4.52 9 > 10 xqdi 9 904. 1.89 40.4 4.70 10 > \end{Soutput} > \end{Schunk} > > > The problem is the output from tibble > # A tibble: 10 × 7 > > > the \times character is Unicode U+00D7 or hex \xd7 and pdflatex lualatex > etc fail where this occurs > Is there a way of adding "sanitizing" code in the output before > compiling > Or do I have to change it manually before compiling > > > I do not want to switch to knitr. > > > Regards > > > Duncan Mackay > You could try to automatically clean the code, by using ?iconv, say. But the results by not be satisfactory, depending on what characters were used. Sweave itself does not compile the LaTeX code. If you run (in R) Sweave(, encoding = "utf8") then it will produce the TeX file, which you can then compile via LuaLaTeX or XeLaTeX [see e.g. https://www.ctan.org/pkg/lualatex-doc]. For instance, on the command line, just say lualatex or another programme (such as latexmk) that your TeX distribution provides. If this is a vignette, you can specify a Makefile, see https://cran.r-project.org/doc/manuals/R-exts.html#Writing-package-vignettes > > > [[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. -- 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.