0

I am new to R and trying to summarize a dataframe with multiple functions and I would like the result to appear in the same column, rather than in separated columns for each function. For example, my data set looks something like this

data =  
  A  B
  ----
  1  2
  2  2
  3  2
  4  2

And I call summarize_all(data, c(min, max)) the dataframe becomes

a_fn1 b_fn1 a_fn2 b_fn2
    1     2     4     2

How can I make it so that the result of the summarize_all becomes this:

  A  B
  ----
  1  2
  4  2

Thanks

2 Answers 2

1

Does this work:

library(dplyr)
bind_rows(apply(data,2,min),apply(data,2,max))
# A tibble: 2 x 2
      A     B
  <dbl> <dbl>
1     1     2
2     4     2
Sign up to request clarification or add additional context in comments.

2 Comments

This does work! And how could I use this in a dplyr pipe?
@Anonymous, you can try apply(data,2,min) %>% bind_rows(apply(data,2,max))
1

Here is an option with transpose

library(dplyr)
library(tidyr)
pivot_longer(df1, cols = everything()) %>% 
    group_by(name) %>% 
    summarise(min = min(value), max = max(value)) %>% 
    data.table::transpose(., make.names = 'name')
  A B
1 1 2
2 4 2

data

df1 <- structure(list(A = 1:4, B = c(2L, 2L, 2L, 2L)),
 class = "data.frame", row.names = c(NA, 
-4L))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.