简体   繁体   中英

Min Max R Getmode Datasets

This is a follow up to another question that I had earlier. I have the solution, but need help with add the last part.

library(plyr)

getmode<- function(origtable,groupby,columnname) {
    data <- ddply (origtable, groupby, .fun = function(xx){
      c(m1 = paste(names(sort(table(xx[,columnname]),decreasing=TRUE)[1]))) 
      } ) 
    return(data)
}

getmode(df,c("CreditCardType","Bank","Year"),"TotalBalance")

df<-read.table(text="CreditCardType  Bank   Year   TotalBalance
               MASTERCARD       BOFA   2017   $100
               MASTERCARD       BOFA   2017   $100
               MASTERCARD       BOFA   2017   $700
               VISA              Wells  2018   $60 
               VISA              Wells  2018   $50
               VISA              Wells  2018   $60",
               header=T, stringsAsFactors=F)

This got me the mode I was looking for! However I need min and max as well, and

there isn't any "Getmin" or "Getmax"

Any ideas??

Thanks in advance!@

Something like this...?

> df %>% 
    group_by(CreditCardType, Bank, Year) %>% 
    summarise(Min=min(TotalBalance), 
              Max=max(TotalBalance))
# A tibble: 2 x 5
# Groups:   CreditCardType, Bank [?]
  CreditCardType Bank   Year   Min   Max
  <chr>          <chr> <int> <dbl> <dbl>
1 MASTERCARD     BOFA   2017   100   700
2 VISA           Wells  2018    50    60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM