logo
Tags down

shadow

Alternative of summarise() function in dplyr


By : AK.Zhang
Date : July 29 2020, 04:00 PM
This might help you We can wrap the output of lm model in a list as it have many components and summarise wants to return with a length of 1 for each group
code :
library(dplyr)
food_consumption %>% 
     group_by(food_category) %>%
     summarise(mod= list(lm(co2_emmission ~ consumption)))
food_consumption %>% 
     group_by(food_category) %>%
     condense(mod= lm(co2_emmission ~ consumption))
mtcars %>% 
     group_by(cyl) %>%
     summarise(mod = list(lm(mpg ~ gear)))
# A tibble: 3 x 2
#    cyl mod   
#  <dbl> <list>
#1     4 <lm>  
#2     6 <lm>  
#3     8 <lm>  
mtcars %>% 
   group_by(cyl) %>%
   condense(mod = lm(mpg ~ gear))
# A tibble: 3 x 2
# Rowwise:  cyl
#    cyl mod   
#  <dbl> <list>
#1     4 <lm>  
#2     6 <lm>  
#3     8 <lm>  
mtcars %>% 
   group_by(cyl) %>%
    condense(mod = lm(mpg ~ gear), Coef = coef(mod))
# A tibble: 3 x 3
# Rowwise:  cyl
#    cyl mod    Coef     
#  <dbl> <list> <list>   
#1     4 <lm>   <dbl [2]>
#2     6 <lm>   <dbl [2]>
#3     8 <lm>   <dbl [2]>
mtcars %>% 
    group_by(cyl) %>%
    summarise(mod = list(lm(mpg ~ gear))) %>% 
    mutate(Coef = map(mod, coef))
# A tibble: 3 x 3
#    cyl mod    Coef     
#  <dbl> <list> <list>   
#1     4 <lm>   <dbl [2]>
#2     6 <lm>   <dbl [2]>
#3     8 <lm>   <dbl [2]>
library(purrr)
mtcars %>% 
    group_by(cyl) %>% 
    nest %>% 
    transmute(mod = map(data, ~ lm(mpg ~ gear, data = .x)))
# A tibble: 3 x 2
# Groups:   cyl [3]
#    cyl mod   
#  <dbl> <list>
#1     6 <lm>  
#2     4 <lm>  
#3     8 <lm>  


Share : facebook icon twitter icon

Reverse of summarise() function in dplyr


By : LordNash
Date : March 29 2020, 07:55 AM
it helps some times Let's consider the following data , We can use base R to do this
code :
 data1 <- as.data.frame(data1)
 data1[rep(1:nrow(data1), data1[,3]),-3]
library(splitstackshape)
data %>%
     group_by(V1, V2) %>%
     summarise(n=n())  %>%
     expandRows(., "n")
 data %>% 
    group_by(V1, V2) %>%
    summarise(n=n()) %>%
    do(data.frame(.[rep(1:nrow(.), .$n),-3]))
#       V1     V2
#     (fctr) (fctr)
#1      A      B
#2      A      B
#3      A      B
#4      B      C
#5      B      C
#6      C      D
#7      C      D
data1 <- group_by(data, V1, V2) %>% summarise(n())

filtering within the summarise function of dplyr


By : Tastes
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am struggling a little with dplyr because I want to do two things at one and wonder if it is possible. , We can try
code :
 df %>%
     group_by(id, tp) %>%
     summarise(all_mean = mean(values), 
                A_mean = mean(values[value_type=="A"]),
                value_count=sum(value_type == 'A'))

Automatic rounding in dplyr::summarise() function


By : user3419925
Date : March 29 2020, 07:55 AM
may help you . This is to do with the way tibbles are printed. The actual numbers in the data frame still have all the decimal places they are just not displayed when printing the tibble.
You can use as.data.frame or print.data.frame() which will show you more decimal points (depending on your getOption("digits")). You can also change the tibble settings but my understanding is that these are always based on significant figures rather than decimal points (so your values >100 will have fewer decimal points than values <100) See https://tibble.tidyverse.org/reference/formatting.html for tibble printing options
code :
df %>% group_by(group) %>% summarise(mL = round(mean(large),3), mS = round(mean(small),3)) %>%
     as.data.frame()
