Search code examples
rdplyrsummarize

How to create a vector of variables using summarise?


Here is a beginner's problem:

I like using summarise(), but sometimes I find it difficult storing the results. For example, I know I can store 1 value in the following manner:

stdv <- Data %>%
 filter(x == 1) %>%
 summarise(stdv = sd(y))

But I get in trouble if I try to do so for more than 1 variable. I think it's something to do with creating a vector o variables in the beginning but this doesn't work:

c(dog, cat) <- Data %>%
 filter(x == 1) %>%
 summarise(dog = sd(y),
           cat = mean(y))

Can someone help? Thank ya


Solution

  • You can store it in a vector like this:

    save_vector <- df %>% 
      summarise(dog = sd(id),
                cat = var(id)) %>% 
      unlist()
    save_vector 
    
    #     dog      cat 
    #1.636392 2.677778 
    

    Data

    structure(list(id = c("1", "4", "3", "4", "6", "3", "5", "6", 
    "2", "3")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
    "data.frame"))