Use Dplyr's Summarise_Each to Return One Row Per Function

use dplyr's summarise_each to return one row per function?

You can achieve a similar output combining the dplyr and tidyr packages.
Something along these lines can help

library(dplyr)
library(tidyr)

iris %>%
select(matches("Petal")) %>%
summarise_each(funs(min, max)) %>%
gather(variable, value) %>%
separate(variable, c("var", "stat"), sep = "\\_") %>%
spread(var, value)
## stat Petal.Length Petal.Width
## 1 max 6.9 2.5
## 2 min 1.0 0.1

Reshape results of summarize all efficiently

Here is one option.

library(tidyverse)

mtcars %>%
gather(Original_Column, Value) %>%
group_by(Original_Column) %>%
summarise_all(., .funs = funs(mean, median, sd, max, min, n_distinct)) %>%
gather(Function, Result, -Original_Column)

# # A tibble: 66 x 3
# Original_Column Function Result
# <chr> <chr> <dbl>
# 1 am mean 0.406
# 2 carb mean 2.81
# 3 cyl mean 6.19
# 4 disp mean 231.
# 5 drat mean 3.60
# 6 gear mean 3.69
# 7 hp mean 147.
# 8 mpg mean 20.1
# 9 qsec mean 17.8
# 10 vs mean 0.438
# # ... with 56 more rows

How to use dplyr's coalesce function with group_by() to create one row per person with all values filled in?

We could reorder the values based on the NA elements and then slice the first row

library(dplyr)
test_dataset %>%
group_by(name) %>%
dplyr::mutate(across(starts_with('score'),
~ .x[order(is.na(.x))])) %>%
slice_head(n = 1) %>%
ungroup

-output

# A tibble: 4 × 3
name score1 score2
<chr> <dbl> <dbl>
1 corey 2 5
2 justin 1 7
3 kate NA NA
4 sib 2 9

Or another option is to use complete.cases after rearranging

test_dataset %>% 
group_by(name) %>%
dplyr::mutate(across(starts_with('score'),
~ .x[order(is.na(.x))])) %>%
filter(complete.cases(across(starts_with('score')))|row_number() == 1) %>%
ungroup

-output

# A tibble: 4 × 3
name score1 score2
<chr> <dbl> <dbl>
1 justin 1 7
2 corey 2 5
3 sib 2 9
4 kate NA NA

Better output with dplyr -- breaking functions and results

Instead of transposing (t) and changing the class types, after the summarise step, do a gather to change it to 'long' format and then spread it back after doing some modifications with separate and unite

library(tidyverse)
ds %>%
group_by(group) %>%
summarise_at(vars(iq, income, math),funs(mean, sd)) %>%
gather(key, val, iq_mean:math_sd) %>%
separate(key, into = c('key1', 'key2')) %>%
unite(group, group, key2) %>%
spread(group, val)

dplyr summarise_each with na.rm

Following the links in the doc, it seems you can use funs(mean(., na.rm = TRUE)):

library(dplyr)
by_species <- iris %>% group_by(Species)
by_species %>% summarise_each(funs(mean(., na.rm = TRUE)))

Using Dplyr group_by and Summarise and a Custom Function to Calculate the Mode of Several Groups

We can use

 Mode <- function(x) {
ux <- unique(x)
if(!anyDuplicated(x)){
NA_character_ } else {
tbl <- tabulate(match(x, ux))
toString(ux[tbl==max(tbl)])
}
}

DF %>%
group_by(Category) %>%
summarise(NumberMode = Mode(Number))
# Category NumberMode
# <fctr> <chr>
#1 A 22
#2 B 12, 14
#3 C <NA>


Related Topics



Leave a reply



Submit