df %>% group_by(group) %>% summarise(mL = mean(large), mS = mean(small))  %>%
     as.data.frame()
df %>% group_by(group) %>% summarise_at(c("large","small"), ~round(mean(.),3)) %>% 
    print.data.frame()

Use a function of the data in dplyr::summarise


By : Col Sanderson
Date : March 29 2020, 07:55 AM
wish helps you I'm not sure I understand what you are trying to do, and I'm not familiar with the differences between the plyr and dplyr packages. The most straightforward way to do what I think you're trying to do is with daply:

dplyr conditional summarise function


By : Ben
Date : March 29 2020, 07:55 AM
will help you I have this situation where I need a different summary function based on a condition. For example, using iris, say for some reason I wanted the sum of the petal width if the species was setosa, otherwise I wanted the mean of the petal width. , This is pretty easy with data.table
code :
library(data.table)
iris2 <- as.data.table(iris)

iris2[, if(Species == 'setosa') sum(Petal.Width) 
        else mean(Petal.Width)
      , by = Species]
iris2[, ifelse(Species == 'setosa', sum, mean)(Petal.Width)
      , by = Species]
iris %>% 
  group_by(Species) %>% 
  summarise(pwz = if_else(first(Species == "setosa")
                          , sum(Petal.Width)
                          , mean(Petal.Width)))
Related Posts Related Posts :
  • Renaming labels of a factor in R
  • R Pick First Value Given Condition
  • How to transform data in Column Date using cut function in R
  • Add a series as a column in R
  • Changing value in in all rows, but the highest
  • select sub dataset with specific conditions without using apply & subset functions
  • Why is any() only defined for a numeric and not logical data.frame?
  • matching row values (text) with colnumn names and return value
  • Encrypting files you create in R
  • Renaming integers within a data.frame
  • Sorting named numeric vectors in Rcpp
  • create a new variable in the data frame based on multiple criteria in r
  • Split Postal Code in R ( just the 2 first numbers)
  • For loop is returning only last value
  • y-axis scale as percents in ggplotly
  • How to check if NA within replace() function in R?
  • Organize numbers in group ascending
  • When to use as.numeric and as.factor in R
  • How to sum two rows by a simple condition in a data frame
  • Convert List of Lists to Dataframe Based on 1 List - Every List Different
  • Order Boxplots by their Means
  • Count events in range on a vector via iteration in R
  • Nesting tibbles and performing calculations on each with a group_by
  • Skipping NA rows in index column
  • Why Do I Keep Getting Errors About Else Statements?
  • Cannot install httr package in R 3.6.2 in Linux Mint 19.3
  • I am unable to create this variable
  • How can I wrap lines of code into an function that I can run with one command in R?
  • Code for converting entire data frame to numeric
  • Using which and ! functions in R
  • How to replace NAs with values from another column in data.table (Example given)?
  • Visualising two very different distributions in one plot
  • R: a tidy way to count number of rows between pipes?
  • Collapsing rows using two vectors as indicators
  • I am trying to calculate the sum of distances between every uninfected point and an infected point
  • R lubridate: Apply helper to dataframe
  • Why does Dplyr group_by not respect .drop=FALSE
  • Is there a function in R that can convert a "time" factor (00:00:00) into seconds
  • Partial functions keeping their signature
  • Completing or inserting empty rows in-between ordered factors
  • How to sum up the duplicated value and keep all the rows on R
  • Filtering for two identical consecutive entries in a column
  • why colour not showing as argument in ggplot for R language
  • Clustering using daisy and pam in R
  • string abbreviation creating dublicates
  • Create a new matrix based on a previous one in R
  • Continuous X variable used but still getting Error: StatBin requires a continuous x variable
  • Classic Statistics Probability in R Draws
  • Removing String from the column in R
  • How to add columns to a dataframe through lapply
  • Error in match.arg(regions) : 'arg' must be NULL or a character vector
  • Substitution Encryption/Decryption in R
  • Calculate average based on date range in R
  • How to select among 3 values, the 2 closest to each other in R?
  • Generate random weights vector with fixed sum
  • glmnet: extracting standardized coefficients
  • Colour stacked bar-chart with unique colour for each bar in ggplot
  • Is there a better way in R to split a file with multiple sections
  • How to remove footnote references from a column in R?
  • Is there something wrong with the sjPlot package?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org